【发布时间】:2012-07-27 18:52:00
【问题描述】:
这个真的开始让我头疼了:(
我有一个非托管的 DLL,我正在尝试与之互操作,但运行不顺利。应用程序有时会正常工作......但大多数时候,随机通过 AccessViolationException 并可怕地崩溃。
我想我已经把它缩小到我对单个 DllImport 的错误处理:
C++ 函数:
HTMLRENDERERDLL_REDIST_API void SetDataBuffer( int windowHandle, unsigned char* dataSource, int format, int stride, int totalBufferSize );
C# DllImport:
[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 是罪魁祸首,但……不知道如何证明!
谢谢
【问题讨论】:
-
如果你有MyDLL的源码,你可以通过将调试器模式设置为“混合”来调试崩溃
-
很遗憾我没有
-
调用api时是否发生访问冲突,还是使用marshaled指针?
-
当我调用 API 时。还有另一个函数——“更新”——被重复调用,并且在执行的 1-30 帧内的某个时间点,该函数将抛出。它没有参数,所以不会出错...
-
错字:可能只是为了显示用法,但您创建
var buffer = ...但您使用:w.buffer