Create an ActiveX using a Csharp Usercontrol

This article is about creating ActiveX controls using a DotNet Usercontrol in Csharp. You can design all ActiveX features like: properties, methods and events.

Environment:  Visual Studio(2005) Csharp

How to Do This

1. Create a Usercontrol using Visual Studio (C#):

2. Configure the project properties:

3. Modify the usercontrol interface:

Note: Make sur you added the EVENTS class:   [ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(UserControlEvents))]
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Drawing;
using
System.Data;
using
System.Text;
using
System.Windows.Forms;
// Add references
using System.Runtime.InteropServices;
using
System.Reflection;
using Microsoft.Win32;

namespace CsharpWindowsActiveX
{

            [ProgId("CsharpWindowsActiveX.ActiveXUserControl")]
            [
ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(UserControlEvents))]

            public partial class ActiveXUserControl : UserControl
           
{

                    public ActiveXUserControl()
                    {
                         InitializeComponent();
                    }

                     .....

 

4. Add the register/unregister section in the source code

// register COM ActiveX object

[ComRegisterFunction()]
public static void RegisterClass(string key)
{
               StringBuilder skey = new StringBuilder(key);
              skey.Replace(
@"HKEY_CLASSES_ROOT\", "");
             
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
             
RegistryKey ctrl = regKey.CreateSubKey("Control");
             ctrl.Close();
             
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
              inprocServer32.SetValue(
"CodeBase", Assembly.GetExecutingAssembly().CodeBase);
              inprocServer32.Close();
              regKey.Close();
}


[ComUnregisterFunction()]
public static void UnregisterClass(string key)
{
              
StringBuilder skey = new StringBuilder(key);
               skey.Replace(
@"HKEY_CLASSES_ROOT\", "");
              
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
               regKey.DeleteSubKey(
"Control", false);
              
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
               regKey.DeleteSubKey(
"CodeBase", false);
               regKey.Close();
}

 

 

5. Add an ActiveX property:

// ActiveX properties (Get/Set) //////////////////////////////////////////////////////////////////

private int ptextVal;
public int TextVal
{
      
get
       {
                  ptextVal = (
int)(numericUpDown1.Value);
                 
return ptextVal;
        }
       
se
t
       {

            ptextVal = value;
                numericUpDown1.Value = ptextVal;
         }
}

 

 

 

6. Add an ActiveX method:

// ActiveX methods/functions //////////////////////////////////////////////////////////////////

public interface ICOMCallable


{
            
int GetTextBoxValue();
}

public int GetTextBoxValue()
{
            
int i = (int)(numericUpDown1.Value);
            
MessageBox.Show("ActiveX method: GetTextBoxValue " + i.ToString());
            
return (i);
}

 

 

7. Add an ActiveX event:

// Eventhandler interface //////////////////////////////////////////////////////////////////
 

public delegate void ControlEventHandler(int NumVal);
[Guid("0A415E38-372F-45fb-813B-D9558C787EA0")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
 

public interface UserControlEvents
{
               [DispId(0x60020001)]
               void OnButtonClick(int NumVal);
}
 

public event ControlEventHandler OnButtonClick;
 

private void buttonOK_Click(object sender, EventArgs e)
{

        int NumVal;
        if (OnButtonClick != null)
        {
                 NumVal = (int)(numericUpDown1.Value);
                 OnButtonClick(NumVal);
         }
}

 

 

 

8. Register the ActiveX on your PC:

Register the new ActiveX on your computer using the command:  RegAsm.exe CsharpWindowsActiveX.dll

9. Test your ActiveX:

Use the TSTCon32.exe tool from Visul Studio to test the ActiveX:

 



Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds