2 // System.Drawing.ComIStreamMarshaler.cs
5 // Kornél Pál <http://www.kornelpal.hu/>
7 // Copyright (C) 2005 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;
42 using System.Runtime.InteropServices.ComTypes;
43 using STATSTG = System.Runtime.InteropServices.ComTypes.STATSTG;
45 using IStream = System.Runtime.InteropServices.UCOMIStream;
48 namespace System.Drawing
50 // Mono does not implement COM interface marshaling
51 // This custom marshaler should be replaced with UnmanagedType.Interface
52 // Provides identical behaviour under Mono and .NET Framework
53 internal sealed class ComIStreamMarshaler : ICustomMarshaler
55 private const int S_OK = 0x00000000;
56 private const int E_NOINTERFACE = unchecked((int)0x80004002);
58 private delegate int QueryInterfaceDelegate(IntPtr @this, [In()] ref Guid riid, IntPtr ppvObject);
59 private delegate int AddRefDelegate(IntPtr @this);
60 private delegate int ReleaseDelegate(IntPtr @this);
61 private delegate int ReadDelegate(IntPtr @this, [Out(), MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pv, int cb, IntPtr pcbRead);
62 private delegate int WriteDelegate(IntPtr @this, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] pv, int cb, IntPtr pcbWritten);
63 private delegate int SeekDelegate(IntPtr @this, long dlibMove, int dwOrigin, IntPtr plibNewPosition);
64 private delegate int SetSizeDelegate(IntPtr @this, long libNewSize);
65 private delegate int CopyToDelegate(IntPtr @this, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(ComIStreamMarshaler))] IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten);
66 private delegate int CommitDelegate(IntPtr @this, int grfCommitFlags);
67 private delegate int RevertDelegate(IntPtr @this);
68 private delegate int LockRegionDelegate(IntPtr @this, long libOffset, long cb, int dwLockType);
69 private delegate int UnlockRegionDelegate(IntPtr @this, long libOffset, long cb, int dwLockType);
70 private delegate int StatDelegate(IntPtr @this, out STATSTG pstatstg, int grfStatFlag);
71 private delegate int CloneDelegate(IntPtr @this, out IntPtr ppstm);
73 [StructLayout(LayoutKind.Sequential)]
74 private sealed class IStreamInterface
76 internal IntPtr lpVtbl;
77 internal IntPtr gcHandle;
80 [StructLayout(LayoutKind.Sequential)]
81 private sealed class IStreamVtbl
83 internal QueryInterfaceDelegate QueryInterface;
84 internal AddRefDelegate AddRef;
85 internal ReleaseDelegate Release;
86 internal ReadDelegate Read;
87 internal WriteDelegate Write;
88 internal SeekDelegate Seek;
89 internal SetSizeDelegate SetSize;
90 internal CopyToDelegate CopyTo;
91 internal CommitDelegate Commit;
92 internal RevertDelegate Revert;
93 internal LockRegionDelegate LockRegion;
94 internal UnlockRegionDelegate UnlockRegion;
95 internal StatDelegate Stat;
96 internal CloneDelegate Clone;
99 // Managed COM Callable Wrapper implementation
100 // Reference counting is thread safe
101 private sealed class ManagedToNativeWrapper
103 // Mono does not implement Marshal.Release
104 [StructLayout(LayoutKind.Sequential)]
105 private sealed class ReleaseSlot
107 internal ReleaseDelegate Release;
110 private sealed class VtableDestructor
114 Marshal.DestroyStructure(comVtable, typeof(IStreamVtbl));
115 Marshal.FreeHGlobal(comVtable);
119 private static readonly Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
120 private static readonly Guid IID_IStream = new Guid("0000000C-0000-0000-C000-000000000046");
121 private static readonly MethodInfo exceptionGetHResult = typeof(Exception).GetProperty("HResult", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding, null, typeof(int), new Type[] {}, null).GetGetMethod(true);
122 // Keeps delegates alive while they are marshaled
123 private static readonly IStreamVtbl managedVtable;
124 private static readonly IntPtr comVtable;
125 private static readonly VtableDestructor vtableDestructor;
127 private IStream managedInterface;
128 private IntPtr comInterface;
129 // Keeps the object alive when it has no managed references
130 private GCHandle gcHandle;
131 private int refCount = 1;
133 static ManagedToNativeWrapper()
135 IStreamVtbl newVtable;
137 newVtable = new IStreamVtbl();
138 newVtable.QueryInterface = new QueryInterfaceDelegate(QueryInterface);
139 newVtable.AddRef = new AddRefDelegate(AddRef);
140 newVtable.Release = new ReleaseDelegate(Release);
141 newVtable.Read = new ReadDelegate(Read);
142 newVtable.Write = new WriteDelegate(Write);
143 newVtable.Seek = new SeekDelegate(Seek);
144 newVtable.SetSize = new SetSizeDelegate(SetSize);
145 newVtable.CopyTo = new CopyToDelegate(CopyTo);
146 newVtable.Commit = new CommitDelegate(Commit);
147 newVtable.Revert = new RevertDelegate(Revert);
148 newVtable.LockRegion = new LockRegionDelegate(LockRegion);
149 newVtable.UnlockRegion = new UnlockRegionDelegate(UnlockRegion);
150 newVtable.Stat = new StatDelegate(Stat);
151 newVtable.Clone = new CloneDelegate(Clone);
152 comVtable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamVtbl)));
153 Marshal.StructureToPtr(newVtable, comVtable, false);
154 managedVtable = newVtable;
156 vtableDestructor = new VtableDestructor();
159 private ManagedToNativeWrapper(IStream managedInterface)
161 IStreamInterface newInterface;
163 this.managedInterface = managedInterface;
164 gcHandle = GCHandle.Alloc(this);
166 newInterface = new IStreamInterface();
167 newInterface.lpVtbl = comVtable;
168 newInterface.gcHandle = (IntPtr)gcHandle;
169 comInterface = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IStreamInterface)));
170 Marshal.StructureToPtr(newInterface, comInterface, false);
173 ~ManagedToNativeWrapper()
178 private void Dispose(bool disposing)
180 Marshal.FreeHGlobal(comInterface);
184 comInterface = IntPtr.Zero;
185 managedInterface = null;
186 GC.SuppressFinalize(this);
190 internal static IStream GetUnderlyingInterface(IntPtr comInterface, bool outParam)
192 if (Marshal.ReadIntPtr(comInterface) == comVtable)
194 IStream managedInterface = GetObject(comInterface).managedInterface;
197 Release(comInterface);
199 return managedInterface;
205 internal static IntPtr GetInterface(IStream managedInterface)
209 if (managedInterface == null)
211 #if !RECURSIVE_WRAPPING
212 else if ((comInterface = NativeToManagedWrapper.GetUnderlyingInterface(managedInterface)) == IntPtr.Zero)
214 comInterface = new ManagedToNativeWrapper(managedInterface).comInterface;
219 internal static void ReleaseInterface(IntPtr comInterface)
221 if (comInterface != IntPtr.Zero)
223 IntPtr vtable = Marshal.ReadIntPtr(comInterface);
225 if (vtable == comVtable)
226 Release(comInterface);
229 ReleaseSlot releaseSlot = (ReleaseSlot)Marshal.PtrToStructure((IntPtr)((long)vtable + (long)(IntPtr.Size * 2)), typeof(ReleaseSlot));
230 releaseSlot.Release(comInterface);
235 // Mono does not implement Marshal.GetHRForException
236 private static int GetHRForException(Exception e)
238 return (int)exceptionGetHResult.Invoke(e, null);
241 private static ManagedToNativeWrapper GetObject(IntPtr @this)
243 return (ManagedToNativeWrapper)((GCHandle)Marshal.ReadIntPtr(@this, IntPtr.Size)).Target;
246 private static int QueryInterface(IntPtr @this, ref Guid riid, IntPtr ppvObject)
252 if (IID_IUnknown.Equals(riid) || IID_IStream.Equals(riid))
254 Marshal.WriteIntPtr(ppvObject, @this);
260 Marshal.WriteIntPtr(ppvObject, IntPtr.Zero);
261 return E_NOINTERFACE;
267 return GetHRForException(e);
272 private static int AddRef(IntPtr @this)
278 ManagedToNativeWrapper thisObject = GetObject(@this);
282 return ++thisObject.refCount;
293 private static int Release(IntPtr @this)
299 ManagedToNativeWrapper thisObject = GetObject(@this);
303 if ((thisObject.refCount != 0) && (--thisObject.refCount == 0))
304 thisObject.Dispose(true);
306 return thisObject.refCount;
317 private static int Read(IntPtr @this, byte[] pv, int cb, IntPtr pcbRead)
323 GetObject(@this).managedInterface.Read(pv, cb, pcbRead);
329 return GetHRForException(e);
334 private static int Write(IntPtr @this, byte[] pv, int cb, IntPtr pcbWritten)
340 GetObject(@this).managedInterface.Write(pv, cb, pcbWritten);
346 return GetHRForException(e);
351 private static int Seek(IntPtr @this, long dlibMove, int dwOrigin, IntPtr plibNewPosition)
357 GetObject(@this).managedInterface.Seek(dlibMove, dwOrigin, plibNewPosition);
363 return GetHRForException(e);
368 private static int SetSize(IntPtr @this, long libNewSize)
374 GetObject(@this).managedInterface.SetSize(libNewSize);
380 return GetHRForException(e);
385 private static int CopyTo(IntPtr @this, IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
391 GetObject(@this).managedInterface.CopyTo(pstm, cb, pcbRead, pcbWritten);
397 return GetHRForException(e);
402 private static int Commit(IntPtr @this, int grfCommitFlags)
408 GetObject(@this).managedInterface.Commit(grfCommitFlags);
414 return GetHRForException(e);
419 private static int Revert(IntPtr @this)
425 GetObject(@this).managedInterface.Revert();
431 return GetHRForException(e);
436 private static int LockRegion(IntPtr @this, long libOffset, long cb, int dwLockType)
442 GetObject(@this).managedInterface.LockRegion(libOffset, cb, dwLockType);
448 return GetHRForException(e);
453 private static int UnlockRegion(IntPtr @this, long libOffset, long cb, int dwLockType)
459 GetObject(@this).managedInterface.UnlockRegion(libOffset, cb, dwLockType);
465 return GetHRForException(e);
470 private static int Stat(IntPtr @this, out STATSTG pstatstg, int grfStatFlag)
476 GetObject(@this).managedInterface.Stat(out pstatstg, grfStatFlag);
482 pstatstg = new STATSTG();
483 return GetHRForException(e);
488 private static int Clone(IntPtr @this, out IntPtr ppstm)
495 IStream newInterface;
497 GetObject(@this).managedInterface.Clone(out newInterface);
498 ppstm = ManagedToNativeWrapper.GetInterface(newInterface);
504 return GetHRForException(e);
510 // Managed Runtime Callable Wrapper implementation
511 private sealed class NativeToManagedWrapper : IStream
513 private IntPtr comInterface;
514 private IStreamVtbl managedVtable;
516 private NativeToManagedWrapper(IntPtr comInterface, bool outParam)
518 this.comInterface = comInterface;
519 managedVtable = (IStreamVtbl)Marshal.PtrToStructure(Marshal.ReadIntPtr(comInterface), typeof(IStreamVtbl));
521 managedVtable.AddRef(comInterface);
524 ~NativeToManagedWrapper()
529 private void Dispose(bool disposing)
531 managedVtable.Release(comInterface);
534 comInterface = IntPtr.Zero;
535 managedVtable = null;
536 GC.SuppressFinalize(this);
540 internal static IntPtr GetUnderlyingInterface(IStream managedInterface)
542 if (managedInterface is NativeToManagedWrapper)
544 NativeToManagedWrapper wrapper = (NativeToManagedWrapper)managedInterface;
546 wrapper.managedVtable.AddRef(wrapper.comInterface);
547 return wrapper.comInterface;
553 internal static IStream GetInterface(IntPtr comInterface, bool outParam)
555 IStream managedInterface;
557 if (comInterface == IntPtr.Zero)
559 #if !RECURSIVE_WRAPPING
560 else if ((managedInterface = ManagedToNativeWrapper.GetUnderlyingInterface(comInterface, outParam)) == null)
562 managedInterface = (IStream)new NativeToManagedWrapper(comInterface, outParam);
564 return managedInterface;
567 internal static void ReleaseInterface(IStream managedInterface)
569 if (managedInterface is NativeToManagedWrapper)
570 ((NativeToManagedWrapper)managedInterface).Dispose(true);
573 // Mono does not implement Marshal.ThrowExceptionForHR
574 private static void ThrowExceptionForHR(int result)
577 throw new COMException(null, result);
580 public void Read(byte[] pv, int cb, IntPtr pcbRead)
582 ThrowExceptionForHR(managedVtable.Read(comInterface, pv, cb, pcbRead));
585 public void Write(byte[] pv, int cb, IntPtr pcbWritten)
587 ThrowExceptionForHR(managedVtable.Write(comInterface, pv, cb, pcbWritten));
590 public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
592 ThrowExceptionForHR(managedVtable.Seek(comInterface, dlibMove, dwOrigin, plibNewPosition));
595 public void SetSize(long libNewSize)
597 ThrowExceptionForHR(managedVtable.SetSize(comInterface, libNewSize));
600 public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
602 ThrowExceptionForHR(managedVtable.CopyTo(comInterface, pstm, cb, pcbRead, pcbWritten));
605 public void Commit(int grfCommitFlags)
607 ThrowExceptionForHR(managedVtable.Commit(comInterface, grfCommitFlags));
612 ThrowExceptionForHR(managedVtable.Revert(comInterface));
615 public void LockRegion(long libOffset, long cb, int dwLockType)
617 ThrowExceptionForHR(managedVtable.LockRegion(comInterface, libOffset, cb, dwLockType));
620 public void UnlockRegion(long libOffset, long cb, int dwLockType)
622 ThrowExceptionForHR(managedVtable.UnlockRegion(comInterface, libOffset, cb, dwLockType));
625 public void Stat(out STATSTG pstatstg, int grfStatFlag)
627 ThrowExceptionForHR(managedVtable.Stat(comInterface, out pstatstg, grfStatFlag));
630 public void Clone(out IStream ppstm)
634 ThrowExceptionForHR(managedVtable.Clone(comInterface, out newInterface));
635 ppstm = NativeToManagedWrapper.GetInterface(newInterface, true);
639 private static readonly ComIStreamMarshaler defaultInstance = new ComIStreamMarshaler();
641 private ComIStreamMarshaler()
645 private static ICustomMarshaler GetInstance(string cookie)
647 return defaultInstance;
650 public IntPtr MarshalManagedToNative(object managedObj)
652 #if RECURSIVE_WRAPPING
653 managedObj = NativeToManagedWrapper.GetInterface(ManagedToNativeWrapper.GetInterface((IStream)managedObj), true);
655 return ManagedToNativeWrapper.GetInterface((IStream)managedObj);
658 public void CleanUpNativeData(IntPtr pNativeData)
660 ManagedToNativeWrapper.ReleaseInterface(pNativeData);
663 public object MarshalNativeToManaged(IntPtr pNativeData)
665 #if RECURSIVE_WRAPPING
666 pNativeData = ManagedToNativeWrapper.GetInterface(NativeToManagedWrapper.GetInterface(pNativeData, true));
668 return NativeToManagedWrapper.GetInterface(pNativeData, false);
671 public void CleanUpManagedData(object managedObj)
673 NativeToManagedWrapper.ReleaseInterface((IStream)managedObj);
676 public int GetNativeDataSize()