C++ 运行时检查失败 #0 - ESP 的值未在函数调用中正确保存

标签 c++ dll

我正在尝试使用 C++ 编写 motorbee

当我运行代码时出现以下错误:

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.

这是我的代码。

#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include "mt.h"
using namespace std;

HINSTANCE BeeHandle= LoadLibrary("mtb.dll"); 
Type_InitMotoBee InitMotoBee;
Type_SetMotors SetMotors;
Type_Digital_IO Digital_IO;
void main ()
{   
    InitMotoBee = (Type_InitMotoBee)GetProcAddress( BeeHandle,"InitMotoBee");
    SetMotors =(Type_SetMotors)GetProcAddress(BeeHandle,"SetMotors");
    Digital_IO =(Type_Digital_IO)GetProcAddress(BeeHandle,"Digital_IO ");
    InitMotoBee();

    SetMotors(0, 50, 0, 0, 0, 0, 0, 0, 0);

}

最佳答案

您的 typedef 函数指针需要匹配 calling convention您正在使用的图书馆。例如,如果 InitMotoBee 使用 cdecl你的 typedef 看起来像:

typedef bool (__cdecl *Type_InitMotoBee)(void)

SetMotors 函数采用参数,因此也需要为此正确设置调用约定(这很可能是应用程序失败的地方)。

关于C++ 运行时检查失败 #0 - ESP 的值未在函数调用中正确保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079625/

相关文章:

c++ - 我们可以通过 IPC 传递具有动态管理成员的类的对象吗?

dll - 使用本地类库作为 Web API 的资源(ASP.NET Core 1.0)

java - 内部调用另一个 dll 的 JNI DLL 的依赖项

c++ - 将包含 0 &1 的文本文件转换为二进制文件

c++ - 同一函数中的异常处理会使编译时间减慢 2 倍以上,为什么?

读取文本文件的 C++ 类 - 出现错误

c++ - Qt 创作者 3.3.0 : Open file in project without using mouse

c++ - 重新编译 MFC 扩展 DLL 更改导出函数的入口点

C++:动态共享库中的虚函数产生段错误

windows - 在哪里可以找到 Windows(XP、Vista、7 等)上可用的默认 dll 文件列表?