Re: [Dev-C++] DLL: Howto make an instance of a class defined in a DLL
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <Ber...@eu...> - 2006-08-24 12:40:53
|
Per Wustermark wrote:
> The easiest way to instantiate classes defined in a DLL is to=20
> give the DLL
> file a factory function, i.e. a function that returns new=20
> DllClass(xxx);
OK, I tried the following now:
dll.h:
DllClass* DLLIMPORT createDllClass();
Dll.cpp:
DllClass* createDllClass() {
return new DllClass;
}
In my program using the dll:
HMODULE hMod =3D LoadLibrary("mydll.dll");
if (hMod=3D=3DNULL) printf("NOT LOADED\n"); else printf("LOADED\n");
typedef DllClass* (*PCreate) ();
PCreate createDC=3D(PCreate)::GetProcAddress =
(hMod,"_Z14createDllClassv");
DllClass *pD=3DcreateDC();
Now I can access interal public variables of the class, f.e.:
printf("Value v: %v\n",pD->v");
But not execute a void of this class:
pD->test();
gives a linker error for an unresolved symbol.
When I try:
void (FAR __stdcall *(test))()=3DNULL;
test=3D (void (__stdcall *)())GetProcAddress =
(hMod,"_ZN8DllClass5testEv");
I can execute:
test2();
But in the function test there is something like:
void DllClass::test() {
v=3Dv+10;
printf("Value of v: %d\n",v);
}
In the constructor v is set to 5, so it must be 15 here.
But I get either an undefined very big value or the program crashes. =
Obviously the memory is not allocated.
So I think that the instance of the class represented by the pointer pD =
has nothing to do with the the second GetProcAddress to get access to =
the void test() in the class. I think it is something like a static call =
DllClass::test(), but this is not what I like to get.
Any more suggestions?
Thank you for tips, Bernd
|