获取DLL所在路径
Get path where DLL resides
我找到了 Get DLL path at runtime,但我不确定要为 localFunc 变量使用什么。我尝试了 DLL 的文件名,我尝试了 null 和其他一些东西,但返回的状态总是 'File Not Found'。
来自 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() 以获取包括路径在内的完全限定名称。
我找到了 Get DLL path at runtime,但我不确定要为 localFunc 变量使用什么。我尝试了 DLL 的文件名,我尝试了 null 和其他一些东西,但返回的状态总是 'File Not Found'。 来自 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() 以获取包括路径在内的完全限定名称。