C++ Run-Time Check Failure #0 – The value of ESP was not properly saved across a function call

cdll

I am trying to program motorbee using c++

when I run the code I get the following error:

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.

This is my code.

#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);

}

Best Answer

Your typedef function pointer needs to match the calling convention of the library you are using. For example, if InitMotoBee uses cdecl your typedef will look like:

typedef bool (__cdecl *Type_InitMotoBee)(void)

The SetMotors function takes parameters so the calling convention will need to be set correctly for that as well (that is likely where the application is failing).

Related Topic