public int GetNewStorage(out UnsafeNativeMethods.IStorage storage) { Debug.WriteLineIf(RichTextDbg.TraceVerbose, "IRichTextBoxOleCallback::GetNewStorage"); UnsafeNativeMethods.ILockBytes pLockBytes = UnsafeNativeMethods.CreateILockBytesOnHGlobal(NativeMethods.NullHandleRef, true); Debug.Assert(pLockBytes != null, "pLockBytes is NULL!"); storage = UnsafeNativeMethods.StgCreateDocfileOnILockBytes(pLockBytes, NativeMethods.STGM_SHARE_EXCLUSIVE | NativeMethods.STGM_CREATE | NativeMethods.STGM_READWRITE, 0); Debug.Assert(storage != null, "storage is NULL!"); return(NativeMethods.S_OK); }
/// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> private void CreateStorage() { Debug.Assert(storage == null, "but we already have a storage!!!"); IntPtr hglobal = IntPtr.Zero; if (buffer != null) { hglobal = UnsafeNativeMethods.GlobalAlloc(NativeMethods.GMEM_MOVEABLE, length); IntPtr pointer = UnsafeNativeMethods.GlobalLock(new HandleRef(null, hglobal)); try { if (pointer != IntPtr.Zero) { Marshal.Copy(buffer, 0, pointer, length); } } finally { UnsafeNativeMethods.GlobalUnlock(new HandleRef(null, hglobal)); } } bool failed = false; try { iLockBytes = UnsafeNativeMethods.CreateILockBytesOnHGlobal(new HandleRef(null, hglobal), true); if (buffer == null) { storage = UnsafeNativeMethods.StgCreateDocfileOnILockBytes(iLockBytes, NativeMethods.STGM_CREATE | NativeMethods.STGM_READWRITE | NativeMethods.STGM_SHARE_EXCLUSIVE, 0); } else { storage = UnsafeNativeMethods.StgOpenStorageOnILockBytes(iLockBytes, null, NativeMethods.STGM_READWRITE | NativeMethods.STGM_SHARE_EXCLUSIVE, 0, 0); } } catch (Exception t) { Debug.Fail(t.ToString()); failed = true; } if (failed) { if (iLockBytes == null && hglobal != IntPtr.Zero) { UnsafeNativeMethods.GlobalFree(new HandleRef(null, hglobal)); } else { iLockBytes = null; } storage = null; } }