Making C++ DLL for C#

前端 未结 2 2014
[愿得一人]
[愿得一人] 2021-01-21 03:24

I have made a very simple Dll like this:

extern \"C\"
{
  __declspec(dllexport) int Try(int v)
  {
    return 10 + v;
  }
}

Then I want to use

2条回答
  •  情歌与酒
    2021-01-21 04:18

    The default calling convention in C and C++ is __cdecl; the default calling convention used by .NET P/Invoke is __stdcall -- you need to reconcile these two.

    • Either make your native function __stdcall, as Hans suggested:

      __declspec(dllexport) int __stdcall Try(int v)
      
    • Or make your managed P/Invoke signature use __cdecl:

      [DllImport("TestLib.dll", CallingConvention = CallingConvention.Cdecl)]
      public static extern int Try(int v);
      

提交回复
热议问题