I’m learning to use dynamic DLL. I have created 2 functions i DLL library:
DWORD fn1(VOID);
DWORD fn2(WCHAR*);
and exported it using def file
EXPORTS
fn1
fn2
When I load&use fn1, everything is ok, but the second is causing problem 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.
pointers are defined like:
typedef DWORD (WINAPI *fn1)(void);
typedef DWORD (WINAPI *fn2)( WCHAR* );
and loaded like this:
fn1 first = NULL;
fn2 second = NULL;
first = (fn1) GetProcAddress( dll, "fn1" );
second = (fn2) GetProcAddress( dll, "fn2" );
Can you help me, what could cause the problem – when I “continue” application works fine…
Clearly the declaration of the function pointer type is not correct. It doesn’t match in your snippet either, you declared them WINAPI. Which is a macro that sets their calling convention to __stdcall. You however didn’t declare the actual functions with the same attribute. The default is __cdecl.
You got away with it for
fn1because it doesn’t have any arguments so getting the calling convention wrong doesn’t imbalance the stack. It does forfn2. The generated code doesn’t pop the passed argument off the stack after the function call as required by __cdecl.Remove WINAPI to fix.