Belthazor
asked on
Communicate with a DLL
Hi,
what I want to do: Different DLL's (say "DLL1.dll" and "DLL2.dll", each containing the same class (say MyClass) with some functions (say int MyClass::Function1, CString MyClass::Function2). The only difference between the two DLL's is the implementation of the functions.
How can I initialize the class from my main application and use the different functions (this includes using the return values)?
thx in advance
Belthazor
what I want to do: Different DLL's (say "DLL1.dll" and "DLL2.dll", each containing the same class (say MyClass) with some functions (say int MyClass::Function1, CString MyClass::Function2). The only difference between the two DLL's is the implementation of the functions.
How can I initialize the class from my main application and use the different functions (this includes using the return values)?
thx in advance
Belthazor
ASKER
Wow, this was fast! Thx!
I knew the LoadLibrary function but this is not my Problem! Say MyClass is not exported, then i can do the following:
MyClass MyObject;
int myInteger = MyObject.Function1;
But as soon as I export 'MyClass' I won't have 'MyObject' to call 'Function1' ! How to solve this?
I knew the LoadLibrary function but this is not my Problem! Say MyClass is not exported, then i can do the following:
MyClass MyObject;
int myInteger = MyObject.Function1;
But as soon as I export 'MyClass' I won't have 'MyObject' to call 'Function1' ! How to solve this?
Just export a single function from the dlls that returns a pointer to the newly created instance of the class, e.g.
CMyClass*
__declspec(dllexport) CreateMyClass () { return new CMyClass();}
and have them return an instance of the different implementations. To ensure clean memory deallocation, add
void
__declspec(dllexport) DeleteMyClass (CMyClass* p) { delete p;}
CMyClass*
__declspec(dllexport) CreateMyClass () { return new CMyClass();}
and have them return an instance of the different implementations. To ensure clean memory deallocation, add
void
__declspec(dllexport) DeleteMyClass (CMyClass* p) { delete p;}
ASKER
@jkr: I exactly did what you said, compiled the DLL and placed it to C:\DLL1.dll
now i am trying to import the function:
typedef UINT (CALLBACK* LPFNDLLFUNC1)();
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
UINT uReturnVal;
hDLL = LoadLibrary("C:\\DLL1.dll" );
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddre ss(hDLL,"C reateMyCla ss");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
}
else
{
// call the function
uReturnVal = lpfnDllFunc1();
}
}
When I run this code, GetProcAddress returns NULL and GetLastError returns 127 (The specified procedure could not be found.)
What did I do wrong?
Belthazor
now i am trying to import the function:
typedef UINT (CALLBACK* LPFNDLLFUNC1)();
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
UINT uReturnVal;
hDLL = LoadLibrary("C:\\DLL1.dll"
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddre
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
}
else
{
// call the function
uReturnVal = lpfnDllFunc1();
}
}
When I run this code, GetProcAddress returns NULL and GetLastError returns 127 (The specified procedure could not be found.)
What did I do wrong?
Belthazor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Use namespaces and standard static linking.
The problem with that solution is that you have qualify everything you use from the dll with the namespace. With the first solution, once you've done the load library function, the code will function transparently with either dll. Adding a third dll, etc would also be trivial.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: jkr {http:#9847264}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: jkr {http:#9847264}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
Finally unload the library with FreeLibrary:
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
DWORD dwParam1;
UINT uParam2, uReturnVal;
hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddre
"DLLFunc1");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
return SOME_ERROR_CODE;
}
else
{
// call the function
uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
}
}