How to get IntPtr from byte[] in C#

netseng picture netseng · Feb 11, 2009 · Viewed 161.8k times · Source

I want to pass a byte[] to a method takes a IntPtr Parameter in C#, is that possible and how?

Answer

user65157 picture user65157 · Feb 11, 2009

Another way,

GCHandle pinnedArray = GCHandle.Alloc(byteArray, GCHandleType.Pinned);
IntPtr pointer = pinnedArray.AddrOfPinnedObject();
// Do your stuff...
pinnedArray.Free();