PDA

Click to See Complete Forum and Search --> : dll calling problems


john tindell
Jan 5th, 2003, 01:30 PM
ive created a DLL and im trying to call it but it keeps giving me these errors.


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.


heres my code

#include <windows.h>

typedef void (*MSGBOX)(char*,int);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HINSTANCE hDLL;
MSGBOX msgProc;
hDLL = LoadLibrary("dlltest.dll");
if(hDLL!=NULL)
{
msgProc = (MSGBOX)GetProcAddress(hDLL,"msgbox");
if(msgProc!=NULL)
(*msgProc)("Batman its worked!", MB_OK);

FreeLibrary(hDLL);
}
return 0;
}


cheers :)

CornedBee
Jan 5th, 2003, 02:11 PM
I guess the function in the DLL looks like
void WINAPI msgbox(char * str, int i)
{
// ...
}

right?
Could be APIENTRY, CALLBACK or __stdcall instead of WINAPI, whatever.

john tindell
Jan 5th, 2003, 02:15 PM
in the dll its,

#include <windows.h>

void __stdcall msgbox(char * cMsg, int i){
MessageBox(NULL,cMsg,i);
}


the function runs, its just that afterwards it comes up with the error.

thnx :p

CornedBee
Jan 5th, 2003, 02:22 PM
typedef void (__stdcall *MSGBOX)(char*,int);

As the error message said, the problem was that the function in the DLL is a __stdcall, while the function pointer (which has no convention keyword) is assumed to be __cdecl. These two calling conventions are incompatible. The solution is to explicitly declare the function pointer to point to a __stdcall function, see above.

CornedBee
Jan 5th, 2003, 02:22 PM
BTW
MessageBox(NULL,cMsg,i);
Does this work? The WinAPI MessageBox takes 4 parameters. :confused:

john tindell
Jan 5th, 2003, 02:30 PM
MessageBox(NULL,cMsg,i); doesn't work, i just forgot to put the title in.
Cheers for the help :)