简体   繁体   中英

Unexpected calling convention for PInvoke

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM