Solved

Communicate with a DLL

Posted on 2003-11-30
8
334 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:Belthazor
8 Comments
 
LVL 5

Expert Comment

by:arjanh
ID: 9845527
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);
   }
}
0
 
LVL 3

Author Comment

by:Belthazor
ID: 9845601
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?

0
 
LVL 86

Expert Comment

by:jkr
ID: 9846068
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;}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 3

Author Comment

by:Belthazor
ID: 9846877
@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
0
 
LVL 86

Accepted Solution

by:
jkr earned 350 total points
ID: 9847264
Well, there are a few thingws going wrong - first of all, the typedef is way off, it should be

typedef CMyClass* (CALLBACK* GETCMYCLASS)();

Then, you need to check under which name the function is exported. To simplify that, you could use

extern "C"
void*
__declspec(dllexport) CreateMyClass () { return new CMyClass();}

and have them return an instance of the different implementations. To ensure clean memory deallocation, add

extern "C"
void
__declspec(dllexport) DeleteMyClass (CMyClass* p) { delete p;}


to turn off the C++ name mangling. Then,

typedef CMyClass* (CALLBACK* GETCMYCLASS)();

HINSTANCE hDLL;               // Handle to DLL
GETCMYCLASS pGetCMyClass;    // Function pointer
CMyClass*  pReturnVal;

hDLL = LoadLibrary("C:\\DLL1.dll");
if (hDLL != NULL)
{
  pGetCMyClass = (GETCMYCLASS)GetProcAddress(hDLL,"CreateMyClass");
  if (!pGetCMyClass)
  {
     // handle the error
     FreeLibrary(hDLL);      
 
  }
  else
  {
     // call the function
     pReturnVal = (CMyClass*)pGetCMyClass();
  }
}



0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9848397
Use namespaces and standard static linking.



0
 
LVL 1

Expert Comment

by:travd
ID: 9872364
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.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286255
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question