Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DLL path at runtime

Tags:

c++

dll

I want to get a dll's directory (or file) path from within its code. (not the program's .exe file path)

I've tried a few methods I've found:
GetCurrentDir - gets the current directory path.
GetModuleFileName - gets the executable's path.

So how can i find out in which dll the code is in ?
I'm looking for something similar to C#'s Assembly.GetExecutingAssembly

like image 773
Yochai Timmer Avatar asked Aug 03 '11 09:08

Yochai Timmer


People also ask

How do I find the path of a DLL?

Dll files are located in C:\Windows\System32.


4 Answers

You can use the GetModuleHandleEx function and get the handle to a static function in your DLL. You'll find more information here.

After that you can use GetModuleFileName to get the path from the handle you just obtained. More information on that call is here.

A complete example:

char path[MAX_PATH]; HMODULE hm = NULL;  if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |          GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,         (LPCSTR) &functionInThisDll, &hm) == 0) {     int ret = GetLastError();     fprintf(stderr, "GetModuleHandle failed, error = %d\n", ret);     // Return or however you want to handle an error. } if (GetModuleFileName(hm, path, sizeof(path)) == 0) {     int ret = GetLastError();     fprintf(stderr, "GetModuleFileName failed, error = %d\n", ret);     // Return or however you want to handle an error. }  // The path variable should now contain the full filepath for this DLL. 
like image 81
mkaes Avatar answered Oct 01 '22 12:10

mkaes


EXTERN_C IMAGE_DOS_HEADER __ImageBase; 

....

TCHAR   DllPath[MAX_PATH] = {0}; GetModuleFileName((HINSTANCE)&__ImageBase, DllPath, _countof(DllPath)); 
like image 21
cprogrammer Avatar answered Oct 01 '22 12:10

cprogrammer


GetModuleFileName() works fine from inside the DLL's codes. Just be sure NOT to set the first parameter to NULL, as that will get the filename of the calling process. You need to specify the DLL's actual module instance instead. You get that as an input parameter in the DLL's DllEntryPoint() function, just save it to a variable somewhere for later use when needed.

like image 21
Remy Lebeau Avatar answered Oct 01 '22 13:10

Remy Lebeau


Here's a Unicode, revised version of the top voted answer:

CStringW thisDllDirPath()
{
    CStringW thisPath = L"";
    WCHAR path[MAX_PATH];
    HMODULE hm;
    if( GetModuleHandleExW( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | 
                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                            (LPWSTR) &thisDllDirPath, &hm ) )
    {
        GetModuleFileNameW( hm, path, MAX_PATH );
        PathRemoveFileSpecW( path );
        thisPath = CStringW( path );
        if( !thisPath.IsEmpty() && 
            thisPath.GetAt( thisPath.GetLength()-1 ) != '\\' ) 
            thisPath += L"\\";
    }
    else if( _DEBUG ) std::wcout << L"GetModuleHandle Error: " << GetLastError() << std::endl;
    
    if( _DEBUG ) std::wcout << L"thisDllDirPath: [" << CStringW::PCXSTR( thisPath ) << L"]" << std::endl;       
    return thisPath;
}
like image 39
BuvinJ Avatar answered Oct 01 '22 11:10

BuvinJ