In Mono.Interop:
[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 #pragma warning disable 169     
56                 #region Sync with object-internals.h
57                 IntPtr iunknown;
58                 IntPtr hash_table;
59                 #endregion
60 #pragma warning restore 169
61
62                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
63                 internal static extern __ComObject CreateRCW (Type t);
64
65                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
66                 private extern void ReleaseInterfaces ();
67
68                 ~__ComObject ()
69                 {
70                         ReleaseInterfaces ();
71                 }
72
73                 public __ComObject ()
74                 {
75                         Initialize (GetType ());
76                 }
77
78                 internal __ComObject (Type t) {
79                         Initialize (t);
80                 }
81
82                 internal __ComObject (IntPtr pItf)
83                 {
84                         Guid iid = IID_IUnknown;
85                         int hr = Marshal.QueryInterface (pItf, ref iid, out iunknown);
86                         Marshal.ThrowExceptionForHR (hr);
87                 }
88
89                 internal void Initialize (Type t)
90                 {
91                         // Guard multiple invocation.
92                         if (iunknown != IntPtr.Zero)
93                                 return;
94                         
95                         ObjectCreationDelegate ocd = ExtensibleClassFactory.GetObjectCreationCallback (t);
96                         if (ocd != null) {
97                                 iunknown = ocd (IntPtr.Zero);
98                                 if (iunknown == IntPtr.Zero)
99                                         throw new COMException (string.Format("ObjectCreationDelegate for type {0} failed to return a valid COM object", t));
100                         }
101                         else {
102                                 int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
103                                 Marshal.ThrowExceptionForHR (hr);
104                         }
105                 }
106
107                 private static Guid GetCLSID (Type t)
108                 {
109                         if (t.IsImport)
110                                 return t.GUID;
111
112                         // look at supertypes
113                         Type super = t.BaseType;
114                         while (super != typeof (object)) {
115                                 if (super.IsImport)
116                                         return super.GUID;
117                                 super = super.BaseType;
118                         }
119                         throw new COMException ("Could not find base COM type for type " + t.ToString());
120                 }
121
122                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
123                 internal extern IntPtr GetInterfaceInternal (Type t, bool throwException);
124
125                 internal IntPtr GetInterface (Type t, bool throwException) {
126                         CheckIUnknown ();
127                         return GetInterfaceInternal (t, throwException);
128                 }
129
130                 internal IntPtr GetInterface(Type t)
131                 {
132                         return GetInterface (t, true);
133                 }
134
135                 private void CheckIUnknown ()
136                 {
137                         if (iunknown == IntPtr.Zero)
138                                 throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
139                 }
140
141                 internal IntPtr IUnknown
142                 {
143                         get
144                         {
145                                 if (iunknown == IntPtr.Zero)
146                                         throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
147                                 return iunknown;
148                         }
149                 }
150
151                 internal IntPtr IDispatch
152                 {
153                         get
154                         {
155                                 IntPtr pUnk = GetInterface (typeof (IDispatch));
156                                 if (pUnk == IntPtr.Zero)
157                                         throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
158                                 return pUnk;
159                         }
160                 }
161
162                 internal static Guid IID_IUnknown
163                 {
164                         get
165                         {
166                                 return new Guid("00000000-0000-0000-C000-000000000046");
167                         }
168                 }
169
170                 internal static Guid IID_IDispatch
171                 {
172                         get
173                         {
174                                 return new Guid ("00020400-0000-0000-C000-000000000046");
175                         }
176                 }
177
178                 public override bool Equals (object obj)
179                 {
180                         CheckIUnknown ();
181                         if (obj == null)
182                                 return false;
183
184                         __ComObject co = obj as __ComObject;
185                         if ((object)co == null)
186                                 return false;
187                         return (iunknown == co.IUnknown);
188                 }
189
190                 public override int GetHashCode ()
191                 {
192                         CheckIUnknown ();
193                         // not what MS seems to do, 
194                         // but IUnknown is identity in COM
195                         return iunknown.ToInt32 ();
196                 }
197
198                 [DllImport ("ole32.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, PreserveSig = true)]
199                 static extern int CoCreateInstance (
200                    [In, MarshalAs (UnmanagedType.LPStruct)] Guid rclsid,
201                    IntPtr pUnkOuter,
202                    uint dwClsContext,
203                   [In, MarshalAs (UnmanagedType.LPStruct)] Guid riid,
204                         out IntPtr pUnk);
205         }
206 }