Question : Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.

Hi there - I am getting the following error although my calling conventions on both my exe and my dll  is _cdecl.  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.

exe code:
HINSTANCE dllHandle;

typedef short (CALLBACK* iRDSToBLGType)(char *,char *,bool,char *);
iRDSToBLGType iRDSToBLGPtr;

void CrdstooutcallerDlg::OnBnClickedButton3()
{
      dllHandle = ::LoadLibrary("RDStoBLG.dll");
      iRDSToBLGPtr = (iRDSToBLGType)GetProcAddress(dllHandle,"iRDSToBLG");

      char m_rds[128000];
      memset(m_rds,0,sizeof(m_rds));
      char m_blgOut[128000];
      memset(m_blgOut,0,sizeof(m_blgOut));

      FILE* lFile = fopen("d:\\generalwork\\blg2\\rdsstring.txt","r+t");
      if(lFile)
      {
            fgets(m_rds,128000,lFile);
            fclose(lFile);
      }
      

      iRDSToBLGPtr(NULL,m_rds,false,m_blgOut);

      ::FreeLibrary(dllHandle);
}

dll code:

int  DLL_DECL _cdecl iRDSToBLG(char *pRetroDate, char *RDSData, bool bIDS, char *OutputData)
{
.
.
.
}

Answer : Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.

>> while both the exe and dll that is written in C++ are compiled with the _cdecl flag.
But when you use the function you are using it with the CALLBACK specifier, which is stdcall not cdecl. This is resulting in a stack-frame mis-match between the caller and the callee, hence the error about the stack pointer.
Random Solutions  
 
programming4us programming4us