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

时间:2018-12-28 09:07:46

标签: c++

我使用Windows 7,当我构建一个C ++ win32控制台项目以执行并运行它时,发生错误: enter image description here

该程序用于打开测试引擎并从蓝牙芯片(CSR3026)读取一些数据。

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

int main()
{
    int iHandle = 0;
    int iTimeout = 0;
    typedef int (*tepsread)(int handle, int keyId, int valueLen, int *value, int  *readLen);
    typedef int (*opentestengine)(int transport, char* transportDevice, int dataRate, int retryTimeOut, int usbTimeOut);
    typedef int (*closetestengine)(int handle);
    char data[50] = {0};
    int v;
    int r;

    HINSTANCE hDLL = LoadLibrary(L"TestEngine.dll");
    opentestengine openTestEngine = (opentestengine)GetProcAddress(hDLL, "openTestEngine");
    closetestengine closeTestEngine = (closetestengine)GetProcAddress(hDLL, "closeTestEngine");
    do
    {
      cout << "Trying to connect..." << endl;
      iHandle = openTestEngine(2, "COM5", 115200, 1000, 0);
      iTimeout += 1000;
    } while(iHandle == 0 && iTimeout < 5000);

    if(iHandle != 0)
    {
      cout << "Connected!" << endl;

      // Perform all your testing here
      tepsread pFun = (tepsread)GetProcAddress(hDLL, "tePsRead");
      cout << pFun(iHandle, 10191, 32, &v, &r) << endl;

      closeTestEngine(iHandle);
    }

    FreeLibrary(hDLL);
    cout << v << endl;

    return v;
}

1通过串行端口5打开测试引擎以与芯片通信。

2调用testengine.dll中的tePsRead函数以从芯片读取10191中的值。

我已更改为__cdecl调用约定,但仍然存在相同的错误: enter image description here

1 个答案:

答案 0 :(得分:1)

错误消息指出,您错误地强制转换了函数以更改其签名后才调用函数

因此,至少 openTestEngine,closeTestEngine tePsRead 函数之一的签名不是您所假定的

所以

  • tePsRead 不是int tePsRead(int, int, int, int *, int *);
  • 和/或 openTestEngine 不是int openTestEngine(int, char*, int, int, int);
  • 和/或 closeTestEngine 不是int closeTestEngine(int);