コード例 #1
0
        public static ComStorage CreateStorageOnStream(Stream stream, ComStorage.OpenMode openMode)
        {
            Util.ThrowOnNullArgument(stream, "stream");
            ComStorage.CheckOpenMode(openMode, "openMode", ComStorage.validCreateModes);
            ComStorage comStorage = null;

            Interop.IStorage storage = null;
            try
            {
                LockBytesOnStream lockBytes = new LockBytesOnStream(stream);
                int num = Interop.StgCreateDocfileOnILockBytes(lockBytes, (uint)openMode, 0, out storage);
                if (num != 0)
                {
                    throw new MsgStorageException(MsgStorageErrorCode.CreateStorageOnStreamFailed, MsgStorageStrings.FailedCreateStorage("ILockBytes"), num);
                }
                comStorage = new ComStorage(storage);
            }
            finally
            {
                if (comStorage == null && storage != null)
                {
                    Marshal.ReleaseComObject(storage);
                }
            }
            return(comStorage);
        }
コード例 #2
0
        public static ComStorage CreateFileStorage(string filename, ComStorage.OpenMode openMode)
        {
            Util.ThrowOnNullArgument(filename, "filename");
            ComStorage.CheckOpenMode(openMode, "openMode", ComStorage.validCreateModes);
            object     obj = null;
            ComStorage result;

            try
            {
                Guid iidistorage = Interop.IIDIStorage;
                int  num         = Interop.StgCreateStorageEx(filename, (uint)openMode, 0, 0U, IntPtr.Zero, IntPtr.Zero, ref iidistorage, out obj);
                if (num != 0)
                {
                    throw new MsgStorageException(MsgStorageErrorCode.CreateFileFailed, MsgStorageStrings.FailedCreateStorage(filename), num);
                }
                Interop.IStorage storage = obj as Interop.IStorage;
                if (storage == null)
                {
                    throw new MsgStorageException(MsgStorageErrorCode.CreateFileFailed, MsgStorageStrings.FailedCreateStorage(filename));
                }
                obj    = null;
                result = new ComStorage(storage);
            }
            finally
            {
                if (obj != null)
                {
                    Marshal.ReleaseComObject(obj);
                }
            }
            return(result);
        }