Click here to Skip to main content
13,410,869 members (31,471 online)
Click here to Skip to main content
Add your own
alternative version


176 bookmarked
Posted 17 Jul 2001

Exposing Windows Forms Controls as ActiveX controls

, 27 Aug 2001
Rate this:
Please Sign up or sign in to vote.
This article describes how to build a Windows Forms control in C# and expose it as an ActiveX control


This article will describe how to utilise Windows Forms controls outside of .NET. In a recent MSDN magazine article on .NET Interop available here, various ways of exposing .NET objects to 'legacy' environments are discussed, including the exposure of Windows Forms controls as ActiveX controls.

The problem is that the goalposts have moved since the article was written as Beta 2 is now available, and unfortunately this support has been removed - see this posting on the .NET list at

The following image shows a control, written purely within .NET, hosted within an ActiveX control container - in this instance tstcon32.exe.

As Beta1 supported this facility, and being somewhat inquisitive, I decided to see if I could find a way to expose controls anyway. The attached project creates the 'Prisoner' control, which won't set the world on fire but does show the main things you need to do in order to get a .NET control up & running within VB6.

CAVEAT: As this support has been dropped from Beta2 of .NET, don't blame me if it fries your PC or toasts the cat.

Now that's out of the way, how's it done?.

Writing the control

  1. Create a new control project from within Visual Studio - my examples are all in C# but VB.NET could also be used.

  2. Add controls etc to the form, put in the code etc.

  3. Add in the following using clauses...

    using System.Runtime.InteropServices;
    using System.Text;
    using System.Reflection;
    using Microsoft.Win32;
  4. Attribute your class so that it gets a ProgID. This isn't strictly necessary as one will be generated, but it's almost always best to be explicit.

    This assigns the ProgID, and also defines that the interface exposed should be 'AutoDual' - this crufts up a default interface for you from all public, non-static members of the class. If this isn't what you want, use one of the other options.

  5. Update the project properties so that your assembly is registered for COM interop.

    If you're using VB.NET, you also need a strong named assembly. Curiously in C# you don't - and it seems to be a feature of the environment rather than a feature of the compiler or CLR.

  6. Add the following two methods into your class.

    public static void RegisterClass ( string key )
      // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
      StringBuilder sb = new StringBuilder ( key ) ;
      sb.Replace(@"HKEY_CLASSES_ROOT\","") ;
      // Open the CLSID\{guid} key for write access
      RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);
      // And create the 'Control' key - this allows it to show up in 
      // the ActiveX control container 
      RegistryKey ctrl = k.CreateSubKey ( "Control" ) ; 
      ctrl.Close ( ) ;
      // Next create the CodeBase entry - needed if not string named and GACced.
      RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ; 
      inprocServer32.SetValue ( "CodeBase" , Assembly.GetExecutingAssembly().CodeBase ) ; 
      inprocServer32.Close ( ) ;
      // Finally close the main key
      k.Close ( ) ;
    The RegisterClass function is attributed with ComRegisterFunction - this static method will be called when the assembly is registered for COM Interop. All I do here is add the 'Control' keyword to the registry, plus add in the CodeBase entry.

    CodeBase is interesting - not only for .NET controls. It defines a URL path to where the code can be found, which could be an assembly on disk as in this instance, or a remote assembly on a web server somewhere. When the runtime attempts to create the control, it will probe this URL and download the control as necessary. This is very useful when testing .NET components, as the usual caveat of residing in the same directory (etc) as the .EXE does not apply.

    public static void UnregisterClass ( string key )
      StringBuilder sb = new StringBuilder ( key ) ;
      sb.Replace(@"HKEY_CLASSES_ROOT\","") ;
      // Open HKCR\CLSID\{guid} for write access
      RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);
      // Delete the 'Control' key, but don't throw an exception if it does not exist
      k.DeleteSubKey ( "Control" , false ) ;
      // Next open up InprocServer32
      RegistryKey inprocServer32 = k.OpenSubKey ( "InprocServer32" , true ) ;
      // And delete the CodeBase key, again not throwing if missing 
      k.DeleteSubKey ( "CodeBase" , false ) ;
      // Finally close the main key 
      k.Close ( ) ;

    The second function will remove the registry entries added when (if) the class is unregistered - it's always a good suggestion to tidy up as you go.

Now you are ready to compile & test your control.

Testing the Control

For this example I have chosen tstcon32.exe, which is available with the installation of .NET. The main reason I've used this rather than VB6 is that I don't have VB6 anymore.

Inserting the Control

First up you need to insert your control, so to do that choose Edit -> Insert New Control, and choose your control from the dropdown...

This will result in a display as shown at the top of the article, if you're following along with my example code.

Testing Methods

The example control only includes one method, 'Question'. To test this within TstCon32, choose Control -> InvokeMethods from the menu, and select the method you want to call. Note that because I defined the interface as AutoDual, I get gazillions of methods. If you implement an interface and expose this as the default interface then the list of methods will be more manageable.

Click on the 'Invoke' button will execute the method, which in this instance displays the obligatory message box.

Wrap Up

Dropping support for creating ActiveX controls from Windows Forms controls is a pain, and one decision I wish Microsoft had not made.

This article presents one way of exposing .NET controls as ActiveX controls, and seems to work OK. Having said that, I've not exhaustively tested this and who knows what bugs might be lurking in there. I haven't delved into events yet, nor property change notifications, so there's some fun to be had there if you like that sort of thing.

The .NET framework truly is the best thing since sliced bread, but the lack of support for creating ActiveX controls from Windows Forms controls is inconvenient. There are many applications out there (ours included) which can be extended with ActiveX controls. It would be nice given the rest of the support in the framework to be able to expose Windows Forms controls to ActiveX containers, and maybe someday the support will be available.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Morgan Skinner
Founder MS Application Development Consulting Ltd
United Kingdom United Kingdom
I started work in 1989 for LabSystems, based in Altrincham. I had originally expected to stay for six months, but the job was so good I stayed for 12.5 years.

In December 2001 I joined Microsoft as an Application Development Consultant, where I spent lots of time assisting customers with their use of Microsoft technologies.

I left in June 2011 to start my own business, and I do contract development, knowledge sharing and bespoke application development.

You may also be interested in...


Comments and Discussions

Questionhave application like this but how do I dispose or free up the resource when running in internet explorer? Pin
Member 1009616625-Feb-16 11:40
memberMember 1009616625-Feb-16 11:40 
GeneralThank you for this post! Pin
GromRom11-Mar-11 2:48
memberGromRom11-Mar-11 2:48 
GeneralCompiling vs Registering dll Pin
Mendocina6-Jan-11 23:03
memberMendocina6-Jan-11 23:03 
Generalregistering the dll on a different machine Pin
snow_cap2-Nov-10 4:21
membersnow_cap2-Nov-10 4:21 
GeneralRe: registering the dll on a different machine Pin
Xavibel24-Nov-10 3:11
memberXavibel24-Nov-10 3:11 
Questionhello there - could u plz tell me how do u test this sample ? Pin
Member 13031157-Mar-10 16:43
memberMember 13031157-Mar-10 16:43 
AnswerRe: hello there - could u plz tell me how do u test this sample ? Pin
Dan Neely2-Jul-10 9:38
memberDan Neely2-Jul-10 9:38 
QuestionHow can I detect UserControl with Acitvated & Deactivated event? Pin
Liang-Kuan Hu17-May-08 17:30
memberLiang-Kuan Hu17-May-08 17:30 
AnswerRe: How can I detect UserControl with Acitvated & Deactivated event? Pin
Jared Nathan Drake21-Sep-12 0:05
memberJared Nathan Drake21-Sep-12 0:05 
NewsGreat news folks - using .NET inside VB6 applications is *now supported* Pin
Morgan Skinner9-Jan-08 12:32
memberMorgan Skinner9-Jan-08 12:32 
GeneralBug In UnRegisterClass Pin
waynebe25-Sep-07 4:30
memberwaynebe25-Sep-07 4:30 
GeneralGood Start, but Much More Is Needed... Pin
S. Sean Stagner6-Jul-07 1:34
memberS. Sean Stagner6-Jul-07 1:34 
QuestionRe: Good Start, but Much More Is Needed... Pin
leolca11-Sep-07 3:49
memberleolca11-Sep-07 3:49 
AnswerRe: Good Start, but Much More Is Needed... Pin
TOMTEFAR66610-Jun-10 5:28
memberTOMTEFAR66610-Jun-10 5:28 
GeneralRe: Good Start, but Much More Is Needed... Pin
Mendocina7-Jan-11 0:18
memberMendocina7-Jan-11 0:18 
GeneralRe: Good Start, but Much More Is Needed... Pin
TOMTEFAR66617-Jan-11 3:57
memberTOMTEFAR66617-Jan-11 3:57 
GeneralVB6 does not like it Pin
Jeltz113-May-07 14:27
memberJeltz113-May-07 14:27 
QuestionRe: VB6 does not like it Pin
controlrisks4-Jul-07 22:55
membercontrolrisks4-Jul-07 22:55 
AnswerRe: VB6 does not like it Pin
Chandra Bhoga23-Sep-07 23:20
memberChandra Bhoga23-Sep-07 23:20 
GeneralRe: VB6 does not like it Pin
kipelovets13-Nov-07 4:13
memberkipelovets13-Nov-07 4:13 
GeneralAuto Direct .NeT Framework Deployment Pin
Vasudevan Deepak Kumar8-Mar-07 6:45
memberVasudevan Deepak Kumar8-Mar-07 6:45 
QuestionASSERT when cretaed dynamically in MFC Pin
DomoG21-Sep-06 23:06
memberDomoG21-Sep-06 23:06 
GeneralRe: ASSERT when cretaed dynamically in MFC Pin
warfric28-Sep-06 11:21
memberwarfric28-Sep-06 11:21 
GeneralRe: ASSERT when cretaed dynamically in MFC Pin
DomoG23-Nov-06 0:42
memberDomoG23-Nov-06 0:42 
GeneralRe: ASSERT when cretaed dynamically in MFC Pin
trinity7917-Jan-07 6:17
membertrinity7917-Jan-07 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180225.1 | Last Updated 28 Aug 2001
Article Copyright 2001 by Morgan Skinner
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid