C# 检测到PinVokesTack不平衡错误
我需要将以下代码从.c移植到c# 我尝试了以下代码:(使用编译的c代码的DLL) 辩护:C# 检测到PinVokesTack不平衡错误,c#,C#,我需要将以下代码从.c移植到c# 我尝试了以下代码:(使用编译的c代码的DLL) 辩护: public delegate void MyCallback(String str, int a); [DllImport(LibName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)] public static extern void SetCallbackError(MyCallback myCall
public delegate void MyCallback(String str, int a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern void SetCallbackError(MyCallback myCallback);
public void PrintCallbackError(String str, int a)
{
Console.WriteLine("issue was detected");
}
Public void CheckConnection()
{
SetCallbackError(PrintCallbackError); -causes 1st exception
OpenConnection(false); - causes 2nd exception
…
}
public delegate void MyCallback();
public static void PrintCallBackError()
第一个例外:
第二个例外:
为避免第二个异常而提出的一个解决方案是更改签名,使其完全不获取任何参数-这没有多大意义-但有效…
辩护:
public delegate void MyCallback(String str, int a);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern void SetCallbackError(MyCallback myCallback);
public void PrintCallbackError(String str, int a)
{
Console.WriteLine("issue was detected");
}
Public void CheckConnection()
{
SetCallbackError(PrintCallbackError); -causes 1st exception
OpenConnection(false); - causes 2nd exception
…
}
public delegate void MyCallback();
public static void PrintCallBackError()
重要的是,我不能改变c代码的实现。
如果有任何帮助和其他编码解决方案,我将不胜感激
感谢advance–使用CharSet.Ansi代替unicode-本机参数为char*。使用Marshal.GetFunctionPointerForDelegate将托管方法作为本机回调传递。
CallingConvention=CallingConvention.Cdecl
WINAPI定义为什么?“Alex Farber”:1。我在哪里调用:Marshal::GetFunctionPointerForDelegate?2.更改为ANSI并没有帮助PInvoke异常:(3)“ta.speot.is”我不知道WINAPI是什么…你认为它相关吗?是的,它相关。它意味着调用Convention.Stdcall。所有WINAPI函数都是Stdcall。