COM Interop: Registering a TLB on Install

When you make a COM interop application, occasionally you would like it if the unmanaged application that will be potentially using your library could have the ability to load the type library of your application. This is not uncommon as interop applications often require the type library to be registered before being executed. Here is a brief description on how to accomplish this.

This requires a few easy steps I discovered and am replicating from the newsgroup reference provided by Ian Turner found here . I added a couple of further details for, what I perceive to be, an easier step-by-step process to follow.

  1. Add a reference to System.Configuration.Install (under the .NET tab) to the project whose primary output (dll) you are wanting to register the type library for.
  2. Add a few "using" statements to your namespace if they are not already there:
            using System.Configuration.Install;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
  3. Add the following class to your namespace:
            [RunInstaller(true)]
    public class ComInstaller : Installer
    {
    public override void Install(System.Collections.IDictionary stateSaver)
    {
    base.Install(stateSaver);

    RegistrationServices regsrv = new RegistrationServices();
    if (!regsrv.RegisterAssembly(this.GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase))
    {
    throw new InstallException("Failed To Register Type Library for COM");
    }
    }

    public override void Uninstall( System.Collections.IDictionary savedState)
    {
    base.Uninstall(savedState);

    RegistrationServices regsrv = new RegistrationServices();
    if (!regsrv.UnregisterAssembly( this.GetType().Assembly))
    {
    throw new InstallException("Failed To Unregister Type Library for COM");
    }
    }
    }

I found another reference site for setup developers that had what appeared to be a well written VB script that registers the type library through a custom action in the setup project. It can be found here (search for "Register TLB File Using VBScript" when the page loads). Understanding custom actions is a bit beyond me and I found the suggestion above easier to implement. If you would like to provide information on how to implement the custom action script, I’d gladly post it here and grant due sitation. Contact me if you have any questions or further ideas.

The information on this page is provided "AS IS" with no warranties, and confers no rights.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s