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

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

问题描述

我花了一整天的时间研究这个,但我一点也不聪明:

我有一个 C# DLL,它 PInvokes 一个 C++ DLL 中的方法.在调试模式下编译时,我没有遇到任何问题,但是在发布模式下编译时,我得到一个 AccessViolationException.谷歌搜索这个问题告诉我,这可能是不符合调用约定的问题.代码现在在 C# 中看起来像这样:

[返回:MarshalAs(UnmanagedType.U1)][DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)]内部静态外部布尔 AMethod(Int32 模式,字节帧);

在 C++ 中:

extern "C" {DLL_EXPORT bool AMethod(int 模式,BYTE 帧){...}}

我已在 VS2010 中将 C++ 项目设置为使用 __cdecl 调用约定进行编译,但我仍然收到 AccessViolationException 并且我不知道还能做什么.我应该注意到我的 C++ DLL 使用第三方 DLL,我不知道他们使用什么调用约定.

任何帮助将不胜感激!

哦,我的开发机器上没有异常,只有在我的目标系统上.

解决方案

尝试重新排列语句:

<前>[DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)][返回:MarshalAs(UnmanagedType.U1)]内部静态外部布尔 AMethod(Int32 模式,字节帧);

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

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

and in C++:

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

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天全站免登陆