Reputation: 171
I am creating an installer with VS 2008 containing a desktop application and a BHO plugin which is created as a Class Library project. Manually ,I can register myPlugin.dll using this command [regasm /codebase "myPlugin.dll" register] ,but I do not know how to realize this in the Setup Project.Any idea please?
Upvotes: 3
Views: 5090
Reputation: 44961
The easiest and safest way to do this is to create your own installer class that accepts the name of the DLL to be registered and uses RegistrationServices to install or uninstall your dll.
When you create an Installer, it can accept parameters from custom actions through the CustomActionData property of the custom action. Each of these parameters is stored in the Installer.Context.Parameters collection. This means that by using some installer properties, you can pass the complete, installed path to the DLL, which can then be used by RegistrationServices to perform the same actions as Regasm.
Implementing this is a multi-step process:
Step 1 is to create the installer class. Because this is reusable code, I suggest creating a separate DLL project to hold this class.
using System;
using System.Text;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
[RunInstaller(true)]
public partial class AssemblyRegistrationInstaller : Installer
{
public AssemblyRegistrationInstaller()
{
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
RegisterAssembly(true);
}
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
RegisterAssembly(false);
}
public override void Uninstall(IDictionary savedState)
{
base.Rollback(savedState);
RegisterAssembly(false);
}
private void RegisterAssembly(bool fDoRegistration)
{
string sAssemblyFileName = base.Context.Parameters["AssemblyFileName"];
if (string.IsNullOrEmpty(sAssemblyFileName))
{
throw new InstallException("AssemblyFileName must be specified");
}
if (!File.Exists(sAssemblyFileName))
{
if (fDoRegistration)
{
throw new InstallException(string.Format("AssemblyFileName {0} is missing", sAssemblyFileName));
}
else
{
// Just bail if we are uninstalling and the file doesn't exist
return;
}
}
var oServices = new RegistrationServices();
var oAssembly = Assembly.LoadFrom(sAssemblyFileName);
if (fDoRegistration)
{
oServices.RegisterAssembly(oAssembly, AssemblyRegistrationFlags.SetCodeBase);
}
else
{
try
{
oServices.UnregisterAssembly(oAssembly);
}
catch
{
// Just eat the exception so that uninstall succeeds even if there is an error
}
}
}
}
Step 2 is to add the new DLL to the setup project. Since we need to configure settings for the DLL, do not add the project itself; add the DLL directly from the release directory of the project.
Step 3 is to add the new DLL as a custom action. To do this, right-click on the setup project and select View
, then Custom Actions
.
Right-click on the Custom Actions
menu and, in the popup dialog, select the new installer DLL. This will automatically add the DLL to all 4 sections. Right-click on the one that was added to the Commit
section and delete it.
For each of the other 3 sections, perform the following actions:
Right-click on the DLL entry and select Properties Window
.
Ensure that the InstallerClass
entry in the properties grid is set to true (it should be).
Add the following entry to the CustomActionData
property to pass the name of the dll to be registered to the custom installer class:
/AssemblyFileName="[TARGETDIR]\myplugin.dll"
Upvotes: 2