Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a .NET CCW with regasm from a Visual Studio 2008 Setup project

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 ?

like image 295
Jan Avatar asked Oct 27 '08 09:10

Jan


1 Answers

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.

like image 182
Sean Gough Avatar answered Oct 21 '22 18:10

Sean Gough



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!