【发布时间】:2013-07-19 12:54:32
【问题描述】:
我正在尝试将一个指向 UInt16 数组指针的指针发送到一个编组函数,就像在 C# 中这样:
C++:
int foo(Unsigned_16_Type** Buffer_Pointer);
C#:
[DllImport("example.dll")]
public static extern int foo(IntPtr Buffer_Pointer);
UInt16[] bufferArray = new UInt16[32];
IntPtr p_Buffer = (IntPtr)Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(UInt16)) * bufferArray.Length);
Marshal.Copy(bufferArray, 0, p_Buffer, bufferArray.Length); //Issue is here
GCHandle handle = GCHandle.Alloc(p_Buffer, GCHandleType.Pinned);
IntPtr ppUnmanagedBuffer = (IntPtr)handle.AddrOfPinnedObject();
UInt16 word_count = 0;
this.lstbox_DATA_WORDS.Items.Clear();
if ( foo(ppUnmanagedBuffer );
我的主要问题是Marshal.Copy,对于作为源数组的第一个参数,它不需要UInt16[]。我想知道是否有人知道如何将Marshal.Copy 与UInt16 数组一起使用。
【问题讨论】:
标签: c# arrays pointers marshalling uint16