I have an interesting situation where I have an application X that dynamically loads any DLLs [A, B, C, ...] in a subfolder. Both X and the DLLs need to use the same DLL Y in the same folder as X. Y needs to be (preferably) statically linked to X and the other DLLs, but it has to be the same instance.
What is the best way to handle this? I control the sources of X and the DLLs it loads, but not Y.
The MODULEENTRY32 struct has a memeber named szExePath that will give you the directory of the DLL, pass this to LoadLibrary(...) [ http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx ] and you should be all set. All you would need is to know what DLL you are looking for, or alternativley you may be able to load them all.
I would just use LoadLibrary (with FindfirstFile, etc) to load the files in your subfolder (A, B, C, ...). As Y is in the app directory, it should be found automatically. (Failing that, use SetCurrentDirectory to set the directory). A pretty standard approach for handling plug-ins and the like.
Sorry - my brain didn't register the "be the same instance"...
Can you provide A, B, C... with a callback and have the app talk to Y on their behalf. That way you don't have to bend any rules
If A, B, C... only need to call C-style functions exported by Y, you could provide the with an array of function pointers (inspired by Winsock SPI's NSPStartup routine's LPNSP_ROUTINE parameter)