0

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

Kiasanth
  • 25
  • 3
  • The easiest way would be to have your DLLs register themselves (when loaded) in some data structure and then you can create some interface(or use the interface) for communication between them... Registry key? Edit - or do you mean you need to know whether it is there at compile time? – Caribou Jan 30 '13 at 15:04
  • No, not at compile (or link) time. just at run time. as for the registry option, I'd much prefer something more internal to the application. – Kiasanth Jan 30 '13 at 15:18
  • An even easier way would be to have DLL A depend upon DLL B, so when DLL A is loaded, DLL B will also be loaded automatically. – Jerry Coffin Jan 30 '13 at 15:20
  • @JerryCoffin Thats actually a better way that what I was going to suggest, if you can do it Kiasanth... – Caribou Jan 30 '13 at 15:21
  • The thing is, I don't want to force linkage of DLL-B, I only want to communicate with it "IF" it is already linked in the same application. EDIT: Preferably without forcing anyone who uses my DLL's to have to handle any of this in their program. – Kiasanth Jan 30 '13 at 15:24
  • I think I'd go with something along the lines of Caribou's suggestion. Why not have your DLLs generate a file in the application's executable directory that is a list of all registered DLLs? This way each DLL can consult this file to see which DLLs are currently loaded? Each DLL removes itself from the file on Unregister. The last one out cleans up the file. Maybe? – Aeluned Jan 30 '13 at 15:42

2 Answers2

3

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.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
0

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"

Community
  • 1
  • 1
Caribou
  • 2,070
  • 13
  • 29
  • Interesting thought, I already have the DLLMain Function set up. Now I have to do some research into singletons (mainly what they are and how to use them) Thanks – Kiasanth Jan 30 '13 at 15:40
  • can't do worse than here : http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – Caribou Jan 30 '13 at 15:49