1

I wrote a dll that is dynamically called by a third party software exe. I need to get from inside the dll at runtime the full path to the dll. I've tried to follow this post:
Get DLL path at runtime
I wrote a proc inside my dll that refers to itself. But it's not working for me: the path returned is the exe path, not the dll path. What am i doing wrong?
here is the proc code (windows 7, visual studio 2008):

void stardust_checkCDM(){

    // GET this DLL path
    char dllPathName[256];
    HMODULE hm = NULL;
    char dllLocalFunc[] = "stardust_checkCDM";

    GetModuleHandleExA( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
                        GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                        (LPCSTR) &dllLocalFunc, &hm );
    GetModuleFileNameA( hm, dllPathName, sizeof(dllPathName) );
    printf( "GetModuleFileNameA: %s\n", dllPathName );

    ...

}

// dllPathName returns caller exe full path name,
// instead of dll full path name.
Community
  • 1
  • 1
Eddy Khemiri
  • 111
  • 3
  • Try the solution from this answer: http://stackoverflow.com/a/10646890/395718 . Maybe the answer needs more details: In `DllMain` on `DLL_PROCESS_ATTACH` save `hinstDLL`, and then use that `HINSTANCE` when calling `GetModuleFileName` in your code. – Dialecticus Apr 11 '15 at 08:58
  • 1
    If you do it that way, the local variable must be `static`, so `static char dllLocalFunc[] = "stardust_checkCDM";` – Roger Rowland Apr 11 '15 at 09:00

0 Answers0