Unexpected calling convention for PInvoke

250 Views Asked by At

I have a library in dll and its header file, I don't have source for it. I need to use pinvoke to call this unmanaged code from C#, but have problem in setting calling convention. The header file look like:

#ifdef EVOLIB_EXPORTS
#define EVOLIB_API __declspec(dllexport)
#else
#define EVOLIB_API __declspec(dllimport)
#endif
extern EVOLIB_API int ConvertRVBtoK(char *FileNameIn, char *FileNameOut,int ColorSmooth,int BlackMode);

I think ConvertRVBtoK calling convention must be __cdecl because that is the default c/c++ calling convention. But when I check the decorated name ("?ConvertRVBtoK@@YGHPAEJJJ0E@Z") with the undname.exe utility, the result shows __stdcall as the calling convention. Why? Is there a conflict between the dll file and header file?

1

There are 1 best solutions below

2
On BEST ANSWER

The header file does not specify the calling convention at all. So now you depend on the compiler default. It is configured in the MSVC++ IDE with Project > Properties > C/C++ > Advanced > "Calling Convention" setting. Default is /Gd as you'd expect, seeing it changed to /Gz is not terribly unusual as a Q&D fix.

Be careful changing it, you might break other programs that depend on stdcall in their interop code. Note that you previously asked about the pascal convention, that is stdcall in 32-bit code. Being explicit in the header file so you don't depend on the compiler default won't hurt either.

If you don't have source code then you there is no way to change it. Nothing particularly wrong with stdcall, it is the expected interop default on Windows.