+2007-07-06 Jonathan Chambers <joncham@gmail.com>
+
+ * ExtensibleClassFactory.cs: Fix ExtensibleClassFactory.
+
2007-06-22 Jonathan Chambers <joncham@gmail.com>
- * __ComObject.cs (ThrowExceptionForHR): Implement/consolidate.
- * __ComObject.cs (GetExceptionForHR): Implement/consolidate.
+ * Marshal.cs (ThrowExceptionForHR): Implement/consolidate.
+ * Marshal.cs (GetExceptionForHR): Implement/consolidate.
2007-05-11 Jonathan Chambers <joncham@gmail.com>
//
using System.Diagnostics;
-using System.Threading;
using System.Collections;
+using System.Reflection;
namespace System.Runtime.InteropServices
{
return hashtable[t] as ObjectCreationDelegate;
}
- public static void RegisterObjectCreationCallback (ObjectCreationDelegate callback)
- {
+ public static void RegisterObjectCreationCallback (ObjectCreationDelegate callback) {
+ int i = 1;
StackTrace trace = new StackTrace (false);
- StackFrame frame = trace.GetFrame (0);
- hashtable.Add (frame.GetMethod ().DeclaringType, callback);
+ while (i < trace.FrameCount) {
+ StackFrame frame = trace.GetFrame (i);
+ MethodBase m = frame.GetMethod ();
+ if (m.MemberType == MemberTypes.Constructor && m.IsStatic) {
+ hashtable.Add (m.DeclaringType, callback);
+ return;
+ }
+ i++;
+ }
+ throw new System.InvalidOperationException (
+ "RegisterObjectCreationCallback must be called from .cctor of class derived from ComImport type.");
}
}
}
public __ComObject ()
{
Type t = GetType ();
- int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
- Marshal.ThrowExceptionForHR (hr);
+ ObjectCreationDelegate ocd = ExtensibleClassFactory.GetObjectCreationCallback (t);
+ if (ocd != null) {
+ iunknown = ocd (IntPtr.Zero);
+ if (iunknown == IntPtr.Zero)
+ throw new COMException (string.Format("ObjectCreationDelegate for type {0} failed to return a valid COM object", t));
+ } else {
+ int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
+ Marshal.ThrowExceptionForHR (hr);
+ }
}
- internal __ComObject (Type t)
- {
- int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
- Marshal.ThrowExceptionForHR (hr);
+ internal __ComObject (Type t) {
+ ObjectCreationDelegate ocd = ExtensibleClassFactory.GetObjectCreationCallback (t);
+ if (ocd != null) {
+ iunknown = ocd (IntPtr.Zero);
+ if (iunknown == IntPtr.Zero)
+ throw new COMException (string.Format("ObjectCreationDelegate for type {0} failed to return a valid COM object", t));
+ }
+ else {
+ int hr = CoCreateInstance (GetCLSID (t), IntPtr.Zero, 0x1 | 0x4 | 0x10, IID_IUnknown, out iunknown);
+ Marshal.ThrowExceptionForHR (hr);
+ }
}
internal __ComObject (IntPtr pItf)