private byte[] SerializeToBlob() { System.Runtime.InteropServices.ComTypes.STATSTG sTATSTG; byte[] numArray = null; IStream stream = null; IntPtr zero = IntPtr.Zero; try { stream = IWbemClassObjectFreeThreaded.CreateStreamOnHGlobal(IntPtr.Zero, 1); IWbemClassObjectFreeThreaded.CoMarshalInterface(stream, ref IWbemClassObjectFreeThreaded.IID_IWbemClassObject, this.pWbemClassObject, 2, IntPtr.Zero, 2); stream.Stat(out sTATSTG, 0); numArray = new byte[sTATSTG.cbSize]; zero = IWbemClassObjectFreeThreaded.GlobalLock(IWbemClassObjectFreeThreaded.GetHGlobalFromStream(stream)); Marshal.Copy(zero, numArray, 0, (int)sTATSTG.cbSize); } finally { if (zero != IntPtr.Zero) { IWbemClassObjectFreeThreaded.GlobalUnlock(zero); } if (stream != null) { Marshal.ReleaseComObject(stream); } } GC.KeepAlive(this); return(numArray); }
private void DeserializeFromBlob(byte[] rg) { IntPtr zero = IntPtr.Zero; IStream stream = null; try { this.pWbemClassObject = IntPtr.Zero; zero = Marshal.AllocHGlobal((int)rg.Length); Marshal.Copy(rg, 0, zero, (int)rg.Length); stream = IWbemClassObjectFreeThreaded.CreateStreamOnHGlobal(zero, 0); this.pWbemClassObject = IWbemClassObjectFreeThreaded.CoUnmarshalInterface(stream, ref IWbemClassObjectFreeThreaded.IID_IWbemClassObject); } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } if (zero != IntPtr.Zero) { Marshal.FreeHGlobal(zero); } } }