public static SafeGlobalAllocHandle CopyHGlobal(SafeGlobalAllocHandle data) { IntPtr src = UnsafeNativeMethods.GlobalLock(data); UIntPtr size = UnsafeNativeMethods.GlobalSize(data); SafeGlobalAllocHandle ptr = UnsafeNativeMethods.GlobalAlloc(0, size); IntPtr buffer = UnsafeNativeMethods.GlobalLock(ptr); try { UnsafeNativeMethods.MoveMemory(buffer, src, size); } finally { if (buffer != IntPtr.Zero) { UnsafeNativeMethods.GlobalUnlock(ptr); } if (src != IntPtr.Zero) { UnsafeNativeMethods.GlobalUnlock(data); } } return(ptr); }