Loading Active-X control at run-time in .NET


In VB 6 days we were using VBControlExtender for loading active-x controls at run-time, but in .NET it's totally different. When we drop any active-x control on a windows form, Visual Studio add 'Ax' as suffix to the control name. .NET uses AxHost class behind the scenes to interact with active-x controls. AxHost wraps active-x controls and exposes them as fully featured Windows Forms controls. I will show you how to load 'Microsoft Calendar Control 11.0' control at run-time, how to set it's properties and call it's methods/functions.

By the way, VB.NET is my first choice of programming so I will use it for this post.

First we have to create a class which will inherit from AxHost class. It will also create a constructor for this newly created class which will accept Class GUID as a parameter. So the code goes like this;

Public Class AxControl
Inherits AxHost

Public Sub New(ByVal strCLSID As String)
End Sub
End Class
And below is the code required to load active-x control at run-time;

' Declarations
Dim WithEvents m_axCtrl As AxControl
Dim strProgId As String
Dim m_type As Type
Dim args As Object()

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load

strProgId = "MSCAL.Calendar.7"
m_type = System.Type.GetTypeFromProgID(strProgId, True)
m_axCtrl = New AxControl(m_type.GUID.ToString())

DirectCast((m_axCtrl), System.ComponentModel.ISupportInitialize).BeginInit()

m_axCtrl.Enabled = True
m_axCtrl.Name = "axCtrl"
m_axCtrl.TabIndex = 0
m_axCtrl.Left = 10
m_axCtrl.Top = 10
m_axCtrl.Width = 250
m_axCtrl.Height = 250

DirectCast((m_axCtrl), System.ComponentModel.ISupportInitialize).EndInit()
End Sub
Above code gets the instance of the type from the class name "MSCAL.Calendar.7" which will be used to get the GUID of the active-x. Using this GUID we create the instance of the active-x control. Next we need to initialize the control. BeginInit method signals the control object that initializing is starting. After this we set some required control properties and end the initialization.

So this few lines of code will load the active-x control at run-time. Now, I will show you how to set properties and call methods.

For calling methods and setting properties syntax is like this;

Dim args As Object()

args = New Object() {"Method Parameters"}
m_type.InvokeMember("MethodName", Reflection.BindingFlags.InvokeMethod, Nothing, m_axCtrl.GetOcx, args)
args = New Object() {"PropertyValue"}
m_type.InvokeMember("Property Name", System.Reflection.BindingFlags.SetProperty, Nothing, m_axCtrl.GetOcx, args)
Call method:

m_type.InvokeMember("NextDay", Reflection.BindingFlags.InvokeMethod, Nothing, m_axCtrl.GetOcx, New Object() {})
Set property:

args = New Object() {"01/01/2009"}
m_type.InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, Nothing, m_axCtrl.GetOcx, args)
Get property value:

Dim selDate As Date
selDate = m_type.InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, Nothing, m_axCtrl.GetOcx, New Object() {})
So this is all about loading Active-X control at run-time in .NET, accessing properties and calling it's methods.


Anonymous said...

Hi. Good information here! How would you go about integrating an event that would normally be raised by a given ActiveX using the mechanism you descibe here?


Deepak Sakpal said...

To catch the events raised by the active-x control created at run-time, we need to implement the ICalendar interface in this case.
It's really a complicated task. Using aximp.exe tool, you can generate source code for Calendar control. Type the below command at Visual Studio command prompt.

aximp.exe c:\WINDOWS\system32\MSCAL.OCX /source

It will generate 3 files - MSACAL.dll, AxMSACAL.dll and AxMSACAL.cs. AxMSACAL.cs contains the source code. You can use this file as a guide line.