public static IntPtr GetIntPtrFromData(Object data) {
Byte[] buf = null;
if (data is MemoryStream) {
buf = new Byte[4];
if (4 != ((MemoryStream)data).Read(buf, 0, 4))
throw new ArgumentException("Could not read an IntPtr from the MemoryStream");
}
if (data is Byte[]) {
buf = (Byte[])data;
if (buf.Length < 4)
throw new ArgumentException("Could not read an IntPtr from the byte array");
}
if (buf == null)
throw new ArgumentException("Could not read an IntPtr from the " + data.GetType().ToString());
Int32 p = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
return new IntPtr(p);
}