예제 #1
0
        internal static void LoadIntoObjectFromByteArray(IPersistStream persistableObject, byte[] byteStream)
        {
            SafeHGlobalHandle hGlobal     = SafeHGlobalHandle.AllocHGlobal(byteStream.Length);
            IntPtr            destination = SafeNativeMethods.GlobalLock(hGlobal);

            if (IntPtr.Zero == destination)
            {
                throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new OutOfMemoryException());
            }
            try
            {
                Marshal.Copy(byteStream, 0, destination, byteStream.Length);
                IStream pStm = SafeNativeMethods.CreateStreamOnHGlobal(hGlobal, false);
                try
                {
                    persistableObject.Load(pStm);
                }
                finally
                {
                    Marshal.ReleaseComObject(pStm);
                }
            }
            finally
            {
                SafeNativeMethods.GlobalUnlock(hGlobal);
            }
        }
        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);
            }
        }
예제 #3
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);
        }