帮助-C#C ++问题 [英] Help - C# C++ question

查看:73
本文介绍了帮助-C#C ++问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个具有以下功能的C ++ dll-

Hi,

I have a C++ dll with the following function -

_declspec(dllexport) double _stdcall Spread (double high, double low)
{ return high - low;  }



当我运行dumpbin时,我看到它在那里-



When I run dumpbin I see that it is there -

2 0001103C Spread = @ILT+55(?Spread@@YANNN@Z)



我将C#导入定义为-



And I define my C# import as -

[DllImport(@"C:\MyDll.dll", EntryPoint="Spread")]
        static extern double Spread(double high, double low);




但是,如下所示的调用失败-




However calls to it as below fail -

MessageBox.Show(Spread(2.3, 3.3).ToString());



错误消息是-

对PInvoke函数``myapp!myapp.Form1 :: Spread''的调用已使堆栈不平衡.这可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.



The error message is -

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

What could be wrong with this?

推荐答案

由于您无法在C ++ dll中更改原型,因此我假设您没有编译该dll,实际上并不知道它是如何编译的.

因此,您怎么知道该函数实际上是使用__stdcall编译的?

仅仅因为您有一个标头文件,这并不意味着该标头文件就是dll编译时所用的标头文件.

不平衡"消息几乎意味着您已经混合使用__stdcall和__cdecl或类似的东西.在一种情况下,调用方应该清理堆栈,而在另一种情况下,被调用方应该清理堆栈.如果将它们混合在一起,则最终呼叫者或被呼叫者都不会清理堆栈,或者两者都这样做-在任何一种情况下,您都会收到不平衡的消息.

参见:

http://msdn.microsoft.com/en-us/library/984x0h58 (v = VS.90).aspx [
Since you can''t change the prototype in the C++ dll, then I''m assuming you didn''t compile that dll and don''t actually know how it was compiled.

So how do you know that the function was actually compiled with __stdcall ??

Just because you have a header file that says that, doesn''t mean that that header file was the one that the dll was compiled with.

The "unbalanced" message pretty much means that you''ve mixed __stdcall and __cdecl or something like that. In one case the caller is supposed to clean up the stack, in the other the callee is supposed to. If you mix them up you end up with either neither caller or callee cleaning up the stack or both of them doing it -- in either case you''d get the unbalanced message.

See:

http://msdn.microsoft.com/en-us/library/984x0h58(v=VS.90).aspx[^]

If __stdcall isn''t working, then the most likely scenario is that the C++ dll was actually compiled with __cdecl semantics.

Try that first, if it doesn''t work try the other calling conventions listed. It really is only going to be the calling convention attribute that is a problem, so find the one it was really compiled with via trial and error (or stop in the debugger and examine the object code and see what stack clean up the callee is actually doing).


您的DllImport属性需要设置更多属性.我不知道哪个(或它们的值应该是什么),但我确定您可以在google上找到答案.
Your DllImport attribute needs more properties to be set. I don''t know which ones (or what their values should be), but I''m sure you can find the answer on google.


请在您的原型上尝试一下:

Try this on your prototype:

static extern "C" int _cdecl Spread(double high, double low);


这篇关于帮助-C#C ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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