PInvoking C++ DLL时出现AccessViolationException(cdecl调用约定问题?)

PNi*_*sen 7 c# c++ pinvoke cdecl access-violation

我花了一整天研究这个,而且我不是更聪明的:

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

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

在C++中:

extern "C" {
     DLL_EXPORT bool AMethod(int mode, BYTE frame)
     {
      ...
     }
}
Run Code Online (Sandbox Code Playgroud)

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

任何帮助,将不胜感激!

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

lsa*_*mon 0

尝试对语句进行重新排序:

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

  • 你的回答引起了我的兴趣。重新排序方法的属性将如何解决提问者的问题? (2认同)