为C#制作C ++ DLL

我做了一个非常简单的Dll:

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

然后我想在我的C#app中使用它:

 class Program { [DllImport("TestLib.dll")] public static extern int Try(int v); static void Main(string[] args) { Console.WriteLine("Wynik: " + Try(20)); Console.ReadLine(); } } 

它一直工作,直到我试图pas参数。 现在我在运行时遇到以下错误:

调用PInvoke函数’ConsoleApplication2!ConsoleApplication1.Program :: Try’使堆栈失去平衡。 这很可能是因为托管的PInvoke签名与非托管目标签名不匹配。 检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

我不知道问题出在哪里。

您收到的错误消息确实包含了一个很好的建议:

检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

您应该在两端都指定相同的调用约定(C ++ dll和C#程序集)。 在C ++中,您可以通过使用__cdecl,__ stdcall等之一预先添加函数声明来指定它。

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

在C#端你使用DllImport属性指定它,默认的是CallingConvention.StdCall,它对应于C ++中的__stdcall,因此,看起来你在C ++端有一个__cdecl。 要解决此问题,请在DLL中使用__stdcall,如上所示,或者在C#中使用CDecl,如下所示:

class Program { [DllImport("TestLib.dll", CallingConvention=CallingConvention.Cdecl )] public static extern int Try(int v); static void Main(string[] args) { Console.WriteLine("Wynik: " + Try(20)); Console.ReadLine(); } }
class Program { [DllImport("TestLib.dll", CallingConvention=CallingConvention.Cdecl )] public static extern int Try(int v); static void Main(string[] args) { Console.WriteLine("Wynik: " + Try(20)); Console.ReadLine(); } } 

C和C ++中的默认调用约定是__cdecl ; .NET P / Invoke使用的默认调用约定是__stdcall – 您需要协调这两个。

  • 正如汉斯建议的那样,要么使你的原生函数__stdcall

     __declspec(dllexport) int __stdcall Try(int v) 
  • 或者使用__cdecl管理P / Invoke签名:

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