The value of ESP was not properly saved across a function call
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: The value of ESP was not properly saved across a function call

Hybrid View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,262

    The value of ESP was not properly saved across a function call

    I'm working with a cross-platform library which defines a function to obtain function addresses from a shared object (i.e. a DLL on Windows). Here's my modified version of the function which works (albeit only on Windows of course):-

    Code:
    typedef void (*SuilVoidFunc)(void);
    
    /** dlsym wrapper to return a function pointer */
    static inline SuilVoidFunc
    suil_dlfunc(void* handle, const char* symbol)
    {
    	return (SuilVoidFunc)GetProcAddress((HMODULE)handle, symbol);
    }
    Now, here's the original (cross-platform) version which is giving me a run time error on Windows:-

    Code:
    typedef void (*SuilVoidFunc)(void);
    #define dlsym GetProcAddress
    
    /** dlsym wrapper to return a function pointer */
    static inline SuilVoidFunc
    suil_dlfunc(void* handle, const char* symbol)
    {
    	typedef SuilVoidFunc (*VoidFuncGetter)(void*, const char*);
    	VoidFuncGetter dlfunc = (VoidFuncGetter)dlsym;
    	return dlfunc(handle, symbol);
    }
    That original version fails at the final return line. The error message says "The value of ESP was not properly saved across a function call".

    I'm assuming there's a problem with the declaration of VoidFuncGetter (i.e. it'll assume that the caling convention for GetProcAddress() is cdecl when in fact, it's stdcall). What's the most elegant way to fix this and still keep cross-platform compatibility?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,262

    Re: The value of ESP was not properly saved across a function call

    Looking at some other cross platform libraries, this seems to be the most common strategy and seems as good a solution as any:-

    Code:
    static inline SuilVoidFunc
    suil_dlfunc(void* handle, const char* symbol)
    {
    #ifdef WIN32
    	return (SuilVoidFunc)GetProcAddress((HMODULE)handle, symbol);
    #else
    	return (SuilVoidFunc)dlsym(handle, symbol);
    #endif
    }
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center