检测到PInvokeStackUnbalance [英] PInvokeStackUnbalance was detected

查看:84
本文介绍了检测到PInvokeStackUnbalance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用第三方本机dll.在我的.net应用程序中.我正在使用c#.本机dll中的功能.具有以下声明:

Hi
I am trying to use a third party native dll. in my .net application. I am using c#. The function in native dll. has the following declaration:

void _stdcall pir_learnCCF( const char * serial, unsigned char state );



使用PInvoke Interop助手,我可以进行以下导入:



With the PInvoke Interop assistant I get the following import:

[DllImport("PIRLIB.dll", EntryPoint = "pir_learnCCF")]
public static extern void pir_learnCCF(
[InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string serial, 
byte state);



我有以下问题.使用PInvoke互操作助手时,我必须从非托管函数定义中删除"_stdcall",否则助手会生成错误.在删除"_stdcall"之后,助手会生成上述使用语句,但是当我尝试调用该函数时,会检测到PInvokeStackUnbalance.第三方dll.具有其他功能,这些功能通常可以与Interop Assistant生成的导入语句一起使用.任何建议将不胜感激.

Uroš



I have the following questions. When using PInvoke interop assistant I have to remove the "_stdcall" from unmanaged function definition, otherwise the assistant generates an error. After I remove the "_stdcall" the assistant generates the above using sentence but when I try to call the function I get the PInvokeStackUnbalance detected. The third party dll. has other functions which work normally with import sentences generated by the Interop Assistant. Any advice will be appreciated.

Uroš

推荐答案

首先,参数正确.您甚至不需要为字符串使用MarshalAs,因为这种封送处理是默认的. (顺便说一句,在属性应用程序的位置,允许并建议从"MarshalAsAttribute"之类的名称中省略"Attribute".)

我可能看到的堆栈不平衡的唯一原因是调用约定.在这种情况下,它可以是C(第一个调用约定的候选者).因此,请首先尝试:

First, the parameters are correct. You don''t even need to use MarshalAs for string, as this marshaling is the default. (By the way, it is allowed and recommended to omit "Attribute" from names like "MarshalAsAttribute" at the place of attribute application.)

The only reason of unbalance stack I could possibly see is calling convention. In this case, it could be C (first calling conventions candidate). So, try this first:

DllImport("PIRLIB.dll", EntryPoint = "pir_learnCCF", CallingConvention = CallingConvention.Cdecl)]
public static extern void pir_learnCCF(string serial, byte state);



参见:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.callingconvention.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.interopservices.callingconvention.aspx [ ^ ].

—SA



See:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.callingconvention.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx[^].

—SA


这篇关于检测到PInvokeStackUnbalance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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