Help me Exporting classes

Hello Everybody,
I am trying to export a class from dll and use it another VC++ code. The code is give below.
/*DllTest.h*/
class __declspec(dllexport) CDllTest
{
public:
__stdcall CDllTest(){}
__stdcall ~CDllTest(){}

public:
void __stdcall SayHello();
};

//

#include "stdafx.h"
#include "DllTest.h"

BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH: break;
}
return TRUE;
}

void CDllTest::SayHello()
{
printf("Hello C++!");
}

I compiled the above program and got the DllTest.Dll file. Now i want to use it in another VC++ program for that i used to versions of VC++ code that are

progr 1:

/*DllTestUse.h*/
class __declspec(dllimport) CDllTest
{
public:
__stdcall CDllTest(){}
__stdcall ~CDllTest(){}

public:
void __stdcall SayHello();
};


/* DllTestUse.cpp*/
#include "DllTestUse.h"
#include<windows.h>
#pragma comment(lib,"DllTest.lib")
void main()
{
CDllTest d;
d.SayHello();
}

It is working Ok.

prg 2:
/*DllTestUse.h*/
class __declspec(dllimport) CDllTest
{
public:
__stdcall CDllTest(){}
__stdcall ~CDllTest(){}

public:
void __stdcall SayHello();
};

/* DllTestUse.cpp*/
#include "DllTestUse.h"
#include<windows.h>
void main()
{
HINSTANCE hInstance;
hInstance=LoadLibrary("DllTest.dll");
CDllTest p;
p.SayHello();
FreeLibrary(hInstance);
}

Compiling...
DllTestUse.cpp
Linking...
DllTestUse.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall CDllTest::~CDllTest(void)" (__imp_ 1CDllTest@@QAE@XZ)
DllTestUse.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __stdcall CDllTest::SayHello(void)" (__imp_ SayHello@CDllTest@@QAGXXZ)
DllTestUse.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall CDllTest::CDllTest(void)" (__imp_ 0CDllTest@@QAE@XZ)
Debug/DllTestUse.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

DllTestUse.exe - 4 error(s), 0 warning(s)

could any body tell me what the problem is going on and how can i solve this. Thanks in Advance.




Answer this question

Help me Exporting classes

  • Umut Alev - MSFT

    You even can not export a variale from a DLL and use it with LoadLibrary.

    The reason is simple. The lnker needs all execution points of all functions at link time. But you don't have a binary image were the linker can bind to. This want to give this binary after you call LoadLibrary.

    Also. What would happen if you have 3 DLLs all implementing the same class and you call LoadLibrary

    Solution:
    Build pure virtial interfaces. Implement those in the DLL. Create a function in the DLL that returns such an interface pointer to the EXE.



  • pogsworth

    Hello Martin Richter,

    Again ,i am very pleased on your explanation and thank you very much. Could you tell me a book name or web-site that teaches how to programm COM(with out MFC) from scratch with suitable examples. Thanks in Advance.



  • nijen04

    You can not use dynamic loading and exporting of a class from a DLL. The linkage to the function is placved directly inside the EXE you can not make this dynamic.

    One way would be to build a wrapper class inside the EXE and redirect the calls to the final DLL class function by function. Its not possible to di this as a whole for the complete class.

    What you can do, is to use a pure Interface without the implementation like COM does to solve this problem.



  • Mexx

    For pure simple COM I prefer ATL.
    And a good book for it is "ATL Internals" from Rector/Sells!



  • Arjan Ligthart

    Hello Martin Richter,

    Thank you very much on explaining my problem. But, can you cleary explain or give reference to a web-site that clears my doubt. i.e, we can export functions and variable from a dll, but why not we export classes from a dll. what is the reason . Thanks in advance.



  • Help me Exporting classes