private ManagedToNativeWrapper(IStream managedInterface) { IStreamInterface newInterface; lock (managedVtable) { // Vtable may have been disposed when shutting down if (vtableRefCount == 0 && comVtable == IntPtr.Zero) { CreateVtable(); } vtableRefCount++; } try { this.managedInterface = managedInterface; gcHandle = GCHandle.Alloc(this); newInterface = new IStreamInterface(); newInterface.lpVtbl = comVtable; newInterface.gcHandle = (IntPtr)gcHandle; comInterface = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamInterface))); Marshal.StructureToPtr(newInterface, comInterface, false); } catch { this.Dispose(); throw; } }
internal static void ReleaseInterface(IStream managedInterface) { if (managedInterface is NativeToManagedWrapper) { ((NativeToManagedWrapper)managedInterface).Dispose(true); } }
public void Clone(out IStream ppstm) { IntPtr newInterface; ThrowExceptionForHR(managedVtable.Clone(comInterface, out newInterface)); ppstm = NativeToManagedWrapper.GetInterface(newInterface, true); }
internal static IntPtr GetUnderlyingInterface(IStream managedInterface) { if (managedInterface is NativeToManagedWrapper) { NativeToManagedWrapper wrapper = (NativeToManagedWrapper)managedInterface; wrapper.managedVtable.AddRef(wrapper.comInterface); return(wrapper.comInterface); } else { return(IntPtr.Zero); } }
internal static IntPtr GetInterface(IStream managedInterface) { IntPtr comInterface; if (managedInterface == null) { return(IntPtr.Zero); } #if !RECURSIVE_WRAPPING else if ((comInterface = NativeToManagedWrapper.GetUnderlyingInterface(managedInterface)) == IntPtr.Zero) #endif comInterface = new ManagedToNativeWrapper(managedInterface).comInterface; return(comInterface); }
public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) { byte[] buffer; long written = 0; int read; int count; if (cb != 0) { if (cb < 4096) { count = (int)cb; } else { count = 4096; } buffer = new byte[count]; SetSizeToPosition(); while (true) { if ((read = baseStream.Read(buffer, 0, count)) == 0) { break; } pstm.Write(buffer, read, IntPtr.Zero); written += read; if (written >= cb) { break; } if (cb - written < 4096) { count = (int)(cb - written); } } } if (pcbRead != IntPtr.Zero) { Marshal.WriteInt64(pcbRead, written); } if (pcbWritten != IntPtr.Zero) { Marshal.WriteInt64(pcbWritten, written); } }
private static int CopyTo(IntPtr @this, IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) { #if MAP_EX_TO_HR try { #endif GetObject(@this).managedInterface.CopyTo(pstm, cb, pcbRead, pcbWritten); return(S_OK); #if MAP_EX_TO_HR } catch (Exception e) { return(GetHRForException(e)); } #endif }
internal static IStream GetUnderlyingInterface(IntPtr comInterface, bool outParam) { if (Marshal.ReadIntPtr(comInterface) == comVtable) { IStream managedInterface = GetObject(comInterface).managedInterface; if (outParam) { Release(comInterface); } return(managedInterface); } else { return(null); } }
private void Dispose() { if (gcHandle.IsAllocated) { gcHandle.Free(); } if (comInterface != IntPtr.Zero) { Marshal.FreeHGlobal(comInterface); comInterface = IntPtr.Zero; } managedInterface = null; lock (managedVtable) { // Dispose vtable when shutting down if (--vtableRefCount == 0 && Environment.HasShutdownStarted) { DisposeVtable(); } } }
public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) { ThrowExceptionForHR(managedVtable.CopyTo(comInterface, pstm, cb, pcbRead, pcbWritten)); }
public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) { byte[] buffer; long written = 0; int read; int count; if (cb != 0) { if (cb < 4096) count = (int)cb; else count = 4096; buffer = new byte[count]; SetSizeToPosition(); while (true) { if ((read = baseStream.Read(buffer, 0, count)) == 0) break; pstm.Write(buffer, read, IntPtr.Zero); written += read; if (written >= cb) break; if (cb - written < 4096) count = (int)(cb - written); } } if (pcbRead != IntPtr.Zero) Marshal.WriteInt64(pcbRead, written); if (pcbWritten != IntPtr.Zero) Marshal.WriteInt64(pcbWritten, written); }
public void Clone(out IStream ppstm) { ppstm = null; throw new ExternalException(null, STG_E_INVALIDFUNCTION); }
private ManagedToNativeWrapper(IStream managedInterface) { IStreamInterface newInterface; lock (managedVtable) { // Vtable may have been disposed when shutting down if (vtableRefCount == 0 && comVtable == IntPtr.Zero) CreateVtable(); vtableRefCount++; } try { this.managedInterface = managedInterface; gcHandle = GCHandle.Alloc(this); newInterface = new IStreamInterface(); newInterface.lpVtbl = comVtable; newInterface.gcHandle = (IntPtr)gcHandle; comInterface = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamInterface))); Marshal.StructureToPtr(newInterface, comInterface, false); } catch { this.Dispose(); throw; } }
private void Dispose() { if (gcHandle.IsAllocated) gcHandle.Free(); if (comInterface != IntPtr.Zero) { Marshal.FreeHGlobal(comInterface); comInterface = IntPtr.Zero; } managedInterface = null; lock (managedVtable) { // Dispose vtable when shutting down if (--vtableRefCount == 0 && Environment.HasShutdownStarted) DisposeVtable(); } }
internal static void ReleaseInterface(IStream managedInterface) { if (managedInterface is NativeToManagedWrapper) ((NativeToManagedWrapper)managedInterface).Dispose(true); }
internal static IntPtr GetUnderlyingInterface(IStream managedInterface) { if (managedInterface is NativeToManagedWrapper) { NativeToManagedWrapper wrapper = (NativeToManagedWrapper)managedInterface; wrapper.managedVtable.AddRef(wrapper.comInterface); return wrapper.comInterface; } else return IntPtr.Zero; }
private static int CopyTo(IntPtr @this, IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) { #if MAP_EX_TO_HR try { #endif GetObject(@this).managedInterface.CopyTo(pstm, cb, pcbRead, pcbWritten); return S_OK; #if MAP_EX_TO_HR } catch (Exception e) { return GetHRForException(e); } #endif }
internal static IntPtr GetInterface(IStream managedInterface) { IntPtr comInterface; if (managedInterface == null) return IntPtr.Zero; #if !RECURSIVE_WRAPPING else if ((comInterface = NativeToManagedWrapper.GetUnderlyingInterface(managedInterface)) == IntPtr.Zero) #endif comInterface = new ManagedToNativeWrapper(managedInterface).comInterface; return comInterface; }