18adc861d72696c2025fc99b195870ac8de68b7d
[mono.git] / mcs / class / corlib / System / __ComObject.cs
1 //
2 // System.__ComObject
3 //
4 // Authors:
5 //   Sebastien Pouliot <sebastien@ximian.com>
6 //   Kornél Pál <http://www.kornelpal.hu/>
7 //   Jonathan Chambers <joncham@gmail.com>
8 //
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 // Copyright (C) 2005 Kornél Pál
11 // Copyright (C) 2006 Jonathan Chambers
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using Mono.Interop;
36 using System.Collections;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
39
40 namespace System
41 {
42         // This is a private class that is used as a generic wrapper class
43         // for COM objects that have no specific wrapper class.
44         //
45         // It has no public methods, it's functionality is exposed trough
46         // System.Runtime.InteropServices.Marshal class and can be casted to
47         // any interface that is implemented by the wrapped COM object.
48         //
49         // This class is referenced in .NET Framework SDK Documentation so
50         // many times that obj.GetType().FullName == "System.__ComObject" and
51         // Type.GetType("System.__ComObject") may be used.
52
53         internal class __ComObject : MarshalByRefObject
54         {
55                 #region Sync with object-internals.h
56                 IntPtr iunknown;
57                 IntPtr hash_table;
58                 #endregion
59
60                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
61                 internal static extern __ComObject CreateRCW (Type t);
62
63                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64                 private extern void ReleaseInterfaces ();
65
66                 ~__ComObject ()
67                 {
68                         ReleaseInterfaces ();
69                 }
70
71                 public __ComObject ()
72                 {
73                         Type t = GetType ();
74                         int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
75                         Marshal.ThrowExceptionForHR (hr);
76                 }
77
78                 internal __ComObject (Type t)
79                 {
80                         int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
81                         Marshal.ThrowExceptionForHR (hr);
82                 }
83
84                 internal __ComObject (IntPtr pItf)
85                 {
86                         Guid iid = IID_IUnknown;
87                         int hr = Marshal.QueryInterface (pItf, ref iid, out iunknown);
88                         Marshal.ThrowExceptionForHR (hr);
89                 }
90
91                 private static Guid GetCLSID (Type t)
92                 {
93                         if (t.IsImport)
94                                 return t.GUID;
95
96                         // look at supertypes
97                         Type super = t.BaseType;
98                         while (super != typeof (object)) {
99                                 if (super.IsImport)
100                                         return super.GUID;
101                                 super = super.BaseType;
102                         }
103                         throw new COMException ("Could not find base COM type for type " + t.ToString());
104                 }
105
106                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
107                 internal extern IntPtr GetInterfaceInternal (Type t, bool throwException);
108
109                 internal IntPtr GetInterface (Type t, bool throwException) {
110                         CheckIUnknown ();
111                         return GetInterfaceInternal (t, throwException);
112                 }
113
114                 internal IntPtr GetInterface(Type t)
115                 {
116                         return GetInterface (t, true);
117                 }
118
119                 private void CheckIUnknown ()
120                 {
121                         if (iunknown == IntPtr.Zero)
122                                 throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
123                 }
124
125                 internal IntPtr IUnknown
126                 {
127                         get
128                         {
129                                 if (iunknown == IntPtr.Zero)
130                                         throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
131                                 return iunknown;
132                         }
133                 }
134
135                 internal IntPtr IDispatch
136                 {
137                         get
138                         {
139                                 IntPtr pUnk = GetInterface (typeof (IDispatch));
140                                 if (pUnk == IntPtr.Zero)
141                                         throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
142                                 return pUnk;
143                         }
144                 }
145
146                 internal static Guid IID_IUnknown
147                 {
148                         get
149                         {
150                                 return new Guid("00000000-0000-0000-C000-000000000046");
151                         }
152                 }
153
154                 internal static Guid IID_IDispatch
155                 {
156                         get
157                         {
158                                 return new Guid ("00020400-0000-0000-C000-000000000046");
159                         }
160                 }
161
162                 public override bool Equals (object obj)
163                 {
164                         CheckIUnknown ();
165                         if (obj == null)
166                                 return false;
167
168                         __ComObject co = obj as __ComObject;
169                         if ((object)co == null)
170                                 return false;
171                         return (iunknown == co.IUnknown);
172                 }
173
174                 public override int GetHashCode ()
175                 {
176                         CheckIUnknown ();
177                         // not what MS seems to do, 
178                         // but IUnknown is identity in COM
179                         return iunknown.ToInt32 ();
180                 }
181
182                 [DllImport ("ole32.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, PreserveSig = true)]
183                 static extern int CoCreateInstance (
184                    [In, MarshalAs (UnmanagedType.LPStruct)] Guid rclsid,
185                    IntPtr pUnkOuter,
186                    uint dwClsContext,
187                   [In, MarshalAs (UnmanagedType.LPStruct)] Guid riid,
188                         out IntPtr pUnk);
189         }
190 }