visual-studio-2008installationregistrationccw

How to register a .NET CCW with regasm from a Visual Studio 2008 Setup project


I have a setup project for a .NET Service Application which uses a .NET component which exposes a COM interface (COM callable wrapper / CCW). To get the component working on a target machine, it has to be registered with

regasm.exe /tlb /codebase component.dll

The /tlb switch to generate the typelib is mandatory in this case, otherwise I can't create objects from that assembly.

The question is, how can I configure my Visual Studio 2008 Setup-Project to register this assembly with a call to regasm /tlb ?


Solution

  • You can lose the manual call to regasm.exe by using System.Runtime.InteropServices.RegistrationServices instead:

    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Install(IDictionary stateSaver)
    {
    base.Install(stateSaver);
    
    RegistrationServices regsrv = new RegistrationServices();
    if (!regsrv.RegisterAssembly(GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase))
    {
        throw new InstallException("Failed to register for COM Interop.");
    }
    
    }
    
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
    public override void Uninstall(IDictionary savedState)
    {
    base.Uninstall(savedState);
    
    RegistrationServices regsrv = new RegistrationServices();
    if (!regsrv.UnregisterAssembly(GetType().Assembly))
    {
        throw new InstallException("Failed to unregister for COM Interop.");
    }
    }
    

    This also unregisters the library upon uninstall.