0

I am aware of the Regasm tool, which is used to register .NET DLLs for COM interoperability: http://msdn.microsoft.com/en-us/library/tzat5yw6%28v=vs.110%29.aspx.

I have left: Register for COM interop and make assembly com visible unticked as per this question: "Register for COM Interop" vs "Make assembly COM visible". The reason for this is so that I can pick and choose which functions to expose to COM. I am using the following command:

Regasm c:\TestApp\Test.dll /codebase /tlb

Do you have to use the codebase flag to register a DLL for interoperability? If I leave out the codebase flag and then browse to the TLB using Object Explorer in Visual Studio 6 (VB6), then no types appear.

I know that codebase is used for DLLs that are registered in the GAC. Does it have to be used for those outside the GAC?

Community
  • 1
  • 1
w0051977
  • 15,099
  • 32
  • 152
  • 329

2 Answers2

4

It is a very important option, blindly applying it is not a good idea.

COM has a very strong DLL Hell problem. Registration of a COM server has machine scope by default, caused by the server registration written to the registry in the HKLM registry hive. Very Bad Things happen when you change the implementation of the server and a client program that used the server is not recompiled and upgraded on the machine. Keep in mind that you can only directly control the version of your server, there's usually very little you can do about making sure that the client program is updated as well.

The good kind of very bad thing that happens is when you follow the COM contract and give the coclass and interfaces a different {guid}. The client program will not be able to locate the proper interface anymore and fails at runtime with the REGDB_E_CLASSNOTREG or E_NOINTERFACE runtime errors. The user can usually figure out what went wrong, doesn't exactly make him very happy about it of course but some odds he'll take his own corrective measure and delete your update.

The very bad kind of very bad thing that can happen is that the COM client doesn't fail locating the coclass and interface but the call fails. Could be the completely wrong function, could be that the arguments are no longer compatible. That kind of failure is utterly undiagnosable, the user has no hope to figure out what went wrong. You'll get a "It doesn't work, help me!" phone call.

.NET has very strong support for allowing different versions of an assembly to live side-by-side on a machine. The GAC is where they are stored, the [AssemblyVersion] attribute is the key. So a client program that's recompiled to use the new version will automatically use the new version of the COM server. A program that wasn't recompiled will continue to operate correctly with the legacy version of the server. The GAC provides the version they need.

The GAC can be useful for a .NET class library as well but not nearly as important since most .NET libraries are deployed locally, in the same directory as the EXE that uses them. Not the case for a [ComVisible] .NET library since its registration is machine-global. There are counter-measures against that by using a manifest but this is up to the client program, not the server. Again outside of your control. And often very impractical if the client program is a widely-used program like an Office app or Internet Explorer.

So deploying your COM server to the GAC is very important, you really want to take advantage of the DLL Hell counter-measures and have some confidence that updating the server doesn't cause massive problems.

The only time you want to use /codebase is when you are developing and testing the server. In which case you of course never worry about DLL Hell, you always use the latest version and don't want to be hassled by the need to put it in the GAC. Easy to forget, that's an hour of your life you'll never get back.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

We use Com visible Net dll's a lot for our hybrid VB6/Net application and in our experience the only way to get it to work is by registering using the /codebase flag. As I understand it, this is used to register outside the GAC.

Dabblernl
  • 15,831
  • 18
  • 96
  • 148
  • I agree, we do the same thing – Matt Wilko Apr 16 '14 at 07:40
  • Thanks +1. Do you know if you can have overloaded constructors in .NET classes that are COM visible? – w0051977 Apr 16 '14 at 16:41
  • 1
    A Com visible class must always have a parameterless constructor. You can create as many parameterized constructors as you like, but you cannot call them from VB6. Tip: use factory methods to create your Net classes. These factory methods can then call a parameterized constructor. You can then even prevent use of the parameterless constructor by having it throw an exception – Dabblernl Apr 16 '14 at 21:34