2 // System.Drawing.ComIStreamMarshaler.cs
5 // Kornél Pál <http://www.kornelpal.hu/>
7 // Copyright (C) 2005-2006 Kornél Pál
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // Undefine to debug the protected blocks
34 // Define to debug wrappers recursively
35 // #define RECURSIVE_WRAPPING
39 using System.Reflection;
40 using System.Runtime.InteropServices;
41 using System.Runtime.InteropServices.ComTypes;
42 using STATSTG = System.Runtime.InteropServices.ComTypes.STATSTG;
44 namespace System.Drawing
46 // Mono does not implement COM interface marshaling
47 // This custom marshaler should be replaced with UnmanagedType.Interface
48 // Provides identical behaviour under Mono and .NET Framework
49 internal sealed class ComIStreamMarshaler : ICustomMarshaler
51 private const int S_OK = 0x00000000;
52 private const int E_NOINTERFACE = unchecked((int)0x80004002);
54 private delegate int QueryInterfaceDelegate(IntPtr @this, [In()] ref Guid riid, IntPtr ppvObject);
55 private delegate int AddRefDelegate(IntPtr @this);
56 private delegate int ReleaseDelegate(IntPtr @this);
57 private delegate int ReadDelegate(IntPtr @this, [Out(), MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pv, int cb, IntPtr pcbRead);
58 private delegate int WriteDelegate(IntPtr @this, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pv, int cb, IntPtr pcbWritten);
59 private delegate int SeekDelegate(IntPtr @this, long dlibMove, int dwOrigin, IntPtr plibNewPosition);
60 private delegate int SetSizeDelegate(IntPtr @this, long libNewSize);
61 private delegate int CopyToDelegate(IntPtr @this, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ComIStreamMarshaler))] IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten);
62 private delegate int CommitDelegate(IntPtr @this, int grfCommitFlags);
63 private delegate int RevertDelegate(IntPtr @this);
64 private delegate int LockRegionDelegate(IntPtr @this, long libOffset, long cb, int dwLockType);
65 private delegate int UnlockRegionDelegate(IntPtr @this, long libOffset, long cb, int dwLockType);
66 private delegate int StatDelegate(IntPtr @this, out STATSTG pstatstg, int grfStatFlag);
67 private delegate int CloneDelegate(IntPtr @this, out IntPtr ppstm);
69 [StructLayout(LayoutKind.Sequential)]
70 private sealed class IStreamInterface
72 internal IntPtr lpVtbl;
73 internal IntPtr gcHandle;
76 [StructLayout(LayoutKind.Sequential)]
77 private sealed class IStreamVtbl
79 internal QueryInterfaceDelegate QueryInterface;
80 internal AddRefDelegate AddRef;
81 internal ReleaseDelegate Release;
82 internal ReadDelegate Read;
83 internal WriteDelegate Write;
84 internal SeekDelegate Seek;
85 internal SetSizeDelegate SetSize;
86 internal CopyToDelegate CopyTo;
87 internal CommitDelegate Commit;
88 internal RevertDelegate Revert;
89 internal LockRegionDelegate LockRegion;
90 internal UnlockRegionDelegate UnlockRegion;
91 internal StatDelegate Stat;
92 internal CloneDelegate Clone;
95 // Managed COM Callable Wrapper implementation
96 // Reference counting is thread safe
97 private sealed class ManagedToNativeWrapper
99 // Mono does not implement Marshal.Release
100 [StructLayout(LayoutKind.Sequential)]
101 private sealed class ReleaseSlot
103 internal ReleaseDelegate Release;
106 private static readonly Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
107 private static readonly Guid IID_IStream = new Guid("0000000C-0000-0000-C000-000000000046");
108 private static readonly MethodInfo exceptionGetHResult = typeof(Exception).GetProperty("HResult", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding, null, typeof(int), new Type[] {}, null).GetGetMethod(true);
109 // Keeps delegates alive while they are marshaled
110 private static readonly IStreamVtbl managedVtable;
111 private static IntPtr comVtable;
112 private static int vtableRefCount;
114 private IStream managedInterface;
115 private IntPtr comInterface;
116 // Keeps the object alive when it has no managed references
117 private GCHandle gcHandle;
118 private int refCount = 1;
120 static ManagedToNativeWrapper()
122 EventHandler onShutdown;
123 AppDomain currentDomain;
124 IStreamVtbl newVtable;
126 onShutdown = new EventHandler(OnShutdown);
127 currentDomain = AppDomain.CurrentDomain;
128 currentDomain.DomainUnload += onShutdown;
129 currentDomain.ProcessExit += onShutdown;
131 newVtable = new IStreamVtbl();
132 newVtable.QueryInterface = new QueryInterfaceDelegate(QueryInterface);
133 newVtable.AddRef = new AddRefDelegate(AddRef);
134 newVtable.Release = new ReleaseDelegate(Release);
135 newVtable.Read = new ReadDelegate(Read);
136 newVtable.Write = new WriteDelegate(Write);
137 newVtable.Seek = new SeekDelegate(Seek);
138 newVtable.SetSize = new SetSizeDelegate(SetSize);
139 newVtable.CopyTo = new CopyToDelegate(CopyTo);
140 newVtable.Commit = new CommitDelegate(Commit);
141 newVtable.Revert = new RevertDelegate(Revert);
142 newVtable.LockRegion = new LockRegionDelegate(LockRegion);
143 newVtable.UnlockRegion = new UnlockRegionDelegate(UnlockRegion);
144 newVtable.Stat = new StatDelegate(Stat);
145 newVtable.Clone = new CloneDelegate(Clone);
146 managedVtable = newVtable;
151 private ManagedToNativeWrapper(IStream managedInterface)
153 IStreamInterface newInterface;
157 // Vtable may have been disposed when shutting down
158 if (vtableRefCount == 0 && comVtable == IntPtr.Zero)
165 this.managedInterface = managedInterface;
166 gcHandle = GCHandle.Alloc(this);
168 newInterface = new IStreamInterface();
169 newInterface.lpVtbl = comVtable;
170 newInterface.gcHandle = (IntPtr)gcHandle;
171 comInterface = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamInterface)));
172 Marshal.StructureToPtr(newInterface, comInterface, false);
181 private void Dispose()
183 if (gcHandle.IsAllocated)
186 if (comInterface != IntPtr.Zero)
188 Marshal.FreeHGlobal(comInterface);
189 comInterface = IntPtr.Zero;
192 managedInterface = null;
196 // Dispose vtable when shutting down
197 if (--vtableRefCount == 0 && Environment.HasShutdownStarted)
202 private static void OnShutdown(object sender, EventArgs e)
206 // There may be object instances when shutting down
207 if (vtableRefCount == 0 && comVtable != IntPtr.Zero)
212 private static void CreateVtable()
214 comVtable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamVtbl)));
215 Marshal.StructureToPtr(managedVtable, comVtable, false);
218 private static void DisposeVtable()
220 Marshal.DestroyStructure(comVtable, typeof(IStreamVtbl));
221 Marshal.FreeHGlobal(comVtable);
222 comVtable = IntPtr.Zero;
225 internal static IStream GetUnderlyingInterface(IntPtr comInterface, bool outParam)
227 if (Marshal.ReadIntPtr(comInterface) == comVtable)
229 IStream managedInterface = GetObject(comInterface).managedInterface;
232 Release(comInterface);
234 return managedInterface;
240 internal static IntPtr GetInterface(IStream managedInterface)
244 if (managedInterface == null)
246 #if !RECURSIVE_WRAPPING
247 else if ((comInterface = NativeToManagedWrapper.GetUnderlyingInterface(managedInterface)) == IntPtr.Zero)
249 comInterface = new ManagedToNativeWrapper(managedInterface).comInterface;
254 internal static void ReleaseInterface(IntPtr comInterface)
256 if (comInterface != IntPtr.Zero)
258 IntPtr vtable = Marshal.ReadIntPtr(comInterface);
260 if (vtable == comVtable)
261 Release(comInterface);
264 ReleaseSlot releaseSlot = (ReleaseSlot)Marshal.PtrToStructure((IntPtr)((long)vtable + (long)(IntPtr.Size * 2)), typeof(ReleaseSlot));
265 releaseSlot.Release(comInterface);
270 // Mono does not implement Marshal.GetHRForException
271 private static int GetHRForException(Exception e)
273 return (int)exceptionGetHResult.Invoke(e, null);
276 private static ManagedToNativeWrapper GetObject(IntPtr @this)
278 return (ManagedToNativeWrapper)((GCHandle)Marshal.ReadIntPtr(@this, IntPtr.Size)).Target;
281 private static int QueryInterface(IntPtr @this, ref Guid riid, IntPtr ppvObject)
287 if (IID_IUnknown.Equals(riid) || IID_IStream.Equals(riid))
289 Marshal.WriteIntPtr(ppvObject, @this);
295 Marshal.WriteIntPtr(ppvObject, IntPtr.Zero);
296 return E_NOINTERFACE;
302 return GetHRForException(e);
307 private static int AddRef(IntPtr @this)
313 ManagedToNativeWrapper thisObject = GetObject(@this);
317 return ++thisObject.refCount;
328 private static int Release(IntPtr @this)
334 ManagedToNativeWrapper thisObject = GetObject(@this);
338 if ((thisObject.refCount != 0) && (--thisObject.refCount == 0))
339 thisObject.Dispose();
341 return thisObject.refCount;
352 private static int Read(IntPtr @this, byte[] pv, int cb, IntPtr pcbRead)
358 GetObject(@this).managedInterface.Read(pv, cb, pcbRead);
364 return GetHRForException(e);
369 private static int Write(IntPtr @this, byte[] pv, int cb, IntPtr pcbWritten)
375 GetObject(@this).managedInterface.Write(pv, cb, pcbWritten);
381 return GetHRForException(e);
386 private static int Seek(IntPtr @this, long dlibMove, int dwOrigin, IntPtr plibNewPosition)
392 GetObject(@this).managedInterface.Seek(dlibMove, dwOrigin, plibNewPosition);
398 return GetHRForException(e);
403 private static int SetSize(IntPtr @this, long libNewSize)
409 GetObject(@this).managedInterface.SetSize(libNewSize);
415 return GetHRForException(e);
420 private static int CopyTo(IntPtr @this, IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
426 GetObject(@this).managedInterface.CopyTo(pstm, cb, pcbRead, pcbWritten);
432 return GetHRForException(e);
437 private static int Commit(IntPtr @this, int grfCommitFlags)
443 GetObject(@this).managedInterface.Commit(grfCommitFlags);
449 return GetHRForException(e);
454 private static int Revert(IntPtr @this)
460 GetObject(@this).managedInterface.Revert();
466 return GetHRForException(e);
471 private static int LockRegion(IntPtr @this, long libOffset, long cb, int dwLockType)
477 GetObject(@this).managedInterface.LockRegion(libOffset, cb, dwLockType);
483 return GetHRForException(e);
488 private static int UnlockRegion(IntPtr @this, long libOffset, long cb, int dwLockType)
494 GetObject(@this).managedInterface.UnlockRegion(libOffset, cb, dwLockType);
500 return GetHRForException(e);
505 private static int Stat(IntPtr @this, out STATSTG pstatstg, int grfStatFlag)
511 GetObject(@this).managedInterface.Stat(out pstatstg, grfStatFlag);
517 pstatstg = new STATSTG();
518 return GetHRForException(e);
523 private static int Clone(IntPtr @this, out IntPtr ppstm)
530 IStream newInterface;
532 GetObject(@this).managedInterface.Clone(out newInterface);
533 ppstm = ManagedToNativeWrapper.GetInterface(newInterface);
539 return GetHRForException(e);
545 // Managed Runtime Callable Wrapper implementation
546 private sealed class NativeToManagedWrapper : IStream
548 private IntPtr comInterface;
549 private IStreamVtbl managedVtable;
551 private NativeToManagedWrapper(IntPtr comInterface, bool outParam)
553 this.comInterface = comInterface;
554 managedVtable = (IStreamVtbl)Marshal.PtrToStructure(Marshal.ReadIntPtr(comInterface), typeof(IStreamVtbl));
556 managedVtable.AddRef(comInterface);
559 ~NativeToManagedWrapper()
564 private void Dispose(bool disposing)
566 managedVtable.Release(comInterface);
569 comInterface = IntPtr.Zero;
570 managedVtable = null;
571 GC.SuppressFinalize(this);
575 internal static IntPtr GetUnderlyingInterface(IStream managedInterface)
577 if (managedInterface is NativeToManagedWrapper)
579 NativeToManagedWrapper wrapper = (NativeToManagedWrapper)managedInterface;
581 wrapper.managedVtable.AddRef(wrapper.comInterface);
582 return wrapper.comInterface;
588 internal static IStream GetInterface(IntPtr comInterface, bool outParam)
590 IStream managedInterface;
592 if (comInterface == IntPtr.Zero)
594 #if !RECURSIVE_WRAPPING
595 else if ((managedInterface = ManagedToNativeWrapper.GetUnderlyingInterface(comInterface, outParam)) == null)
597 managedInterface = (IStream)new NativeToManagedWrapper(comInterface, outParam);
599 return managedInterface;
602 internal static void ReleaseInterface(IStream managedInterface)
604 if (managedInterface is NativeToManagedWrapper)
605 ((NativeToManagedWrapper)managedInterface).Dispose(true);
608 // Mono does not implement Marshal.ThrowExceptionForHR
609 private static void ThrowExceptionForHR(int result)
612 throw new COMException(null, result);
615 public void Read(byte[] pv, int cb, IntPtr pcbRead)
617 ThrowExceptionForHR(managedVtable.Read(comInterface, pv, cb, pcbRead));
620 public void Write(byte[] pv, int cb, IntPtr pcbWritten)
622 ThrowExceptionForHR(managedVtable.Write(comInterface, pv, cb, pcbWritten));
625 public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
627 ThrowExceptionForHR(managedVtable.Seek(comInterface, dlibMove, dwOrigin, plibNewPosition));
630 public void SetSize(long libNewSize)
632 ThrowExceptionForHR(managedVtable.SetSize(comInterface, libNewSize));
635 public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
637 ThrowExceptionForHR(managedVtable.CopyTo(comInterface, pstm, cb, pcbRead, pcbWritten));
640 public void Commit(int grfCommitFlags)
642 ThrowExceptionForHR(managedVtable.Commit(comInterface, grfCommitFlags));
647 ThrowExceptionForHR(managedVtable.Revert(comInterface));
650 public void LockRegion(long libOffset, long cb, int dwLockType)
652 ThrowExceptionForHR(managedVtable.LockRegion(comInterface, libOffset, cb, dwLockType));
655 public void UnlockRegion(long libOffset, long cb, int dwLockType)
657 ThrowExceptionForHR(managedVtable.UnlockRegion(comInterface, libOffset, cb, dwLockType));
660 public void Stat(out STATSTG pstatstg, int grfStatFlag)
662 ThrowExceptionForHR(managedVtable.Stat(comInterface, out pstatstg, grfStatFlag));
665 public void Clone(out IStream ppstm)
669 ThrowExceptionForHR(managedVtable.Clone(comInterface, out newInterface));
670 ppstm = NativeToManagedWrapper.GetInterface(newInterface, true);
674 private static readonly ComIStreamMarshaler defaultInstance = new ComIStreamMarshaler();
676 private ComIStreamMarshaler()
680 private static ICustomMarshaler GetInstance(string cookie)
682 return defaultInstance;
685 public IntPtr MarshalManagedToNative(object managedObj)
687 #if RECURSIVE_WRAPPING
688 managedObj = NativeToManagedWrapper.GetInterface(ManagedToNativeWrapper.GetInterface((IStream)managedObj), true);
690 return ManagedToNativeWrapper.GetInterface((IStream)managedObj);
693 public void CleanUpNativeData(IntPtr pNativeData)
695 ManagedToNativeWrapper.ReleaseInterface(pNativeData);
698 public object MarshalNativeToManaged(IntPtr pNativeData)
700 #if RECURSIVE_WRAPPING
701 pNativeData = ManagedToNativeWrapper.GetInterface(NativeToManagedWrapper.GetInterface(pNativeData, true));
703 return NativeToManagedWrapper.GetInterface(pNativeData, false);
706 public void CleanUpManagedData(object managedObj)
708 NativeToManagedWrapper.ReleaseInterface((IStream)managedObj);
711 public int GetNativeDataSize()