public void Write(string OutputFileName) { IOleStorage OleStorage = null; StgCreateDocfile(OutputFileName, STGM.Direct | STGM.Create | STGM.Write | STGM.ShareExclusive, 0, out OleStorage); try { foreach (KeyValuePair <string, byte[]> Section in Sections) { IOleStream OleStream = null; OleStorage.CreateStream(Section.Key, STGM.Write | STGM.ShareExclusive, 0, 0, out OleStream); try { uint Written; OleStream.Write(Section.Value, (uint)Section.Value.Length, out Written); OleStream.Commit(STGC.Overwrite); } finally { Marshal.ReleaseComObject(OleStream); } } } finally { Marshal.ReleaseComObject(OleStorage); } }
public static VCSolutionOptions Read(string InputFileName) { IOleStorage Storage = null; StgOpenStorage(InputFileName, null, STGM.Direct | STGM.Read | STGM.ShareExclusive, IntPtr.Zero, 0, out Storage); try { IOleEnumSTATSTG Enumerator = null; Storage.EnumElements(0, IntPtr.Zero, 0, out Enumerator); try { uint Fetched; STATSTG[] Stats = new STATSTG[200]; Enumerator.Next((uint)Stats.Length, Stats, out Fetched); VCSolutionOptions Options = new VCSolutionOptions(); foreach (STATSTG Stat in Stats) { if (Stat.pwcsName == "SolutionConfiguration") { IOleStream OleStream; Storage.OpenStream(Stat.pwcsName, IntPtr.Zero, STGM.Read | STGM.ShareExclusive, 0, out OleStream); try { uint SizeRead; byte[] Buffer = new byte[Stat.cbSize]; OleStream.Read(Buffer, (uint)Stat.cbSize, out SizeRead); using (MemoryStream InputStream = new MemoryStream(Buffer, false)) { BinaryReader Reader = new BinaryReader(InputStream, Encoding.Unicode); while (InputStream.Position < InputStream.Length) { Options.SolutionConfiguration.Add(VCBinarySetting.Read(Reader)); } } } finally { Marshal.ReleaseComObject(OleStream); } } } return(Options); } finally { Marshal.ReleaseComObject(Enumerator); } } finally { Marshal.ReleaseComObject(Storage); } }
public VCOleContainer(string InputFileName) { IOleStorage Storage = null; StgOpenStorage(InputFileName, null, STGM.Direct | STGM.Read | STGM.ShareExclusive, IntPtr.Zero, 0, out Storage); try { IOleEnumSTATSTG Enumerator = null; Storage.EnumElements(0, IntPtr.Zero, 0, out Enumerator); try { uint Fetched; STATSTG[] Stats = new STATSTG[200]; Enumerator.Next((uint)Stats.Length, Stats, out Fetched); for (uint Idx = 0; Idx < Fetched; Idx++) { IOleStream OleStream; Storage.OpenStream(Stats[Idx].pwcsName, IntPtr.Zero, STGM.Read | STGM.ShareExclusive, 0, out OleStream); try { uint SizeRead; byte[] Buffer = new byte[Stats[Idx].cbSize]; OleStream.Read(Buffer, (uint)Stats[Idx].cbSize, out SizeRead); Sections.Add(new KeyValuePair <string, byte[]>(Stats[Idx].pwcsName, Buffer)); } finally { Marshal.ReleaseComObject(OleStream); } } } finally { Marshal.ReleaseComObject(Enumerator); } } finally { Marshal.ReleaseComObject(Storage); } }
public void Write(string OutputFileName) { IOleStorage OleStorage = null; StgCreateDocfile(OutputFileName, STGM.Direct | STGM.Create | STGM.Write | STGM.ShareExclusive, 0, out OleStorage); try { if (SolutionConfiguration.Count > 0) { using (MemoryStream OutputStream = new MemoryStream()) { // Write all the settings to the memory stream BinaryWriter Writer = new BinaryWriter(OutputStream, Encoding.Unicode); foreach (VCBinarySetting Setting in SolutionConfiguration) { Setting.Write(Writer); } // Create a named stream in the compound document and write the binary data out IOleStream OleStream = null; OleStorage.CreateStream("SolutionConfiguration", STGM.Write | STGM.ShareExclusive, 0, 0, out OleStream); try { uint Written; OleStream.Write(OutputStream.GetBuffer(), (uint)OutputStream.Length, out Written); OleStream.Commit(STGC.Overwrite); } finally { Marshal.ReleaseComObject(OleStream); } } } } finally { Marshal.ReleaseComObject(OleStorage); } }
static extern int StgOpenStorage([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, IOleStorage pstgPriority, STGM grfMode, IntPtr snbExclude, uint reserved, out IOleStorage ppstgOpen);
static extern int StgCreateDocfile([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, STGM grfMode, uint reserved, out IOleStorage ppstgOpen);
static extern int StgCreateDocfile([MarshalAs(UnmanagedType.LPWStr)]string pwcsName, STGM grfMode, uint reserved, out IOleStorage ppstgOpen);