REGASM, COM and Type Libraries

If you've ever done any work with making .NET components that are exposed to COM, you're familiar with REGASM. This utility makes registry entries to make your .NET object look like a COM object. In its simplest form, you can do:

REGASM MyAssembly.dll

Now, all of the COM-compatible classes are registered as COM objects. You can fire up VB6 and start writing code:

Dim net As Object

Set obj = CreateObject("NETProject.Foo")
obj.Move

Pretty easy. Except that you're late binding because you don't have a COM type library.

No problem! REGASM can generate a type library for you and even register it:

REGASM MyAssembly.dll /tlb:MyAssembly.tlb

Now, in VB6 you can add a reference to the type library and use early binding:

Dim net As Foo

Set obj = New NETProject.Foo
obj.Move

Suppose you want to "unregister" this assembly? REGASM has a "/unregister" switch:

REGASM /u MyAssembly.dll

The thing you want to watch out for (and the real reason for this post) is that if you used the /tlb option to register a type library, you need to make sure you include the /tlb option when unregistering. If you don't, only the COM object is unregistered, but the type library is still registered. And you can't unregister the type library by itself.

So, rule of thumb:

REGASM MyAssembly.dll /tlb:MyAssembly.tlb

Balance that with:

REGASM /u MyAssembly.dll /tlb:MyAssembly.tlb

7 Comments

  • Nice, short summary.



    Next up, do the same for regsvcs, then compare the two (regsvcs and regasm). I'm mostly aware of the differences (I'm not an interop guru by any means, but I've done my share) but I see these types of questions a lot.

  • Hi thanks for the summary, I wanted to know are there any side effects to never unregistering the library. My app manually re-registers the new library on install, but never on uninstall. I can see the different registry entries for each time it was re-registered. Aside from a thoroughly cluttered registry, are there any other negative side effects?

    Thanks

  • Nick, I can't think of any other side effects except the one you mentioned -- a cluttered registry. And with the way the registry is, I wouldn't want to clutter it unless I really saw a good reason. ;)

  • Great summary, didn't know about unregistering the .tlb as well. how about registering the .exe? i have an application that is used both ways, and find it a huge pain to always compile both a dll and exe each time i do an update. is there a way to use regasm to register my .exe file to create a tlb and use it just as i use a normal COM dll? for instance:

    regasm myApp.exe /tlb:myApp.tlb

    i have done this and regasm claims it succeeded, but referencing the component from VB6 gives me a runtime error (File not found).
    thanks for your help.

  • Maybe someone can help me: i have a .net service COM+ that calls an assembly using .tlb type lib. We have updated the called assembly to .NET 2.0 and now i have an error accessing this object. I think that is a CAS or permission problem but i'm not sure. Someone had the same experience? Thaks

  • this a very good stuff...I liked is short and prsice

  • I ran into this article of yours today when I was trying to produce a tlb file from a .Net dll and then I noticed the name and it sounded familiar. Are you still in MI?

Comments have been disabled for this content.