我正在尝试将指向 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
。