calling conventions for dll exported functions

Tue, Apr 28, 2009

What a blast from the past. That’s the trouble with going back to something you haven’t done in years, in this case COM programming and exporting functions from DLLs in particular. You hit problems you solved a long time ago and have to dredge up the answer again. I want to keep the COM stuff out of the way so I’ve bunged it in a DLL and exported a function:

extern “C” GADFLY_API bool CreateUser(LPSTR, LPSTR);
where GADFLY_API is defined as:
#define GADFLY_API __declspec(dllexport)
extern “C” is required to stop the compiler name mangling the C++ functions in the DLL. Next, I declared the imported function in the test app using a standard function pointer:
typedef bool (CALLBACK* LPFNDLLFUNCCREATEUSER)(LPSTR, LPSTR);
ran the test and got 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.
so I checked what CALLBACK was defined as in WinDef.h:
#define CALLBACK    stdcall
but of course I’m exporting using extern “C” which means I need to change the calling convention on the function pointer:
typedef bool (cdecl* LPFNDLLFUNCCREATEUSER)(LPSTR, LPSTR);
and it works fine now giving the old grey matter a good workout along the way!

comments powered by Disqus