views:

15

answers:

2

I created a dll with VS C++ (of course as a dll project) with the following code of the header file:

#pragma once
#include <iostream>
#include "..\..\profiles/ProfileInterface.h"

using namespace std;

extern "C" __declspec(dllexport) class CExportCoordinator: public CProfileInterface
{
public:
    CExportCoordinator(void);
    virtual ~CExportCoordinator(void);

    CProfileInterface* Create();
    void Initialize();
    void Start();   
};

Here is .cpp file of the dll:

#include "StdAfx.h"
#include "ExportCoordinator.h"

CExportCoordinator::CExportCoordinator(void)
{
}

CExportCoordinator::~CExportCoordinator(void)
{
}

CProfileInterface* CExportCoordinator::Create(){

    cout << "ExportCoordinator3 created..." << endl;
    return new CExportCoordinator();
}

void CExportCoordinator::Initialize(){

        cout << "ExportCoordinator3 initialized..." << endl;
}

void CExportCoordinator::Start(){

    cout << "ExportCoordinator3 started..." << endl;
}

I exported the whole class CExportCoordinator because I need to use all three methods it offers. Following is the code from the main application loading the, above given, dll on the fly.

    typedef CProfileInterface* (WINAPI*Create)();

    int _tmain(int argc, _TCHAR* argv[])

{    
    HMODULE hLib = LoadLibrary(name);


    if(hLib==NULL) {
        cout << "Unable to load library!" << endl;         
        return NULL;
    }
    char mod[MAXMODULE];

    GetModuleFileName(hLib, (LPTSTR)mod, MAXMODULE);
    cout << "Library loaded: " << mod << endl;   

    Create procAdd = (Create) GetProcAddress(hLib,"Create");

    if (!procAdd){
        cout << "function pointer not loaded";
    }
    return;
}

On the output I get that correct library is loaded, but that function pointer procAdd is NULL. I thought it had something to do with name mangling and added extern "C" when exporting the class in header of dll, but nothing changed. Btw, I used dll export viewer for viewing the exported functions of the class, and the whole class is exported correctly. Any help?

UPDATE
there is an error in the header file of dll. I shouldn't be using extern "C" __declspec(dllexport) before class because then class won't be exported at all. If I use class __declspec(dllexport) CExportCoordinator then the class is exported correctly, but anyway I can't get the address of the function other than NULL.

+1  A: 

It seems to me that you should declare Create method as a static method and export this method only. If you will stay have NULL in GetProcAddress you should examine exports of your DLL with respect of Dependency Walker (see http://www.dependencywalker.com/) and modify the name of the function "Create" to something like "_Create" or "_Create@2".

Oleg
+1  A: 
extern "C" __declspec(dllexport) class CExportCoordinator: public CProfileInterface 
{ 

This is nonsense. A class cannot be "extern C"

... inside the class ...
    CProfileInterface* Create();  

This creates a member function of the class, which is not probably what you want. For one thing, it will be mangled in the DLL, second, it will not be callable without the this pointer. Probably, you need this declaration:

extern "C" __declspec(dllexport) CProfileInterface* Create();  

and implemntation:

extern "C" __declspec(dllexport) CProfileInterface* Create(){
    cout << "ExportCoordinator3 created..." << endl;     
    return new CExportCoordinator();     
}   
jpalecek