Solved

Communicate with a DLL

Posted on 2003-11-30
8
331 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Author Comment

by:Belthazor
Comment Utility
@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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Accepted Solution

by:
jkr earned 350 total points
Comment Utility
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
Comment Utility
Use namespaces and standard static linking.



0
 
LVL 1

Expert Comment

by:travd
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now