Results 1 to 6 of 6

Thread: dll calling problems

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    dll calling problems

    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
    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    in the dll its,
    Code:
    #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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    BTW
    MessageBox(NULL,cMsg,i);
    Does this work? The WinAPI MessageBox takes 4 parameters.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    MessageBox(NULL,cMsg,i); doesn't work, i just forgot to put the title in.
    Cheers for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width