在运行时解析函数,而不是在 C++ 编译时

Resolving functions at runtime, not at compilation C++

我正在使用 Visual Studio (2017) 在 C++ 中编写代码并尝试使用第 3 方库中的函数。

有人告诉我“[库] 不提供链接器库,因此您需要在运行时而不是编译时解析函数”。

如何在 Visual Studio 中完成?

谢谢

编辑:显然我尝试了谷歌搜索,但出现的都是修复编译和运行时错误的建议

如果您只需要几个功能

HMODULE hMod = LoadLibrary("C:\lib\path\library.dll");
void* fnPtr = GetProcAddress(hMod, "nameOfExportedFunction");

要调用该函数,您需要了解调用约定和参数,以下是如何执行此操作的示例:

typedef void* (__cdecl* _Cvar_Get)(const char* var_name, const char* var_value, int flags);
_Cvar_Get Cvar_Get = (_Cvar_Get)GetProcAddress(hMod, "nameOfExportedFunction");
void* result = Cvar_Get("cl_gamepath", "Name", 0);

如果 DLL 与您的 exe 位于同一目录中,那么您可以只使用“library.dll”

如果您需要所有功能,请使用 this dxiv 在评论中发布的答案