Regasm /codebase for an installer project
OK, so you've spent ages making a little Browser Helper Object (BHO) for
Internet Explorer, and you've tested it on many other machines by registering it
using regasm like so:
regasm /codebase your.dll
So then you decide the time's right to bundle your code into an installer for
mass consumption. You now spend then next week trying to work out how to get
your dll registered on the target machine before you go nuts or rip too much of
your hair out. Welcome to my world.
I followed countless articles on the web which informed me of the basic
pitfalls when trying to register a .NET dll on the target machine using a Visual
Studio setup project. These included:
- Making sure you do not use project references when adding your files to
the installer. Use direct file references instead
- Do the same for referenced files that are included automatically, as
they do not get dropped in the obj folder and cause problems. Just exclude
them from the project and then add them manually to the installer output
- Make sure you have a type library for your dll and that this is included
in the installer and set to register for COM
- Make sure your dll is set to register for COM (duh)
Still nothing. Nowt.
So in the end I got fed up and just created a custom action which will locate
regasm in the latest version of the .NET framework that is installed on the
target machine and run it against the newly installed dll. Hurrah! It works
perfectly!
Realising that I couldn't be the only person who's having this problem, I've
decided to share the code to assist others. All you need to do is follow the
below steps:
- 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
- Replace the code in the window with the code listed below, and make sure
you change 'YourNamespace'. This has to
match with the namespace in the clsRegisterDll.Designer.cs file.
- 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
You should now have an installer that registers your dll using regasm /codebase.
clsRegisterDll.cs:
using System.ComponentModel;
using System.Configuration.Install;
namespace YourNamespace
{
[RunInstaller(true)]
public partial class RegisterDll : Installer
{
public RegisterDll()
{
InitializeComponent();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Commit(System.Collections.IDictionary savedState)
{
base.Commit(savedState);
// Get the location of regasm
string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";
// Get the location of our DLL
string componentPath = typeof(RegisterDll).Assembly.Location;
// Execute regasm
System.Diagnostics.Process.Start(regasmPath, "/codebase \"" + componentPath + "\"");
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
}
}
}
Home