Je tente d'envoyer un pointeur vers un pointeur d'un tableau UInt16 à une fonction marshalé comme tant en C#:Comment puis-je passer un pointeur vers un pointeur d'un tableau UInt16 à une fonction Marshalled?
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);
Mon principal problème est avec le Marshal.Copy
, pour le premier argument qui est le tableau source, il ne prend pas un UInt16[]
. Je me demandais si quelqu'un savait comment utiliser Marshal.Copy
avec un tableau UInt16
.
System.Buffer.BlockCopy (bufferArray, 0, temp, 0, temp.Length * 2]; Devrait avoir un) à la fin pas], mais autrement fonctionne très bien merci! – user2134127