gpt4 book ai didi

c++ - 在运行时获取 DLL 路径

转载 作者:IT老高 更新时间:2023-10-28 13:58:13 28 4
gpt4 key购买 nike

我想从其代码中获取 dll 的 目录(或文件)路径。 (不是程序的.exe文件路径)

我尝试了一些我发现的方法:
GetCurrentDir - 获取当前目录路径。
GetModuleFileName - 获取可执行文件的路径。

那么我怎样才能找出代码在哪个dll中呢?
我正在寻找类似于 C# 的 Assembly.GetExecutingAssembly

的东西

最佳答案

您可以使用 GetModuleHandleEx 函数获取 DLL 中静态函数的句柄。你会发现更多信息here .

之后,您可以使用 GetModuleFileName 从刚刚获得的句柄中获取路径。有关该电话的更多信息是here .

一个完整的例子:

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.

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

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