Link to home
Start Free TrialLog in
Avatar of Belthazor
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
Avatar of arjanh
arjanh

use Windows Api function LoadLibrary to either load DLL1 or DLL2, and then use GetProcAddress to get the address of your functions.
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)GetProcAddress(hDLL,
                                           "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);
      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}
Avatar of 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?

Avatar of jkr
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;}
@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)GetProcAddress(hDLL,"CreateMyClass");
   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
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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