private void CreateStorage() { Debug.Assert(storage == null, "but we already have a storage!!!"); IntPtr hglobal = IntPtr.Zero; if (buffer != null) { hglobal = Kernel32.GlobalAlloc(Kernel32.GMEM.MOVEABLE, (uint)length); IntPtr pointer = Kernel32.GlobalLock(hglobal); try { if (pointer != IntPtr.Zero) { Marshal.Copy(buffer, 0, pointer, length); } } finally { Kernel32.GlobalUnlock(hglobal); } } try { iLockBytes = Ole32.CreateILockBytesOnHGlobal(hglobal, BOOL.TRUE); if (buffer == null) { storage = Ole32.StgCreateDocfileOnILockBytes( iLockBytes, Ole32.STGM.CREATE | Ole32.STGM.READWRITE | Ole32.STGM.SHARE_EXCLUSIVE, 0); } else { storage = Ole32.StgOpenStorageOnILockBytes( iLockBytes, null, Ole32.STGM.READWRITE | Ole32.STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0); } } catch (Exception) { if (iLockBytes == null && hglobal != IntPtr.Zero) { Kernel32.GlobalFree(hglobal); } else { iLockBytes = null; } storage = null; } }
public HRESULT GetNewStorage(out Ole32.IStorage storage) { Debug.WriteLineIf(RichTextDbg.TraceVerbose, "IRichTextBoxOleCallback::GetNewStorage"); Ole32.ILockBytes pLockBytes = Ole32.CreateILockBytesOnHGlobal(IntPtr.Zero, BOOL.TRUE); Debug.Assert(pLockBytes != null, "pLockBytes is NULL!"); storage = Ole32.StgCreateDocfileOnILockBytes( pLockBytes, Ole32.STGM.SHARE_EXCLUSIVE | Ole32.STGM.CREATE | Ole32.STGM.READWRITE, 0); Debug.Assert(storage != null, "storage is NULL!"); return(HRESULT.S_OK); }