不平衡堆栈 [英] Unbalanced Stack!

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

问题描述

我已经写了一个VC ++ dll。 dll中的一种方法的声明如下:

  externC_declspec(dllexport)
void startIt(int number)
{
capture = cvCaptureFromCAM(number);
}

我使用这个dll在C#代码使用P / Invoke。我作出以下声明:

  [DllImport(Tracking.dll,EntryPoint =startIt)] 
public extern static void startIt(int number);

我将代码中的函数调用为:

  startIt(0); 

现在,当遇到这一行时,编译器会抛出这个错误:

 调用PInvoke函数UsingTracking!UsingTracking.Form1 :: startIt使
不平衡堆栈。这可能是因为管理的PInvoke签名的
与非托管目标签名不匹配。检查调用约定
和PInvoke签名的参数是否匹配目标非托管签名。

我不明白为什么在托管和非托管代码中的签名相同时,它会抛出此错误。此外,在我的另一台机器中,相同的代码在视觉工作室中完美运行。所以,这让我觉得错误的引导错误。



请帮助。



感谢

解决方案

当您调用外部函数时,调用约定使用默认值为 __ STDCALL 。由于您的功能使用 __cdecl 约定,您需要声明为:

  [DllImport( Tracking.dll,EntryPoint =startIt,
CallingConvention = CallingConvention.Cdecl)]
public extern static void startIt(int number);


I have written a VC++ dll. The declaration for one of the methods in the dll is as follows:

extern "C" _declspec(dllexport)
void startIt(int number)
{
     capture = cvCaptureFromCAM(number);
}

I use this dll in a C# code using P/Invoke. I make the declaration as:

[DllImport("Tracking.dll", EntryPoint = "startIt")]
        public extern static void startIt(int number);

and I call the function in the code as:

startIt(0);

Now, when this line is encountered, the compiler is throwing me this error:

A call to PInvoke function 'UsingTracking!UsingTracking.Form1::startIt' 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.

I cannot understand why is it throwing this error as the signature in both managed and unmanaged code are the same. Moreover, in my another machine, the same code is running perfectly in visual studio. So, this makes me think that the error thrown is mis leading.

Please help.

Thanks

解决方案

When you p/invoke an external function, the calling convention used defaults to __stdcall. Since your function uses the __cdecl convention, you need to declare it as such:

[DllImport("Tracking.dll", EntryPoint = "startIt",
    CallingConvention = CallingConvention.Cdecl)]
public extern static void startIt(int number);

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

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