Search code examples
windowsdllwin32-process

How can I find actual path to a loaded DLL with in a windows process (XP /Windows 7)


We create a DLL for other applications to load and use some of the functionality in the application. The DLL has dependency on the the actual path where it is loaded from.

  <product_home>/bin/<DLL is here>
              |
              |----/configdir/configfile
              |----/lib/<java jarfiles>

It needs the product_home location to read config files and load jar files etc

My windows application proloads a special DLL. I need to find actual path to a loaded DLL with in the process and use it to set a "HOME" variable. This will be used in rest of the processing. Using an externally set environment variable some time fails when there are multiple versions of dll present on the machine. To me it looks like DLL can figure out its own "product_home" as long it can get the actual loaded location.

The DLL This article Get Your DLL's Path/Name provides one such way- (yet to try it successfully. The generated exe crashes). Is this the correct approach?


Solution

  • Either I don't understand your need, or the link you mention is not what you need. If I understand you correctly, you'd like to get the full path of a certain DLL loaded by the process. So, say that DLL is "kernel32.dll", you'd like to get "c:\windows\system32\kernel32.dll". Please correct me if I'm wrong.

    If that's what you want, the easiest way to do that would be:

    HMODULE hModule = GetModuleHandle(_T("kernel32.dll"));
    TCHAR dllPath[_MAX_PATH];
    GetModuleFileName(hModule, dllPath, _MAX_PATH);
    

    Failures checks omitted for brevity - read more about GetModuleHandle and GetModuleFileName.