views:

107

answers:

2

Hi,

I have a question about getting DLL's directory on Windows system.

The situation is like this :

I have a DLL and an EXE file. The exe file must load the DLL to run. These 2 modules are in different directories. Moreover, the directory of the DLL is changeable. Now I have to get the directory of the DLL in "run time".

How could I do this?

Thanks in advance.

+1  A: 

I guess you need to implement some custom search algorithm. Only your exe knows which one DLL is needed and where it can be. So find the path and use it with LoadLibrary().

BTW, if possible, I would consider using COM. In this way you will use DLL stuff by some CLSID, which is completely independent from file path.

Eugene
+1  A: 

Do you need to find where the DLL is to load it or find the the path where it was loaded from?

The DLL path search algorithm is documented on MSDN and you can use the SearchPath function to search the system path.

If you need to find the path where a DLL was loaded, after it was loaded, use the GetModuleFileName function. That takes the module handle that is returned by LoadLibrary, GetModuleHandle, or passed in as hinstDLL to DllMain and returns the full path to the DLL.

shf301
Yes, I need to find the path to load it.Thanks.