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);
            }
        }
Ejemplo n.º 2
0
        internal static byte[] PersistIPersistStreamToByteArray(IPersistStream persistableObject)
        {
            byte[]  buffer;
            IStream pStm = SafeNativeMethods.CreateStreamOnHGlobal(SafeHGlobalHandle.InvalidHandle, false);

            try
            {
                persistableObject.Save(pStm, true);
                SafeHGlobalHandle hGlobalFromStream = SafeNativeMethods.GetHGlobalFromStream(pStm);
                if ((hGlobalFromStream == null) || (IntPtr.Zero == hGlobalFromStream.DangerousGetHandle()))
                {
                    throw Fx.AssertAndThrow("HGlobal returned from  GetHGlobalFromStream is NULL");
                }
                buffer = ConvertHGlobalToByteArray(hGlobalFromStream);
            }
            finally
            {
                Marshal.ReleaseComObject(pStm);
            }
            return(buffer);
        }