C#用byte[]字节数组获取C中byte*指针

在C中,byte*对应C#中Sbyte,所以没有直接转换的机制,目前开发过程中发现采用System.Runtime.InteropServices.Marshal还是一个不错的选择,,详情请参考如下代码:

接口调用文件中,用句柄获取C中byte*,

[DllImport("MKL_Camera.dll", EntryPoint = "MKL_CaptureBuffer")
public static extern IntPtr MKL_CaptureBuffer(capture_type captureType,ref int size);

将内存中的buffer保存到byte[]中。

IntPtr buffer = camera.MKL_CaptureBuffer(camera.capture_type.TYPE_CAPTURE_JPG, ref size);
byte[] byte1 = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(buffer, byte1, 0, size);

记得要释放buffer

if (buffer != IntPtr.Zero)
   {
     camera.MKL_ReleaseBuffer(buffer);
     buffer = IntPtr.Zero;
   }

 

 

你可能感兴趣的:(C#用byte[]字节数组获取C中byte*指针)