dll内获取自身路径的方法(windows)

include <windows.h>  

bool GetSelfModulePath(char* path)
{
    MEMORY_BASIC_INFORMATION mbi;
    HMODULE dllHandle = ((::VirtualQuery(GetSelfModulePath, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL);
    TCHAR t_path[MAX_PATH] = { 0 };
    GetModuleFileName(dllHandle, t_path, MAX_PATH);
    int iLength = WideCharToMultiByte(CP_ACP, 0, t_path, -1, NULL, 0, NULL, NULL);
    return WideCharToMultiByte(CP_ACP, 0, t_path, -1, path, iLength, NULL, NULL);
}

Ps: 拿到path之后,应尽量去掉可能的尾部空数据

猜你喜欢

转载自blog.csdn.net/a34140974/article/details/80001597