Published: 20 Apr 2007
By: Imran Nathani

An ActiveX control is an object that supports a customizable programmatic interface. Using the methods, events and properties exposed by the control, web developers can automate their web pages to give the functionality which is equivalent to that of a desktop application.


As web application developers, we want to give our users a highly functional application. We want to provide our users with functionality like printing streams, local socket programming ,local threading, cross domain scripting etc., but as we know that due to the disconnected architecture of the Internet and security restrictions of any standard browser, this task becomes difficult. That’s when the ActiveX comes to the rescue. This is mostly for web applications where the users would not be apprehensive about doing a one time installation of the component. Also, in an intranet application these components can be a big boost to the functionality of the application.

Writing ActiveX Class in C#

We will first write an interface called ASignatures which holds the signatures of the methods and properties. These methods and properties can then be accessed via JavaScript at the browser level. By default all members of an interface are abstract and public. The main ActiveX class AClass will inherit from this interface. Above the main ActiveX class we will mention the ClassInterfaceType as AutoDual. This will indicate the type of the interface generated for the main class which will automatically be generated and exposed to the COM. Normally AutoDual is not recommended because it has some versioning limitations. We will use the ClassIntrefaceType as AutoDual because the purpose of this code is instructional. In the main class we will write two methods FName(), and SName() and one property Age. In our example we will return the basic datatypes but this can be implemented for complex datatypes too.

Compiling the ActiveX control

For those who do not know how to compile out of Visual Studio IDE, you need to search for the c# compilercsc.exe in the folder:

Place your AClass.cs file in the folder where the csc.exe exists. Then by command (DOS) interface go to that particular folder and execute the following command:

Registering the Assembly with the client

You can register the assembly in multiple ways of implementation and it mostly depends on the target users. For example, creating a setup file for download or having a self extractor file which could prompt in the browser, depends totally on the functionality requirement. However for our example we would register the assembly by using the command prompt which is the easiest and could be done by a batch file too. Therefore, in same folder as above immediately after the compilation step we execute the following command:

Also we must note that the .NET Framework needs to be installed on the client for registration and working.

Using the ActiveX control

We can then access our newly created ActiveX control via JavaScript. We will simply display the data returned by the methods and property in alert boxes. The below code demonstrates how we can access the properties in the ActiveX control.

This will work like a charm in Internet Explorer but may need an API plugin for other browsers like FireFox or Safari.


In this article you have seen how we can increase the functionality of our web application by implementation of ActiveX controls in C#. The practical applications of ActiveX are limitless especially for graphics and multimedia.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Imran Nathani

Sorry, no bio is available

This author has published 5 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category

C# 4.0 Reflection Programming - Part 2
As introduced in the first article, the most typically-used tools associated with .NET reflection ar...
C# 4.0 Reflection Programming - Part 3
In the previous article, we used the reflection to obtain the information of an assembly, module, ty...
C# 4.0 Reflection Programming - Part 4
In this last article of this series, we will learn what to do with reflection. But before making the...
Understanding and Using Extension Methods
Extension methods were new to C# 3.0. They allow you to add a method to an existing type without hav...
Introduction to C# 3.0 features
C# 3.0 introduced some of very useful features built on top of 2.0. This article explains the usage,...

You might also be interested in the following related blog posts

Designer v Xaml v Code read more
Choose your preferred data layout with RadListView for ASP.NET AJAX read more
VS 2010 Code Intellisense Improvements (VS 2010 and .NET 4.0 Series) read more
Searching and Navigating Code in VS 2010 (VS 2010 and .NET 4.0 Series) read more
Silverlight Toolkit adds DragDrop targets! read more
ASP.NET MVC2 Preview 2: Areas and Routes read more
Ajax Control Toolkit: new controls, bug fixes read more
Behaviors vs Subclassing in Silverlight read more
Telerik Releases New Controls for Silverlight 3 and WPF read more
Q3 2009 Beta released for Telerik RadControls for Silverlight/WPF read more


Subject Author Date
placeholder Automation server cant create object Anon Ymous 5/19/2009 4:47 AM
RE: Automation server cant create object Sonu Kapoor 5/19/2009 7:30 AM
placeholder Passing arrays Jeremy Perrin 9/21/2009 7:56 PM
Will it work on Windows higher OS l Dwipayan Das 12/4/2009 12:28 AM
placeholder Very good to the point example Coremans Stefan 4/21/2010 10:26 AM
Object expected error Rynaard B 6/15/2010 5:31 AM
placeholder Exactly what I needed!!! Luiz Alano Lopes 2/9/2011 2:16 PM
Did you ever figure this problem out? Mohamed Omar 7/28/2008 3:58 PM
placeholder Writing ActiveX in C# Jasdfw Asdfw 5/13/2007 10:48 AM
Its security problem . . 8/22/2008 6:04 AM
placeholder Retun Complex DataTypes Gabriel Salazar 10/27/2008 5:45 PM
No DllRegisterServer John Marcovitz 4/20/2009 12:12 PM
placeholder RE: No DllRegisterServer Nick Langmaid 2/19/2010 7:32 PM
No DLL Register Server John Marcovitz 4/20/2009 12:13 PM

Please login to rate or to leave a comment.