这个真的开始让我头疼:(
我有一个非托管 DLL,我正在尝试与之互操作,但运行不顺利。该应用程序有时会工作......但大多数时候,随机通过 AccessViolationException 并可怕地崩溃。
我想我已经把它缩小到我对单个 DllImport 的错误处理:
C++ 函数:
HTMLRENDERERDLL_REDIST_API void SetDataBuffer( int windowHandle, unsigned char* dataSource, int format, int stride, int totalBufferSize );
C# DLL 导入:
[DllImport("MyDll.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
static private extern unsafe void SetDataBuffer(Int32 windowHandle, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] dataSource, Int32 format, Int32 stride, Int32 totalBufferSize);
调用上述函数:
var buffer = new byte[windowWidth * windowHeight * bytesPerPixel];
SetDataBuffer(windowHandle, buffer, (Int32)0, (Int32)(windowWidth * bytesPerPixel), (Int32)(windowWidth * windowHeight * bytesPerPixel));
这有什么明显的问题吗?我怀疑这dataSource
是罪魁祸首,但......不知道如何证明!
谢谢