c# - Marshal.Copy, copying an array of IntPtr into an IntPtr -
i can't figure out how copy(intptr[], int32, intptr, int32)
method works. though copy data contained in multiple intptrs single intptr (as msdn states) apparently doesn't work expected:
intptr[] ptrarray = new intptr[] { marshal.allochglobal(1), marshal.allochglobal(2) }; marshal.writebyte(ptrarray[0], 0, 0xc1); // allocate total size. intptr ptr = marshal.allochglobal(3); marshal.copy(ptrarray, 0, ptr, ptrarray.length); // expect read 0xc1 value random!! byte value = marshal.readbyte(ptr, 0);
does know if i'm using method not purpose?
static void main(string[] args) { intptr[] ptrarray = new intptr[] { marshal.allochglobal(1), marshal.allochglobal(2) }; marshal.writebyte(ptrarray[0], 0, 100); int size = marshal.sizeof(typeof(intptr)) * ptrarray.length; intptr ptr = marshal.allochglobal(size); marshal.copy(ptrarray, 0, ptr, ptrarray.length); // have native pointer ptr, points 2 pointers, // each of thme points own memory (size 1 , 2). // let's read first intptr ptr: intptr p = marshal.readintptr(ptr); // let's read byte p: byte b = marshal.readbyte(p); console.writeline((int)b); // prints 100 // do: release intptr }
read explanations in comments.
Comments
Post a Comment