[Task] Add an extra check in Task.WaitAny to make sure the index returned is valid
[mono.git] / mcs / class / corlib / Mono.Interop / ComInteropProxy.cs
1 //
2 // Mono.Interop.ComInteropProxy
3 //
4 // Authors:
5 //   Jonathan Chambers <joncham@gmail.com>
6 //
7 // Copyright (C) 2006 Jonathan Chambers
8 //
9
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
29 //
30
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;
39
40
41 namespace Mono.Interop
42 {
43         internal class ComInteropProxy : RealProxy, IRemotingTypeInfo
44     {
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;
50
51                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
52                 private extern static void AddProxy (IntPtr pItf, ComInteropProxy proxy);
53
54                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
55                 internal extern static ComInteropProxy FindProxy (IntPtr pItf);
56
57                 // Private. Objects must be created with CreateProxy.
58                 ComInteropProxy (Type t)
59                         : base (t)
60                 {
61                         // object only created here
62                         // .ctor is called later
63                         com_object = __ComObject.CreateRCW (t);
64                 }
65
66                 void CacheProxy ()
67                 {
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);
74                 }
75
76                 ComInteropProxy (IntPtr pUnk)
77                         : this (pUnk, typeof (__ComObject))
78                 {
79                 }
80
81                 internal ComInteropProxy (IntPtr pUnk, Type t)
82                         : base (t)
83                 {
84                         com_object = new __ComObject (pUnk);
85                         CacheProxy ();
86                 }
87
88                 internal static ComInteropProxy GetProxy (IntPtr pItf, Type t)
89                 {
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);
98                         }
99                         else {
100                                 Marshal.Release (pItf);
101                                 System.Threading.Interlocked.Increment (ref obj.ref_count);
102                                 return obj;
103                         }
104                 }
105
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)
109                 {
110                         ComInteropProxy proxy = new ComInteropProxy (t);
111                         proxy.com_object.Initialize (t);
112
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;
121                         }
122                         return proxy;
123                 }
124
125                 public override IMessage Invoke (IMessage msg)
126                 {
127                         Console.WriteLine ("Invoke");
128             Console.WriteLine (System.Environment.StackTrace);
129
130                         throw new Exception ("The method or operation is not implemented.");
131                 }
132
133                 public string TypeName
134                 {
135                         get { return type_name; }
136                         set { type_name = value; }
137                 }
138
139                 public bool CanCastTo (Type fromType, object o)
140                 {
141             __ComObject co = o as __ComObject;
142             if (co == null)
143                 throw new NotSupportedException ("Only RCWs are currently supported");
144
145             if ((fromType.Attributes & TypeAttributes.Import) == 0)
146                 return false;
147
148             if (co.GetInterface (fromType, false) == IntPtr.Zero)
149                 return false;
150             
151             return true;
152                 }
153         }
154 }