move FrameworkName from corlib to System
[mcs.git] / class / corlib / Mono.Interop / ComInteropProxy.cs
blob54fefe2b24d9c8f9b68babac98de366b2e811dc7
1 //
2 // Mono.Interop.ComInteropProxy
3 //
4 // Authors:
5 // Jonathan Chambers <joncham@gmail.com>
6 //
7 // Copyright (C) 2006 Jonathan Chambers
8 //
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 using System;
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 internal class ComInteropProxy : RealProxy, IRemotingTypeInfo
45 #region Sync with object-internals.h
46 private __ComObject com_object;
47 int ref_count = 1; // wrapper ref count
48 #endregion
49 private string type_name;
51 [MethodImplAttribute (MethodImplOptions.InternalCall)]
52 private extern static void AddProxy (IntPtr pItf, ComInteropProxy proxy);
54 [MethodImplAttribute (MethodImplOptions.InternalCall)]
55 internal extern static ComInteropProxy FindProxy (IntPtr pItf);
57 // Private. Objects must be created with CreateProxy.
58 ComInteropProxy (Type t)
59 : base (t)
61 // object only created here
62 // .ctor is called later
63 com_object = __ComObject.CreateRCW (t);
66 void CacheProxy ()
68 // called from unmanaged code after .ctor is invoked
69 // we need .ctor to create unmanaged object and thus IUnknown property value
70 if (FindProxy(com_object.IUnknown) == null)
71 AddProxy (com_object.IUnknown, this);
72 else
73 System.Threading.Interlocked.Increment (ref ref_count);
76 ComInteropProxy (IntPtr pUnk)
77 : this (pUnk, typeof (__ComObject))
81 internal ComInteropProxy (IntPtr pUnk, Type t)
82 : base (t)
84 com_object = new __ComObject (pUnk);
85 CacheProxy ();
88 internal static ComInteropProxy GetProxy (IntPtr pItf, Type t)
90 IntPtr ppv;
91 Guid iid = __ComObject.IID_IUnknown;
92 int hr = Marshal.QueryInterface (pItf, ref iid, out ppv);
93 Marshal.ThrowExceptionForHR (hr);
94 ComInteropProxy obj = FindProxy (ppv);
95 if (obj == null) {
96 Marshal.Release (pItf);
97 return new ComInteropProxy (ppv);
99 else {
100 Marshal.Release (pItf);
101 System.Threading.Interlocked.Increment (ref obj.ref_count);
102 return obj;
106 // Gets the proxy of the specified COM type. If the COM type is
107 // already known, a cached proxy will be returned.
108 internal static ComInteropProxy CreateProxy (Type t)
110 ComInteropProxy proxy = new ComInteropProxy (t);
111 proxy.com_object.Initialize (t);
113 ComInteropProxy cachedProxy = FindProxy (proxy.com_object.IUnknown);
114 if (cachedProxy != null) {
115 // check that the COM type of the cached proxy matches
116 // the requested type. See 2nd part of bug #520437.
117 Type cachedType = cachedProxy.com_object.GetType ();
118 if (cachedType != t)
119 throw new InvalidCastException (String.Format ("Unable to cast object of type '{0}' to type '{1}'.", cachedType, t));
120 return cachedProxy;
122 return proxy;
125 public override IMessage Invoke (IMessage msg)
127 Console.WriteLine ("Invoke");
128 Console.WriteLine (System.Environment.StackTrace);
130 throw new Exception ("The method or operation is not implemented.");
133 public string TypeName
135 get { return type_name; }
136 set { type_name = value; }
139 public bool CanCastTo (Type fromType, object o)
141 __ComObject co = o as __ComObject;
142 if (co == null)
143 throw new NotSupportedException ("Only RCWs are currently supported");
145 if ((fromType.Attributes & TypeAttributes.Import) == 0)
146 return false;
148 if (co.GetInterface (fromType, false) == IntPtr.Zero)
149 return false;
151 return true;