Ejemplo n.º 1
0
 internal static extern int StgOpenStorageEx(
     string pwcsName,
     [MarshalAs(UnmanagedType.U4)] NativeMethods.STGM grfMode,
     [MarshalAs(UnmanagedType.U4)] NativeMethods.STGFMT stgfmt,
     [MarshalAs(UnmanagedType.U4)] int grfAttrs,
     IntPtr pStgOptions,
     IntPtr reserved2,
     ref Guid riid,
     out NativeMethods.IStorage ppObjectOpen);
Ejemplo n.º 2
0
        private static void ExtractSubStorage(string databaseFile, string storageName, string extractFile)
        {
            IStorage storage;

            NativeMethods.STGM openMode = NativeMethods.STGM.READ | NativeMethods.STGM.SHARE_DENY_WRITE;
            int hr = NativeMethods.StgOpenStorage(databaseFile, IntPtr.Zero, (uint)openMode, IntPtr.Zero, 0, out storage);

            if (hr != 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            try
            {
                openMode = NativeMethods.STGM.READ | NativeMethods.STGM.SHARE_EXCLUSIVE;
                IStorage subStorage = storage.OpenStorage(storageName, IntPtr.Zero, (uint)openMode, IntPtr.Zero, 0);

                try
                {
                    IStorage newStorage;
                    openMode = NativeMethods.STGM.CREATE | NativeMethods.STGM.READWRITE | NativeMethods.STGM.SHARE_EXCLUSIVE;
                    hr       = NativeMethods.StgCreateDocfile(extractFile, (uint)openMode, 0, out newStorage);
                    if (hr != 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    try
                    {
                        subStorage.CopyTo(0, IntPtr.Zero, IntPtr.Zero, newStorage);

                        newStorage.Commit(0);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(newStorage);
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(subStorage);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(storage);
            }
        }