Create ActiveX object
authorAlistair Leslie-Hughes <leslie_alistair@hotmail.com>
Tue, 4 Dec 2012 03:14:17 +0000 (14:14 +1100)
committerAlistair Leslie-Hughes <leslie_alistair@hotmail.com>
Fri, 18 Jan 2013 07:33:52 +0000 (18:33 +1100)
mcs/class/Managed.Windows.Forms/System.Windows.Forms/AxHost.cs

index 40804df5b0878c1c5b286b97078fe5fba9ee3f62..34c96a063db046cc8df0ef0f3eaa21a25655cc1b 100644 (file)
@@ -196,23 +196,23 @@ namespace System.Windows.Forms {
                #endregion      // AxHost Subclasses
 
                //private int flags;
-               //private Guid clsid;
+               private Guid clsid;
+               private object instance;
                private AboutBoxDelegate aboutDelegate = null;
                private AxHost.State ocxState = null;
 
                #region Protected Constructors
 
-               [MonoTODO]
                protected AxHost (string clsid) : this(clsid, 0)
                {
 
                }
 
-               [MonoTODO]
                protected AxHost (string clsid, int flags)
                {
-                       //this.clsid = new Guid(clsid);
+                       this.clsid = new Guid(clsid);
                        //this.flags = flags;
+                       this.instance = null;
                }
                #endregion      // Public Instance Properties
 
@@ -518,7 +518,7 @@ namespace System.Windows.Forms {
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public object GetOcx ()
                {
-                       throw new NotImplementedException("COM/ActiveX support is not implemented");
+                       return instance;
                }
                
                public bool HasPropertyPages ()
@@ -563,13 +563,26 @@ namespace System.Windows.Forms {
                #region Protected Instance Methods
                protected virtual void AttachInterfaces ()
                {
-                       throw new NotImplementedException("COM/ActiveX support is not implemented");
+
                }
                
                protected override void CreateHandle ()
                {
-                       if(!base.IsHandleCreated)
-                               base.CreateHandle();
+                       if(!base.IsHandleCreated) {
+                               GetActiveXInstance ();
+                               AttachInterfaces ();
+
+                               base.CreateHandle ();
+                       }
+               }
+
+               private void GetActiveXInstance()
+               {
+                       if (this.instance == null) {
+                               object obj;
+                               CoCreateInstance (ref clsid, null, 1, ref IID_IUnknown, out obj);
+                               this.instance = obj;
+                       }
                }
 
                protected virtual object CreateInstanceCore (Guid clsid)
@@ -577,6 +590,7 @@ namespace System.Windows.Forms {
                        throw new NotImplementedException("COM/ActiveX support is not implemented");
                }
 
+
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                protected virtual void CreateSink ()
                {
@@ -596,6 +610,11 @@ namespace System.Windows.Forms {
 
                protected override void Dispose (bool disposing)
                {
+                       if(disposing) {
+                               if(this.instance != null)
+                                       Marshal.ReleaseComObject (this.instance);
+                               this.instance = null;
+                       }
                        base.Dispose(disposing);
                }
 
@@ -1083,5 +1102,16 @@ namespace System.Windows.Forms {
                        throw new NotImplementedException("COM/ActiveX support is not implemented");
                }
                #endregion      // Interfaces
+
+               static Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
+
+               [DllImport("ole32.dll")]
+               static extern int CoCreateInstance (
+                       [In] ref Guid rclsid,
+                       [In, MarshalAs (UnmanagedType.IUnknown)] object pUnkOuter,
+                       [In] uint dwClsContext,
+                       [In] ref Guid riid,
+                       [Out, MarshalAs (UnmanagedType.Interface)] out object ppv);
+
        }
 }