PInvoking C ++ DLL时AccessViolationException(cdecl调用约定的问题?) [英] AccessViolationException when PInvoking C++ DLL (cdecl calling convention problem?)

查看:307
本文介绍了PInvoking C ++ DLL时AccessViolationException(cdecl调用约定的问题?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一整天研究这个,我仍然不明白:

I've spent all day researching this, and I'm none the wiser:

我有PInvokes在C ++ DLL的方法的C#DLL。我有没有问题,在调试模式下编译时这样做,但在Release模式编译时,我得到一个AccessViolationException。谷歌搜索这个问题告诉我,这可能是与调用不遵医嘱约定的问题。现在的代码看起来像这样在C#中:

I have a C# DLL which PInvokes a method in a C++ DLL. I have had no problems doing this when compiling in Debug mode, but when compiling in Release mode I get an AccessViolationException. Googling this problem tells me that it's probably an issue with incompliant calling conventions. The code now looks like this in C#:

[return: MarshalAs(UnmanagedType.U1)]
[DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern Boolean AMethod(Int32 mode, byte frame);

和C ++中:

extern "C" {
     DLL_EXPORT bool AMethod(int mode, BYTE frame)
     {
      ...
     }
}

我已经设置了C ++项目编译__cdecl调用VS2010中约定,但我仍然得到AccessViolationException我不知道还有什么我能做的。我要指出,我的C ++ DLL使用第三方的DLL,我不知道什么叫他们使用的约定。

I've set the C++ project to compile with the __cdecl calling convention in VS2010, but I still get the AccessViolationException and I have no idea what more I can do. I should note that my C++ DLL uses third-party DLLs, and I do not know what calling convention they use.

任何帮助,将不胜感激!

Any help would be appreciated!

哦,我没有得到我的异常开发计算机上,只有我的目标系统。

Oh, and I don't get the exception on my development machine, only on my target system.

推荐答案

尝试用这个重新排序声明:结果

Try with this reordering of statements :


[DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.U1)]
internal static extern Boolean AMethod(Int32 mode, byte frame);

这篇关于PInvoking C ++ DLL时AccessViolationException(cdecl调用约定的问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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