1

I get this error when trying to use this function

 void WSPAPI GetLspGuid( LPGUID lpGuid )
 {
    memcpy( lpGuid, &gProviderGuid, sizeof( GUID ) );
 }

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

the function is called by using

HMODULE         hMod = NULL;
LPFN_GETLSPGUID fnGetLspGuid = NULL;
int             retval = SOCKET_ERROR;

// Load teh library
hMod = LoadLibraryA( LspPath );
if ( NULL == hMod )
{
    fprintf( stderr, "RetrieveLspGuid: LoadLibraryA failed: %d\n", GetLastError() );
    goto cleanup;
}

// Get a pointer to the LSPs GetLspGuid function
fnGetLspGuid = (LPFN_GETLSPGUID) GetProcAddress( hMod, "GetLspGuid" );
if ( NULL == fnGetLspGuid )
{
    fprintf( stderr, "RetrieveLspGuid: GetProcAddress failed: %d\n", GetLastError() );
    goto cleanup;
}

// Retrieve the LSPs GUID
fnGetLspGuid( Guid );
Anwar Mohamed
  • 625
  • 2
  • 13
  • 27
  • possible duplicate of [Weird MSC 8.0 error: "The value of ESP was not properly saved across a function call..."](http://stackoverflow.com/questions/142644/weird-msc-8-0-error-the-value-of-esp-was-not-properly-saved-across-a-function). [This answer](http://stackoverflow.com/a/1332874/902497) discusses the reasons. – Raymond Chen Jun 04 '13 at 00:49
  • You did not include the definition of `LPFN_GETLSPGUID`. The runtime is telling you that `LPFN_GETLSPGUID` does not match `GetLspGuid`. – Raymond Chen Jun 04 '13 at 01:05
  • @RaymondChen it is defined as typedef void (*LPFN_GETLSPGUID) (GUID *lpGuid); – Anwar Mohamed Jun 04 '13 at 01:08
  • Notice that the typedef does not match the actual function. That's why you're getting the error. – Raymond Chen Jun 04 '13 at 06:05

1 Answers1

3

This runtime check guards against a mismatch between the function declaration and the actual definition. Accidents that can happen when you compile code into a static library or a DLL. Common mismatches are the calling convention or the number or type of the arguments that are passed.

The shoe fits, you've got a macro named WSPAPI that declares the calling convention. It typically expands to either __cdecl or __stdcall, usually biased towards __stdcall. So very high odds that it this macro has the wrong value in your client code. Ask the library author for assistance if you can't figure out how to set this macro correctly.


After edit: with the additional failure mode that you are loading the wrong version of the DLL. And that your LPFN_GETLSPGUID function pointer declaration is wrong, missing the WSPAPI macro. I'll put my money on that one, especially since I can't see it.


After comment, the info is slowly trickling in:

it is defined as typedef void (*LPFN_GETLSPGUID) (GUID *lpGuid);

Which is wrong, it should be

typedef void (WSPAPI * LPFN_GETLSPGUID)(GUID *lpGuid);

If you don't have the macro available, unlikely, then substitute WSPAPI with __stdcall.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536