Solved

Communicate with a DLL

Posted on 2003-11-30
8
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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.

739 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