views:

287

answers:

0

I am receiving an IntPtr and an int specifying the number of bytes it points to. The data can contain any characters including null, EOL, etc. When trying the following, the buffer is corrupted:

//buffer is the IntPtr  
//count is the number of bytes in 'buffer'

byte[] test = new byte[count];  
Marshal.Copy(buffer, test, 0, count);

IntPtr ptr = IntPtr.Zero;  
ptr = Marshal.AllocCoTaskMem(count);  
Marshal.Copy(test, 0, ptr, count);

I would assume 'buffer' and 'ptr' would be pointing to the same buffer blob in different memory locations but they don't (I am just copying the same data to another mem location AFAIK). However, 'ptr' seems to point to an arbitrary memory location as it contains string references to DLL modules.

Any ideas? Thanks!