DSS
DSS

Reputation: 7259

How to create activex control in c#.net windows form and how to deploy

I have created an activeX control but i am facing an issue when it comes to deploying it in Internet Explorer. The browser (IE 11) is not able to download the activeX control. I am not sure what is wrong or what piece of code might not be working well. I am using .net 2010, framekwork 4.0

This is the code that i have tried.

[ProgId("Newcomp.UserControl1")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("5FE8E181-7D6D-4CE2-AB83-BAEB9906EF48")]
[ComVisible(true)]
public partial class UserControl1: UserControl
{

    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.BackColor = Color.YellowGreen;
    }
    /// 
    /// Register the class as a control and set it's CodeBase entry
    /// 
    /// The registry key of the control
    [ComRegisterFunction()]
    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;
        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();

    }

    /// 
    /// Called to unregister the control
    /// 
    /// Tke registry key
    [ComUnregisterFunction()]
    public static void UnregisterClass(string key)
    {
        StringBuilder sb = new StringBuilder;
        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();
    }
}

I have also followed this

Upvotes: 1

Views: 5107

Answers (1)

DaniDev
DaniDev

Reputation: 2631

The problem you experiencing is caused by the fact that, by default, IE10 (and onward) will not let you download/run Active X controls, in its default configuration. This was done because of security considerations. In other words ActiveX controls where being used maliciously.

Unfortunately, the ‘How To’ article (while accurate) you used: https://blogs.msdn.microsoft.com/asiatech/2011/12/05/how-to-develop-and-deploy-activex-control-in-c/ , predates this default restriction, and so neglects to warn the user about it.

Microsoft provides a way to disable this filtering for AciveX in IE10 and IE11: https://support.microsoft.com/en-us/help/17469/windows-internet-explorer-use-activex-controls

this solution, however, requires action by the end user, and there is no way for the programmer to force this option, other than to advice the end-user about it. This can work for an intranet app with a small audience, but becomes unmanageable for a wider audience.

Upvotes: 2

Related Questions