internal static byte[] ConvertHGlobalToByteArray(SafeHGlobalHandle hGlobal) { // this has to be Int32, even on 64 bit machines since Marshal.Copy takes a 32 bit integer Int32 sizeOfByteArray = (SafeNativeMethods.GlobalSize(hGlobal)).ToInt32(); if (sizeOfByteArray > 0) { byte[] byteArray = new Byte[sizeOfByteArray]; IntPtr pBuff = SafeNativeMethods.GlobalLock(hGlobal); if (IntPtr.Zero == pBuff) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new OutOfMemoryException()); } try { Marshal.Copy(pBuff, byteArray, 0, sizeOfByteArray); } finally { SafeNativeMethods.GlobalUnlock(hGlobal); } return(byteArray); } return(null); }
internal static void LoadIntoObjectFromByteArray(IPersistStream persistableObject, Byte[] byteStream) { SafeHGlobalHandle hGlobal = SafeHGlobalHandle.AllocHGlobal(byteStream.Length); IntPtr pBuff = SafeNativeMethods.GlobalLock(hGlobal); if (IntPtr.Zero == pBuff) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new OutOfMemoryException()); } try { Marshal.Copy(byteStream, 0, pBuff, byteStream.Length); IStream stream = SafeNativeMethods.CreateStreamOnHGlobal(hGlobal, false); try { persistableObject.Load(stream); } finally { Marshal.ReleaseComObject(stream); } } finally { SafeNativeMethods.GlobalUnlock(hGlobal); } }
public static SafeHandle GetTokenInformation(SafeCloseHandle token, TOKEN_INFORMATION_CLASS infoClass) { uint length; if (!SafeNativeMethods.GetTokenInformation(token, infoClass, SafeHGlobalHandle.InvalidHandle, 0, out length)) { int error = Marshal.GetLastWin32Error(); if (error != (int)Win32Error.ERROR_INSUFFICIENT_BUFFER) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error, SR.GetString(SR.GetTokenInfoFailed, error))); } } SafeHandle buffer = SafeHGlobalHandle.AllocHGlobal(length); try { if (!SafeNativeMethods.GetTokenInformation(token, infoClass, buffer, length, out length)) { int error = Marshal.GetLastWin32Error(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error, SR.GetString(SR.GetTokenInfoFailed, error))); } } catch { buffer.Dispose(); throw; } return(buffer); }
internal static Byte[] PersistIPersistStreamToByteArray(IPersistStream persistableObject) { IStream stream = SafeNativeMethods.CreateStreamOnHGlobal(SafeHGlobalHandle.InvalidHandle, false); try { persistableObject.Save(stream, true); SafeHGlobalHandle hGlobal = SafeNativeMethods.GetHGlobalFromStream(stream); if (null == hGlobal || IntPtr.Zero == hGlobal.DangerousGetHandle()) { throw Fx.AssertAndThrow("HGlobal returned from GetHGlobalFromStream is NULL"); } return(ConvertHGlobalToByteArray(hGlobal)); } finally { Marshal.ReleaseComObject(stream); } }
internal static extern IntPtr GlobalSize(SafeHGlobalHandle hGlobal);
internal static extern bool GlobalUnlock(SafeHGlobalHandle hGlobal);
internal static extern IntPtr GlobalLock(SafeHGlobalHandle hGlobal);
public static extern IStream CreateStreamOnHGlobal( [In] SafeHGlobalHandle hGlobal, [In, MarshalAs(UnmanagedType.Bool)] bool fDeleteOnRelease);
internal static extern IntPtr GlobalSize(SafeHGlobalHandle hGlobal);
internal static extern bool GlobalUnlock(SafeHGlobalHandle hGlobal);
internal static extern IntPtr GlobalLock(SafeHGlobalHandle hGlobal);