tags:

views:

119

answers:

2

If I have a dll called "foo.dll" and the end user renames it to "bar.dll" and LoadLibrary's it, how can I get the name "bar.dll" from inside my dll?

Is it GetModuleFilename(hModule, buffer); ?

+4  A: 

yes, you need to store hModule in DllMain

BOOL WINAPI DllMain(HINSTANCE hinstDLL,  DWORD fdwReason,  LPVOID lpvReserved)
{
  switch (hinstDLL)
  {
    case DLL_PROCESS_ATTACH:
      hModule = hinstDLL
      break;
  }
}
Shay Erlichmen
+2  A: 

You need to provide DllMain function, store the module handle you get passed in a static variable, and then use that variable to call GetModuleFilename. You should avoid calling GetModuleFilename (or any other function) in DllMain itself, as Windows is very picky about what you can and can't do in there.

anon