我試圖調用,將其包裝成一個DLL下面的C++函數:如何在C#中編組一個字節數組?
unsigned char * rectifyImage(unsigned char *pimg, int rows, int cols)
我的import語句如下所示:
[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文件中......任何人有任何想法?
你可能想看看這個問題:http://stackoverflow.com/questions/289076/how-can-i-pass-a-pointer-to-an-array-using- p-invoke-in-c – Bobby 2010-08-06 18:11:39
來自'rectifyImage'的返回值是否應該被釋放,如果是,怎麼辦? – 2010-08-06 18:25:57
rectifyImage的返回值用於創建C#位圖對象,然後釋放它。我還沒有試圖弄清楚如何實際上釋放它。 – Tim 2010-08-06 18:29:06