6

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 ?

5 Answers 5

14

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.

3
  • 1
    What should this code be added to? I have a VB.net project which needs the regasm and has some C# dependencies. These two methods can be added to a class which implements which superclass or interface?
    – Amala
    Apr 25, 2012 at 16:13
  • I see these two methods go into a class which overrides System.Configuration.Install.Installer, then this single class is added as a primary output? I am not sure how to add this class or these actions to the installer. Will they automatically get added with the assembly or do I need to add an action?
    – Amala
    Apr 25, 2012 at 16:19
  • "I am not sure how to add this class or these actions to the installer." See the link from Wolfwyrd in another answer, for details on how to add this to your installer package. This answer is a modification of the code in the linked article. Aug 9, 2012 at 23:05
4
  1. In your main project (the one containing the class you want to register), right click the project file and select Add / New Item and select Installer Class. Call it something like clsRegisterDll.cs
  2. In the designer that appears, click 'Click here to switch to code view' or right click the clsRegisterDll.cs file in solution explorer and select View Code
  3. Override the Install, Commit and Uninstall methods adding:

    // Get the location of regasm string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe"; // Get the location of our DLL string componentPath = typeof(RegisterAssembly).Assembly.Location; // Execute regasm
    System.Diagnostics.Process.Start(regasmPath, "/codebase /tlb \"" + componentPath + "\"");

    Swap /codebase /tlb for /u in the uninstall action.

  4. Compile your project
  5. In your installer, make sure you have added your dll to the Application Folder, and then right-click the installer project and select View / Custom Actions
  6. Right-click Install, and then click Add Custom Action
  7. Double click on Application Folder, and then on your dll
  8. Do the same for the Commit action
  9. Build and test your installer

A walkthrough with an actual class for you to try can be found at: http://leon.mvps.org/DotNet/RegasmInstaller.html

0
1

Your service should have an Installer class. Register to the OnAfterInstall event and call RegAsm: the path should be computed from the Windows directory and tied to a specific .Net version.

1

I initially tried running regasm from the installer process (before I saw this posting). Trying to run regasm , and handling all the errors was problematic - even without trying to handle elevated privileges for Windows 7.

Using Runtime.InteropServices.RegistrationServices.RegisterAssembly was much cleaner and provided a much better error trapping.

0

Visual Studio installer makes only COM class registration, but does not make the type library generation and registration (this is what /tlb switch at regasm.exe does) by default. At least in Visual Studio 2017 it is enough to generate the type library in the post-build steps of DLL to be registered using Tlbexp.exe utility.

If the installer project discovers a file with extension .tlb in the same directory and with the same name as the library you are installing, it automatically includes it to the setup project and makes the registration steps during the installation. Of course it is also possible to generate the .tlb file by hand and include it in the setup project (and set its Register property to vsdrfCOM).

Here is a great article about C# and COM interface and the information above come from its section called Deployment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.