Creating ActiveX control in C#

Something that supports programmatic interface is an ActiveX control object. The control exposes methods, events and properties using which web developers can create web pages which has the functionality equivalent to that of desktop application.

Introduction

The main aim of web application developers is to give users a highly functional application. As developers we want to provide 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. Rescue for this is “ActiveX”. 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 ActiveXInterface 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 ActiveXClass 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 FirstName(), and LastName() and one property Age. In our example we will return the basic datatypes but this can be implemented for complex datatypes too.

To start with follow these steps:

  1. Launch the Visual Studio.
  2. Create New Project
  3. Select Class Library
  4. Give a logical name for project and namespace
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace ActiveXExample
{
    public interface ActiveXInterface
    {
        string FirstName();
        string LastName();
        int Age { get; }
    }

    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class ActiveXClass : ActiveXInterface
    {
        public string FirstName()
        {
            return "Dhiraj";
        }
        public string LastName()
        {
            return "Ranka";
        }
        public int Age
        {
            get { return 24; }
        }
    }
}

Compiling the ActiveX control

There are two ways one can compile or make the DLL

  1. Build your solution in Visual Studio and make the DLL
  2. For those who love command tools can compile the code using c# compiler "csc.exe" which can be found in following folder:
    WINDOWSMicrosoft.NETFrameworkv2.0.xxxxx

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

    csc /t:library ActiveXClass.cs

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:

regasm ActiveXClass.dll /tlb /codebase

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.

<html>
<head>
<script language="javascript">
<!-- Load the ActiveX object  -->
var x = new ActiveXObject("ActiveXExample.ActiveXClass");

<!-- Access the Method -->
alert(x.FirstName());
alert(x.LastName());

<!-- Access the Property -->
alert(x.Age);
</script>
</head>
<body>
</body>
</html>

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

In case if you get an error in Internet Explorer which look like this

Automation server can’t create object

Then you have to perform some additional steps to resolve this.

Goto Internet Explorer -> Tools -> Internet Options

Goto Security tab -> Select “Local Intranet” -> Default value is “Medium-Low”

local intranet default

local intranet with default value

Set to “Low”

local intranet low

local intranet with low value

This should solve the problem.

Summary

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.

Tagged with: ,
  • Beneficial info and excellent design you got here! I want to thank you for sharing your ideas and putting the time into the stuff you publish! Great work!

  • Fantastic Post. I have read a lot of posts on this subject and you done the best job. Keep it up!