您的位置:首页 > 编程语言 > C#

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

2010-12-27 14:58 691 查看
在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;
   }

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  byte c# c buffer