Search code examples
c#comactivextstcon32

How to create an ActiveX control in C#?


I am not able to create a functioning ActiveX control in C#; I have tried following tutorials to do so without success.

I create a sample Class Library project which includes this code:

namespace AACWCSurvey
{
    [ProgId("Prisoner.PrisonerControl")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Class1
    {
        public Class1()
        {
            MessageBox.Show("FIRETRUCK!!!");
        }
    }
}

I then did the following steps:

  1. Properties => Application => Assembly Information => Make Assembly COM-visible
  2. Build => Register for COM interop TRUE (checked)
  3. Make Strong name for assembly (signing)
  4. Build the project
  5. regasm MyDll.dll /tlb /codebase

  6. Can't see Prisoner.PrisonerControl in tstcon32 =(

My OS is WinXP x86.


UPD: it works from VBScript:

Dim objJava
Set objJava = WScript.CreateObject("Prisoner.PrisonerControl")

but it is not visible in tstcon32.


Solution

  • If you read the actual article using the Prisoner.PrisonerControl control a sub key named Control is created inside the key with your control GUID.

    On my machine with the guid {9DEA5F06-E324-31A7-837B-D0F3BDE91423} creating the key

    HKEY_CLASSES_ROOT\CLSID\{9DEA5F06-E324-31A7-837B-D0F3BDE91423}\Control
    

    Make the control appears in tstcon32. And with or without it the ActiveX is usable for javascript

    var x = new ActiveXControl("Prisoner.PrisonerControl");
    

    Actually i had to fight windows on both the javascript execution and registry path to test it on my system because it's an x64 machine but that's another story.