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

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

问题描述

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

该usuals嫌疑人似乎并不造成问题。有没有别的东西我应该检查? VS2008的建成C ++ DLL和C#应用程序从未有过的问题,没有任何怪异或神秘的错误 - 是啊,我知道这并不意味着什么。

下面是被检查的事情:


  • DLL的名称是正确的。

  • 的入口点名称是正确的,并已被证实与Depends.exe分析 - 在code的使用重整名称和它

  • 调用约定是正确的。

  • 的尺寸和类型都似乎是正确的。

  • 的字符集是正确的。

  • 似乎没有被忽略错误后的任何问题,没有问题的调试器外运行时。

C#:

 函数[DllImport(Correct.dll,入口点=SuperSpecialOpenFileFunc,CallingConvention = CallingConvention.StdCall,字符集= CharSet.Ansi,ExactSpelling = TRUE)]
公共静态外部短SuperSpecialOpenFileFunc(REF SuperSpecialStruct东西);[StructLayout(LayoutKind.Sequential,包= 1,字符集= CharSet.Ansi)
公共结构SuperSpecialStruct
{
   公众诠释FIELD1;
   [的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 256)]
   公共字符串域2;
   [的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 20)]
   公共字符串FIELD3;
   [的MarshalAs(UnmanagedType.ByValTStr,SizeConst = 10)]
   公共字符串字段4;
   公共USHORT字段5;
   公共USHORT字段6;
   公共USHORT字段7;
   公共短栏位8;
   公共短字段9;
   公共UINT字段10;
   公共短fi​​eld11;
};

C ++

 短SuperSpecialOpenFileFunc(SuperSpecialStruct *的东西);结构SuperSpecialStruct
{
   诠释FIELD1;
   焦域2 [256];
   焦炭FIELD3 [20];
   CHAR字段4 [10];
   无符号短字段5;
   无符号短栏位6;
   无符号短字段7;
   短栏位8;
   短字段9;
   无符号整型字段10;
   短field11;
};

以下是错误:


  

托管调试助手
  'PInvokeStackImbalance'已检测到一个
  在管理应用程序路径的问题。


  
  

附加信息:一个调用
  PInvoke的功能
  SuperSpecialOpenFileFunc'有
  不平衡堆栈。这很可能是
  因为托管的PInvoke签名
  不匹配的非托管目​​标
  签名。检查主叫
  会议及参数
  的PInvoke签名与目标
  非托管签名。



解决方案

作为<一提到href=\"http://stackoverflow.com/questions/2390407/pinvokestackimbalance-c-sharp-call-to-unmanaged-c-function/2738125#comment2825285_2738125\">Dane罗斯的评论,您可以使用 __ STDCALL 你的C ++函数或声明 CallingConvention = CallingConvention.Cdecl 上你的的DllImport

这是解决我的问题的答案。

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.

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.

Here are the things that were checked:

  • The dll name is correct.
  • The entry point name is correct and has been verified with depends.exe - the code has to use the mangled name and it does.
  • The calling convention is correct.
  • The sizes and types all seem to be correct.
  • The character set is correct.
  • There doesn't seem to be any issues after ignoring the error and there isn't an issue when running outside the debugger.

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;
};

Here is the error:

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

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.

解决方案

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

This is the answer that solves my problem.

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

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