Calling conventions
suggest changeSeveral calling conventions specify who pops arguments from the stack (caller or callee), how arguments are passed, and in what order. C++ uses Cdecl by default, but DllImport defaults to StdCall on Windows, which is what the Windows API uses. Match one side to the other:
- Change calling convention to
StdCallin C++:
extern "C" __declspec(dllexport) int __stdcall add(int a, int b)
[DllImport("myDLL.dll")]
- Or, change calling convention to
Cdeclin C#:
extern "C" __declspec(dllexport) int /*__cdecl*/ add(int a, int b)
[DllImport("myDLL.dll", CallingConvention = CallingConvention.Cdecl)]
If you want to use a function with Cdecl calling convention and a mangled name, your code will look like this:
__declspec(dllexport) int add(int a, int b)
[DllImport("myDLL.dll", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?add@@YAHHH@Z")]
thiscall(__thiscall) is mainly used for C++ member functions.- When a function uses
thiscall, a pointer to the object is passed as a hidden parameter. C# instance methods do not use this convention.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents