Solved

Communicate with a DLL

Posted on 2003-11-30
8
336 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
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!

 
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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 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