2 // Mono.Interop.ComInteropProxy
5 // Jonathan Chambers <joncham@gmail.com>
7 // Copyright (C) 2006 Jonathan Chambers
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.
32 using System.Collections;
33 using System.Reflection;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting;
36 using System.Runtime.Remoting.Messaging;
37 using System.Runtime.Remoting.Proxies;
38 using System.Runtime.InteropServices;
41 namespace Mono.Interop
43 [StructLayout (LayoutKind.Sequential)]
44 internal class ComInteropProxy : RealProxy, IRemotingTypeInfo
46 #region Sync with object-internals.h
47 private __ComObject com_object;
48 int ref_count = 1; // wrapper ref count
50 private string type_name;
52 [MethodImplAttribute (MethodImplOptions.InternalCall)]
53 private extern static void AddProxy (IntPtr pItf, ComInteropProxy proxy);
55 [MethodImplAttribute (MethodImplOptions.InternalCall)]
56 internal extern static ComInteropProxy FindProxy (IntPtr pItf);
58 // Private. Objects must be created with CreateProxy.
59 ComInteropProxy (Type t)
62 // object only created here
63 // .ctor is called later
64 com_object = __ComObject.CreateRCW (t);
69 // called from unmanaged code after .ctor is invoked
70 // we need .ctor to create unmanaged object and thus IUnknown property value
71 if (FindProxy(com_object.IUnknown) == null)
72 AddProxy (com_object.IUnknown, this);
74 System.Threading.Interlocked.Increment (ref ref_count);
77 ComInteropProxy (IntPtr pUnk)
78 : this (pUnk, typeof (__ComObject))
82 internal ComInteropProxy (IntPtr pUnk, Type t)
85 com_object = new __ComObject (pUnk);
89 internal static ComInteropProxy GetProxy (IntPtr pItf, Type t)
92 Guid iid = __ComObject.IID_IUnknown;
93 int hr = Marshal.QueryInterface (pItf, ref iid, out ppv);
94 Marshal.ThrowExceptionForHR (hr);
95 ComInteropProxy obj = FindProxy (ppv);
97 Marshal.Release (ppv);
98 return new ComInteropProxy (ppv);
101 Marshal.Release (ppv);
102 System.Threading.Interlocked.Increment (ref obj.ref_count);
107 // Gets the proxy of the specified COM type. If the COM type is
108 // already known, a cached proxy will be returned.
109 internal static ComInteropProxy CreateProxy (Type t)
111 ComInteropProxy proxy = new ComInteropProxy (t);
112 proxy.com_object.Initialize (t);
114 ComInteropProxy cachedProxy = FindProxy (proxy.com_object.IUnknown);
115 if (cachedProxy != null) {
116 // check that the COM type of the cached proxy matches
117 // the requested type. See 2nd part of bug #520437.
118 Type cachedType = cachedProxy.com_object.GetType ();
120 throw new InvalidCastException (String.Format ("Unable to cast object of type '{0}' to type '{1}'.", cachedType, t));
126 public override IMessage Invoke (IMessage msg)
128 Console.WriteLine ("Invoke");
129 Console.WriteLine (System.Environment.StackTrace);
131 throw new Exception ("The method or operation is not implemented.");
134 public string TypeName
136 get { return type_name; }
137 set { type_name = value; }
140 public bool CanCastTo (Type fromType, object o)
142 __ComObject co = o as __ComObject;
144 throw new NotSupportedException ("Only RCWs are currently supported");
146 if ((fromType.Attributes & TypeAttributes.Import) == 0)
149 if (co.GetInterface (fromType, false) == IntPtr.Zero)