5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Kornél Pál <http://www.kornelpal.hu/>
7 // Jonathan Chambers <joncham@gmail.com>
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 // Copyright (C) 2005 Kornél Pál
11 // Copyright (C) 2006 Jonathan Chambers
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
36 using System.Collections;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
42 // This is a private class that is used as a generic wrapper class
43 // for COM objects that have no specific wrapper class.
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.
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.
53 internal class __ComObject : MarshalByRefObject
55 #region Sync with object-internals.h
61 static bool coinitialized;
63 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64 internal static extern __ComObject CreateRCW (Type t);
66 [MethodImplAttribute (MethodImplOptions.InternalCall)]
67 private static extern void AddInterface (__ComObject co, Type t, IntPtr pItf);
69 [MethodImplAttribute (MethodImplOptions.InternalCall)]
70 private static extern IntPtr FindInterface (__ComObject co, Type t);
72 [MethodImplAttribute (MethodImplOptions.InternalCall)]
73 private extern void ReleaseInterfaces ();
82 // call CoInitialize once per thread
84 CoInitialize (IntPtr.Zero);
89 int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
90 Marshal.ThrowExceptionForHR (hr);
93 internal __ComObject (Type t)
95 // call CoInitialize once per thread
97 CoInitialize (IntPtr.Zero);
101 int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
102 Marshal.ThrowExceptionForHR (hr);
105 internal __ComObject (IntPtr pItf)
107 Guid iid = IID_IUnknown;
108 int hr = Marshal.QueryInterface (pItf, ref iid, out iunknown);
109 Marshal.ThrowExceptionForHR (hr);
112 private static Guid GetCLSID (Type t)
117 // look at supertypes
118 Type super = t.BaseType;
119 while (super != typeof (object)) {
122 super = super.BaseType;
124 throw new COMException ("Could not find base COM type for type " + t.ToString());
127 internal IntPtr GetInterface(Type t)
130 IntPtr pItf = FindInterface (this, t);
131 if (pItf != IntPtr.Zero) {
137 int hr = Marshal.QueryInterface (iunknown, ref iid, out ppv);
138 Marshal.ThrowExceptionForHR (hr);
139 AddInterface (this, t, ppv);
143 private void CheckIUnknown ()
145 if (iunknown == IntPtr.Zero)
146 throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
149 internal IntPtr IUnknown
153 if (iunknown == IntPtr.Zero)
154 throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
159 internal IntPtr IDispatch
163 IntPtr pUnk = GetInterface (typeof (IDispatch));
164 if (pUnk == IntPtr.Zero)
165 throw new InvalidComObjectException ("COM object that has been separated from its underlying RCW cannot be used.");
170 internal static Guid IID_IUnknown
174 return new Guid("00000000-0000-0000-C000-000000000046");
178 internal static Guid IID_IDispatch
182 return new Guid ("00020400-0000-0000-C000-000000000046");
186 public override bool Equals (object obj)
192 __ComObject co = obj as __ComObject;
193 if ((object)co == null)
195 return (iunknown == co.IUnknown);
198 public override int GetHashCode ()
201 // not what MS seems to do,
202 // but IUnknown is identity in COM
203 return iunknown.ToInt32 ();
206 [DllImport ("ole32.dll", CallingConvention = CallingConvention.StdCall)]
207 static extern int CoInitialize (IntPtr pvReserved);
209 [DllImport ("ole32.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, PreserveSig = true)]
210 static extern int CoCreateInstance (
211 [In, MarshalAs (UnmanagedType.LPStruct)] Guid rclsid,
214 [In, MarshalAs (UnmanagedType.LPStruct)] Guid riid,