c++ - 在 Windows 上加载 DLL 失败

标签 c++ dll

作为一名 Xcode 开发人员,我也必须在 Windows 上使用我编写的代码。我认为我已经成功掌握了所有跨平台问题,但现在我在理解 Windows 上的 DLL hell 方面遇到了真正的问题。 我成功地将我的代码与 Xcode 和 Gcc (Ubuntu) 一起使用。在 Windows 上,我收到一条错误消息:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

我读了很多关于这个用途的资料,但我在理解这个问题时遇到了问题。 通常在 Windows 上我有类似

#define MYLIB_API __declspec(dllimport)

我在 Bass 库 (bass.h) 的 header 中找不到它。只有一行

#define BASSDEF(f) WINAPI f

现在,我尝试在我的代码中动态加载 DLL 函数。您可以在底部看到动态加载标题作为链接。在这里复制太多。这种动态加载适用于 .dylib 和 .so 库,不适用于 .dll 我的目标是使用额外的库动态加载 DLL 而不是静态的。

在我的代码中,我使用了 bass.h 和 bassdecode.h。在我的代码中,我称之为示例:

bool returnVar = _BASS_SetConfig(BASS_CONFIG_DEV_DEFAULT,1);

在这里我得到了调用约定消息。

我必须在头文件中做什么才能在 Windows 上成功导入 DLL 函数?

您可以在以下位置下载文件:header files to download

最佳答案

好的,对于所有遇到相同问题的人,解决方案是 Hans Passant 的回答。我无法将此答案标记为解决方案,因此我想给他声誉。

函数的原始类型定义:

typedef BOOL    (*BASS_SetConfig_Type)(DWORD option, DWORD value);

在DLL中搜索

_BASS_SetConfig = (BASS_SetConfig_Type)DllFindSym(m_hMod, "BASS_SetConfig")

DLLFindSym 定义为:

#define DllFindSym(handle,name) (GetProcAddress(handle,name))   

现在将 typedef 更改为

typedef BOOL    (__stdcall *BASS_SetConfig_Type)(DWORD option, DWORD value);

现在 Windows 中的一切都像魅力一样运作。非常感谢 Hans Passant 的快速提示。

关于c++ - 在 Windows 上加载 DLL 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55508996/

相关文章:

C++重新定义

c++ - Xcode 中的警告不正确?

C++ 使用 INT32 : Implicit conversion loses integer precision

c# - System.Printing 未找到(C#)?

c++ - 如何将 C++ 库与 CGO 和 Swig 链接起来?

c++ - 为 C++ 获取 AST?

c++ - C onnx header 找不到 OrtEnv 定义

delphi - 使用 SuperObject 和 OmniThreadLibrary 从 DLL 获取 JSON 数据时出现问题

c++ - DLL加载库-错误代码126

python-3.x - 如何将整数解释为 float