0

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?

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

1 Answers1

2

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • so ,you means anyone,who wrote this dll and header file, Sets Default CallingConvention to __stdcall and then builds the project. and he/she does not mention calling convention in header file and assumed users of dll MUST use undname.exe for interop with library? I think That Is very very Bad Practice. – Alireza Khalesi Aug 13 '18 at 11:08
  • It is indeed not a good practice. But this DLL *might* have been explicitly designed to be used for interop and not intended for a C or C++ client program. And then there is the Q&D fix, the most likely shortcut that the author/build engineer took. Some languages don't support anything other than stdcall well, VBA is a notable example. – Hans Passant Aug 13 '18 at 11:13