[sgen] Fix logging of major heap size with concurrent sweep
[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 #if !FULL_AOT_RUNTIME
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         [StructLayout (LayoutKind.Sequential)]
44         internal class ComInteropProxy : RealProxy, IRemotingTypeInfo
45     {
46         #region Sync with object-internals.h
47                 private __ComObject com_object;
48                 int ref_count = 1; // wrapper ref count
49         #endregion
50                 private string type_name;
51
52                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
53                 private extern static void AddProxy (IntPtr pItf, ComInteropProxy proxy);
54
55                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
56                 internal extern static ComInteropProxy FindProxy (IntPtr pItf);
57
58                 // Private. Objects must be created with CreateProxy.
59                 ComInteropProxy (Type t)
60                         : base (t)
61                 {
62                         // object only created here
63                         // .ctor is called later
64                         com_object = __ComObject.CreateRCW (t);
65                 }
66
67                 void CacheProxy ()
68                 {
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);
73                         else
74                                 System.Threading.Interlocked.Increment (ref ref_count);
75                 }
76
77                 ComInteropProxy (IntPtr pUnk)
78                         : this (pUnk, typeof (__ComObject))
79                 {
80                 }
81
82                 internal ComInteropProxy (IntPtr pUnk, Type t)
83                         : base (t)
84                 {
85                         com_object = new __ComObject (pUnk);
86                         CacheProxy ();
87                 }
88
89                 internal static ComInteropProxy GetProxy (IntPtr pItf, Type t)
90                 {
91                         IntPtr ppv;
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);
96                         if (obj == null) {
97                                 Marshal.Release (ppv);
98                                 return new ComInteropProxy (ppv);
99                         }
100                         else {
101                                 Marshal.Release (ppv);
102                                 System.Threading.Interlocked.Increment (ref obj.ref_count);
103                                 return obj;
104                         }
105                 }
106
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)
110                 {
111                         ComInteropProxy proxy = new ComInteropProxy (t);
112                         proxy.com_object.Initialize (t);
113
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 ();
119                                 if (cachedType != t)
120                                         throw new InvalidCastException (String.Format ("Unable to cast object of type '{0}' to type '{1}'.", cachedType, t));
121                                 return cachedProxy;
122                         }
123                         return proxy;
124                 }
125
126                 public override IMessage Invoke (IMessage msg)
127                 {
128                         Console.WriteLine ("Invoke");
129             Console.WriteLine (System.Environment.StackTrace);
130
131                         throw new Exception ("The method or operation is not implemented.");
132                 }
133
134                 public string TypeName
135                 {
136                         get { return type_name; }
137                         set { type_name = value; }
138                 }
139
140                 public bool CanCastTo (Type fromType, object o)
141                 {
142             __ComObject co = o as __ComObject;
143             if (co == null)
144                 throw new NotSupportedException ("Only RCWs are currently supported");
145
146             if ((fromType.Attributes & TypeAttributes.Import) == 0)
147                 return false;
148
149             if (co.GetInterface (fromType, false) == IntPtr.Zero)
150                 return false;
151             
152             return true;
153                 }
154         }
155 }
156 #endif