如何在 C# 中编组字节数组?
我正在尝试调用包含在 DLL 中的以下 C++ 函数:
unsigned char * rectifyImage(unsigned char *pimg, int rows, int cols)
我的导入语句如下所示:
[DllImport("mex_rectify_image.dll")]
unsafe public static extern IntPtr rectifyImage(
byte[] data, int rows, int columns);
我的调用例程如下所示:
byte[] imageData = new byte[img.Height * img.Width * 3];
// ... populate imageData
IntPtr rectifiedImagePtr = rectifyImage(imageData, img.Height, img.Width);
Byte[] rectifiedImage = new Byte[img.Width * img.Height * 3];
Marshal.Copy(rectifiedImagePtr, rectifiedImage, 0, 3 * img.Width * img.Height);
但是,我不断收到运行时错误:
类型 < 的第一次机会异常xxx.dll 中发生代码>System.AccessViolationException 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
我只是想知道问题是否出在我整理数据的方式或导入的 DLL 文件中......有人有什么想法吗?
I'm trying to call the following C++ function that's wrapped up into a DLL:
unsigned char * rectifyImage(unsigned char *pimg, int rows, int cols)
My import statement looks like the following:
[DllImport("mex_rectify_image.dll")]
unsafe public static extern IntPtr rectifyImage(
byte[] data, int rows, int columns);
And my call routine looks like the following:
byte[] imageData = new byte[img.Height * img.Width * 3];
// ... populate imageData
IntPtr rectifiedImagePtr = rectifyImage(imageData, img.Height, img.Width);
Byte[] rectifiedImage = new Byte[img.Width * img.Height * 3];
Marshal.Copy(rectifiedImagePtr, rectifiedImage, 0, 3 * img.Width * img.Height);
However, I keep getting a runtime error:
A first chance exception of type System.AccessViolationException
occurred in xxx.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I'm just wondering if the fault lies in the way I'm marshaling my data or in my imported DLL file... anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发生这种情况的原因可能是该方法的调用约定与编组器猜测的不同。您可以在 DllImport 属性中指定约定。
您不需要在此处的 C# 声明中使用“不安全”关键字,因为它不是“不安全”代码。也许您曾在某一时刻尝试使用“固定”指针,但忘记在发布之前删除不安全的关键字?
This is likely occurring because the calling convention of the method is not as the marshaller is guessing it to be. You can specify the convention in the DllImport attribute.
You don't need the 'unsafe' keyword in the C# declaration here as it's not 'unsafe' code. Perhaps you were trying it with a 'fixed' pointer at one point and forgot to remove the unsafe keyword before posting?
不确定这是否是您的问题,但通常 C++ 指针映射到 IntPtr。所以尝试将您的导入语句修改为:
not sure if this is your problem, but in general C++ pointers map to IntPtr. so try modifying your import statement to this:
unsigned char *pimg
无法自动编组为byte[]
。为了传递
byte[] imageData
,您需要手动将其编组到IntPtr
并将所述IntPtr
传递给rectifyImage
>。这是实现此类封送的推荐模式:
unsigned char *pimg
can't be marshalled tobyte[]
automatically.In order to pass the
byte[] imageData
you need to marshal it manually toIntPtr
and pass saidIntPtr
torectifyImage
.This is the recommended pattern for implementing such marshaling:
rectifyImage 正在寻找块中第一个字节的标记,以获取您在块中发送的数据。尝试图像数据[0]
rectifyImage Is looking for a ponter to a the 1st byte in a block for data you are sending in the block. Try imageData[0]