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
Published Wednesday, June 16, 2004 10:38 PM by PSteele

Comments

# re: REGASM, COM and Type Libraries

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.

Thursday, June 17, 2004 9:03 AM by Darrell

# re: REGASM, COM and Type Libraries

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

Tuesday, October 10, 2006 1:22 PM by Nick

# re: REGASM, COM and Type Libraries

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.  ;)

Thursday, October 12, 2006 7:42 AM by PSteele

# re: REGASM, COM and Type Libraries

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.

Thursday, November 16, 2006 5:27 PM by Daniel

# re: REGASM, COM and Type Libraries

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

Thursday, December 21, 2006 2:56 AM by Massimo

# re: REGASM, COM and Type Libraries

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

Friday, January 5, 2007 9:00 AM by Yoseph Mekasha

# re: REGASM, COM and Type Libraries

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?

Saturday, June 30, 2007 4:55 PM by M Asif