Marshal.UnsafeAddrOfPinnedArrayElement on CF

  • Thread starter Thread starter Jorge Castillo
  • Start date Start date
J

Jorge Castillo

Hello.
Does anybody knows a way to get similar results on CompactFramework

byte[] buffer;
int bufferOffset = 0;
GCHandle bufferHandle;
IntPtr bufferPtr;
..
..
..
bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, bufferOffset);

8<----------8<----------8<----------8<----------8<----------
I need to get bufferPtr to use in a C dll (void * buffer)

Thanks in advance,
Jorge Castillo
 
bufferPtr = (IntPtr)((int)bufferHandle.AddrOfPinnedObject() + bufferOffset
+ 4);

Note: there's a bug in CF V1 implementation of AddrOfPinnedObject().
It returns a pointer to the internal array structure (consists of 32 bit
array size followed by array data), not to the array itself.
Add 4 to compensate.

Another solution would be to use pointers in unsafe code.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
Back
Top