Search code examples
c#c++dlldllimportcalling-convention

Calling convention for an unmanaged DLL in C#


I created a DLL in C++ and loaded in C# with [DllImport].

  1. Is __cdecl default in C++ when exporting without calling convention?

  2. Is it okay to use CallingConvention.StdCall or CallingConvention.Winapi with the function __cdecl? (It worked in my test, and I don't use 'Variable Arguments')


Solution

  • Is __cdecl default in C++ when exporting without calling convention?

    Yes.

    Is it okay to use CallingConvention.StdCall or CallingConvention.Winapi with the function __cdecl?

    No. Use CallingConvention.Cdecl. Using the wrong calling convention might appear to work for some functions, but it is wrong, and at some point you will be caught out.