0

I found Get DLL path at runtime but I'm not sure what to use for the localFunc variable. I tried the filename of the DLL, I tried null and some other things, but the status returned was always 'File Not Found'. From the MSDN:

lpModuleName [in, optional] The name of the loaded module (either a .dll or .exe file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).

So I assume they just mean the plain file name eg, "MyControl.dll" and not the path to file, since I do not know the path. Edit: added the actual code:

char localFunc[MAX_PATH]
sprintf_s(localFunc, 52, "MyActiveXComponent.dll");
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &localFunc, &hm))
{
    int ret = GetLastError();
    OutFile << L"GetModuleHandle returned " <<  ret << std::endl;
} else {
    GetModuleFileNameA(hm, path, sizeof(path));
    OutFile << L"Path of dll is:" << path << L"<" << std::endl;
}

Here's what I ended up with (performed both ways)

LPCWSTR anotherFunc = L"MyActiveXComponents.dll";
HMODULE hm2 = GetModuleHandle(anotherFunc);  // get the handle to the module
LPWSTR anotherPath = new WCHAR[MAX_PATH];
GetModuleFileName(hm2, anotherPath, MAX_PATH);  // get the full path
OutFile << L"Path of dll is:" << anotherPath << L"<" << std::endl;

Here's the other way.

char path[MAX_PATH];
HMODULE hm = NULL;
char localFunc[MAX_PATH] = {"MyActiveXComponents.dll"};
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, LPCSTR) &localFunc, &hm))
{
    int ret = GetLastError();
    OutFile << L"GetModuleHandle returned " <<  ret << std::endl;
} else {
    GetModuleFileNameA(hm, path, sizeof(path));
    OutFile << L"Path of dll is:" << path << L"<" << std::endl;
}

Thanks. I'm sure it is simple question.

Vortico
  • 2,610
  • 2
  • 32
  • 49
Ryan Buton
  • 341
  • 3
  • 11
  • If the DLL resides in the same path as the executable, then the executable should be able to find it without a full path. Otherwise and unless the DLL is known to be system-wide (probably not unless you are using Windows DLLs), the full path or relative path will be required for your application to find it. – David Peterson Mar 27 '15 at 15:24
  • 1
    Since it says "or an address in the modulle" that leads me to believe it can be the address of any function in the module. So make void Foo() { } then pass &Foo, and add GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS into the dwFlags parameter. – Moby Disk Mar 27 '15 at 15:31
  • @DavidPeterson So I need to know the path to the DLL to call the function that will tell me the path to the DLL? Seems a little weird, so maybe it only applies to Windows DLL's and not user created DLL's. – Ryan Buton Mar 27 '15 at 16:07
  • Are you trying to get the path of a DLL you created, or a 3rd-party DLL? – Moby Disk Mar 27 '15 at 17:32
  • 1
    you do not need to know exactly the location of the DLL to load it or to resolve its path, so long as that location can be resolved by following the [dynamic library search order](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx), which includes the rules David mentioned (and more, but it does not include searching all connected drives). – SleuthEye Mar 27 '15 at 18:31
  • @MobyDisk I am trying to find the location of my DLL. On a developer's machine it is in one location and would reference files relative to its' own location. When the application is installed it would look under "My Documents"\etc... to find the files it needs. That's why I need to know where the DLL file is located, not where the Executable that is using it is located. I will place what I tried in my original question. – Ryan Buton Mar 27 '15 at 18:48
  • After re-reading the documentation it looks like SleuthEye is on the right track. I would try simply passing the DLL name (with or without extension) and see what happens since the DLL is already loaded in memory. In fact, this seems to be Joe Willcoxson's recommendation. I assume there is a data map behind the scenes that maps DLL module names to meta information including the loaded file path, and simply passing the module name will retrieve the information you requested from it. – David Peterson Mar 27 '15 at 19:17

1 Answers1

4

Call GetModuleHandle() with the raw name like user32.dll or whatever the name of the DLL is. After you have the handle, call GetModuleFileName() to get the fully qualified name including path.

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29