Clark Gaebel
Clark Gaebel

Reputation: 17988

Getting the name of a DLL from within the dll

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

Is it GetModuleFileName(hModule, buffer); ?

Upvotes: 5

Views: 5302

Answers (2)

Shay Erlichmen
Shay Erlichmen

Reputation: 31928

yes, you need to store hModule in DllMain

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

Upvotes: 8

anon
anon

Reputation:

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.

Upvotes: 4

Related Questions