public static void SaveObjectToStream(object obj, Stream stm) { IStreamImpl istm = new IStreamImpl(stm); IPersistStream ps = obj as IPersistStream; if (ps != null) { ps.Save(istm, false); } else { IPersistStreamInit psi = (IPersistStreamInit)obj; psi.Save(istm, false); } }
public static void LoadObjectFromStream(object obj, Stream stm) { IStreamImpl istm = new IStreamImpl(stm); IPersistStream ps = obj as IPersistStream; if (ps != null) { ps.Load(istm); } else { IPersistStreamInit psi = (IPersistStreamInit)obj; psi.InitNew(); psi.Load(istm); } }
private void btnCreateWrite_Click(object sender, EventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "All Files (*.*)|*.*"; this.DialogResult = dlg.ShowDialog(); if (this.DialogResult == DialogResult.OK) { try { Stream = new IStreamImpl(dlg.FileName, System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Close(); }
private void btnCreateRead_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Filter = "All Files (*.*)|*.*"; dlg.ShowReadOnly = false; this.DialogResult = dlg.ShowDialog(); if (this.DialogResult == DialogResult.OK) { try { Stream = new IStreamImpl(dlg.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } Close(); }
private void menuObjectFromMarshalledStream_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Filter = "All Files (*.*)|*.*"; if (dlg.ShowDialog(this) == DialogResult.OK) { try { byte[] data = File.ReadAllBytes(dlg.FileName); IStreamImpl stm = new IStreamImpl(new MemoryStream(data)); Guid iid = COMInterfaceEntry.IID_IUnknown; IntPtr pv; int hr = COMUtilities.CoUnmarshalInterface(stm, ref iid, out pv); if (hr == 0) { object comObj = Marshal.GetObjectForIUnknown(pv); Marshal.Release(pv); OpenObjectInformation(comObj, "Marshalled Object"); } else { Marshal.ThrowExceptionForHR(hr); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }