Reputation: 25
I have tried to search for answers to this but nothing quite does what I want.
I am writing a set of DLL's that can be used as a package or separately. How would I go about Detecting in DLL-A if the application has also linked to DLL-B
The application is not always written by me, just the DLL's.
Once I have confirmation that DLL-B is part of the program, I'd like to be able to communicate between the 2 DLL's.
Let me know if this doesn't make sense, it's VERY late at night :P
Upvotes: 0
Views: 71
Reputation: 2081
You need to create a DLLMain
function which will be called when your DLL gets loaded or unloaded.
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682583(v=vs.85).aspx
BOOL WINAPI DllMain(HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
/* Init Code here */
break;
case DLL_THREAD_ATTACH:
/* Thread-specific init code here */
break;
case DLL_THREAD_DETACH:
/* Thread-specific cleanup code here.
*/
break;
case DLL_PROCESS_DETACH:
/* Cleanup code here */
break;
}
/* The return value is used for successful DLL_PROCESS_ATTACH */
return TRUE;
}
Now in here you'll need to call a function of your devising to register the fact that the DLL loaded (N.B. Make sure you look at what each switch case means above and initialise appropriately). it could do pretty much anything to be honest but maybe call a singleton and register (create an instance of your interface?) there.
Once you have that you should be able to do what you want. If you register both DLLs in the same way you will get the 2 way comms.
N.B. if you are providing an API it would probably be good if you have some initialisation call common to both DLLs so that the user sets up a "shared environment"
Upvotes: 0
Reputation: 98496
Just call GetModuleHandle()
with the name of the DLL you want to check, without the path. If it returns NULL
, then the DLL is not loaded. If it returns otherwise, it is loaded.
Then you can go on and call GetProcAddress()
with the DLL handle and the name of the function you want to call. It will return a pointer to that function that you can cast to the appropriate function pointer type and call it.
Upvotes: 3