PInvokeStackImbalance C# 调用非托管 C++ 函数 [英] PInvokeStackImbalance C# call to unmanaged C++ function

查看:35
本文介绍了PInvokeStackImbalance C# 调用非托管 C++ 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到 VS2010 后,托管调试助手显示有关从 C# 应用程序调用非托管 C++ 函数导致堆栈不平衡的错误.

After switching to VS2010, the managed debug assistant is displaying an error about an unbalanced stack from a call to an unmanaged C++ function from a C# application.

通常的嫌疑人似乎不是导致问题的原因.还有什么我应该检查的吗?VS2008 构建的 C++ dll 和 C# 应用程序从来没有出现过问题,没有奇怪或神秘的错误 - 是的,我知道这没有多大意义.

The usuals suspects don't seem to be causing the issue. Is there something else I should check? The VS2008 built C++ dll and C# application never had a problem, no weird or mysterious bugs - yeah, I know that doesn't mean much.

以下是检查的内容:

  • dll 名称正确.
  • 入口点名称是正确的,并且已经使用depends.exe 进行了验证 - 代码必须使用损坏的名称,并且确实如此.
  • 调用约定是正确的.
  • 尺寸和类型似乎都正确.
  • 字符集正确.
  • 忽略错误后似乎没有任何问题,在调试器之外运行时也没有问题.

C#:

[DllImport("Correct.dll", EntryPoint = "SuperSpecialOpenFileFunc", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern short SuperSpecialOpenFileFunc(ref SuperSpecialStruct stuff);

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct SuperSpecialStruct
{
   public int field1;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
   public string field2;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
   public string field3;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
   public string field4;
   public ushort field5;
   public ushort field6;
   public ushort field7;
   public short field8;
   public short field9;
   public uint field10;
   public short field11;
};

C++:

short SuperSpecialOpenFileFunc(SuperSpecialStruct * stuff);

struct SuperSpecialStruct
{
   int               field1;
   char              field2[256];
   char              field3[20];
   char              field4[10];
   unsigned short    field5;
   unsigned short    field6;
   unsigned short    field7;
   short             field8;
   short             field9;
   unsigned int      field10;
   short             field11;
};

这里是错误:

托管调试助手'PInvokeStackImbalance' 检测到一个托管应用程序路径"中的问题.

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'Managed application path'.

附加信息:致电调用函数'SuperSpecialOpenFileFunc' 有堆栈不平衡.这很可能因为托管 PInvoke 签名与非托管目标不匹配签名.检查调用约定和参数PInvoke 签名匹配目标非托管签名.

Additional Information: A call to PInvoke function 'SuperSpecialOpenFileFunc' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

推荐答案

Dane Rose 的评论,您可以在 C++ 函数上使用 __stdcall 或在您的 CallingConvention = CallingConvention.Cdecl 声明>DllImport.

As mentioned in Dane Rose's comment, you can either use __stdcall on your C++ function or declare CallingConvention = CallingConvention.Cdecl on your DllImport.

这篇关于PInvokeStackImbalance C# 调用非托管 C++ 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆