C# byte[]與byte*(IntPtr)內存copy

轉爲byte*(IntPtr)是爲了與一些C\C++的dll交互。

需要如下設置, 允許不安全代碼


類或方法前加上 unsafe如

public abstract unsafe class IAudioDecoder {}

1.byte* 轉 byte[]

public int (byte* write_data, int data_len)
{
    byte[] write = new byte[data_len];
    Marshal.Copy((IntPtr)write_data, write, 0, write.Length);
}

2.byte[] 轉 byte*

public void (byte[] write)
{
    IntPtr write_data = Marshal.AllocHGlobal(write.Length);    
    Marshal.Copy(write, 0, write_data, write.Length);
    Marshal.FreeHGlobal(write_data );
}

3.byte* 複製到 byte*

[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
        public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
CopyMemory((IntPtr)(m_buffer_in + m_buffer_in_pos), (IntPtr)out_data, (uint)out_len);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章