How to register a .NET CCW with regasm from a Visual Studio 2008 Setup project
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.
- 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
- 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
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.
- Compile your project
- 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
- Right-click Install, and then click Add Custom Action
- Double click on Application Folder, and then on your dll
- Do the same for the Commit action
- 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