gpt4 book ai didi

c++ - 获取DLL所在路径

转载 作者:行者123 更新时间:2023-11-28 00:17:35 38 4
gpt4 key购买 nike

我找到了 Get DLL path at runtime但我不确定对 localFunc 变量使用什么。我尝试了 DLL 的文件名,我尝试了 null 和其他一些东西,但返回的状态始终是“找不到文件”。来自 MSDN:

lpModuleName [in, optional]The name of the loaded module (either a .dll or .exe file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).

所以我假设它们只是指普通文件名,例如“MyControl.dll”,而不是文件路径,因为我不知道路径。编辑:添加实际代码:

char localFunc[MAX_PATH]
sprintf_s(localFunc, 52, "MyActiveXComponent.dll");
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR) &localFunc, &hm))
{
int ret = GetLastError();
OutFile << L"GetModuleHandle returned " << ret << std::endl;
} else {
GetModuleFileNameA(hm, path, sizeof(path));
OutFile << L"Path of dll is:" << path << L"<" << std::endl;
}

这是我最终得到的结果(两种方式都执行)

LPCWSTR anotherFunc = L"MyActiveXComponents.dll";
HMODULE hm2 = GetModuleHandle(anotherFunc); // get the handle to the module
LPWSTR anotherPath = new WCHAR[MAX_PATH];
GetModuleFileName(hm2, anotherPath, MAX_PATH); // get the full path
OutFile << L"Path of dll is:" << anotherPath << L"<" << std::endl;

这是另一种方式。

char path[MAX_PATH];
HMODULE hm = NULL;
char localFunc[MAX_PATH] = {"MyActiveXComponents.dll"};
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, LPCSTR) &localFunc, &hm))
{
int ret = GetLastError();
OutFile << L"GetModuleHandle returned " << ret << std::endl;
} else {
GetModuleFileNameA(hm, path, sizeof(path));
OutFile << L"Path of dll is:" << path << L"<" << std::endl;
}

谢谢。我敢肯定这是个简单的问题。

最佳答案

使用原始名称(如 user32.dll 或 DLL 的任何名称)调用 GetModuleHandle()。获得句柄后,调用 GetModuleFileName() 以获取包括路径在内的完全限定名称。

关于c++ - 获取DLL所在路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29304241/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com