Tuesday, October 20, 2020

Regasm in setup project

 This post is a reminder to myself (and anyone) about how to register a COM visible .NET assembly in a Visual Studio Setup Project (vdproj). Countless pages describe how to register traditional COM server libraries, but it took hours to stumble upon a tiny clue that lead me to the code below that can be placed in a CA (Custom Action) class to register a COM visible assembly.

The rather obscure RegistrationServices class seems to contain the core functionality of the regasm command, so it can be called from inside the CA during install and uninstall processing to register and unregister the library.

[RunInstaller(true)]
public partial class ComCA : Installer
{
	public ComCA()
	{
		InitializeComponent();
	}
	public override void Install(IDictionary stateSaver)
	{
		base.Install(stateSaver);
		var regsrv = new RegistrationServices();
		var asm = GetType().Assembly;
		if (!regsrv.RegisterAssembly(asm, AssemblyRegistrationFlags.SetCodeBase))
		{
			throw new InstallException($"Failed to register for COM Interop: {asm.FullName}");
		}
	}
	public override void Uninstall(IDictionary savedState)
	{
		base.Uninstall(savedState);
		var regsrv = new RegistrationServices();
		var asm = GetType().Assembly;
		if (!regsrv.UnregisterAssembly(GetType().Assembly))
		{
			throw new InstallException($"Failed to unregister for COM Interop: {asm.FullName}");
		}
	}
}

No comments:

Post a Comment