19

I have a COM-visible DLL written in C# that I would like to use in a VB6 application. I have two main use cases of the DLL and am wondering when the /codebase option is applicable and when it is better to register in the GAC.


Use cases:

  1. The DLL will be loaded onto another developers PC and needs to be accessible to the VB6 IDE under the Project > References menu

  2. The DLL will be loaded onto production machines when the VB6 application is released


Any information on the appropriate use of the /codebase would be helpful.

Thanks.

davidk
  • 690
  • 1
  • 5
  • 16
  • Did you mean *GAC* instead of *DOM*? – Euro Micelli May 29 '14 at 18:01
  • 2
    The /codebase argument doesn't affect the registration *per-se*, or how VB6 or anybody else sees the object. Its only effect is determine where .NET will look for the DLL when VB6 (or anybody else) actually tries to use it. I've seen arguments for both GAC and /codebase. Microsoft seems to recommend the GAC for production use and /codebase for development. Other well respected users here appear to advocate for /codebase always. I tend to follow the former. It's mostly a question of deployment ease, not an issue how to code against it, or execution. – Euro Micelli May 29 '14 at 18:02
  • I did mean GAC. Thanks for the correction, I have updated the original post. – davidk May 29 '14 at 18:03
  • @EuroMicelli could you list some of the arguments or point me towards some resources? I am interested in reading more on the topic. – davidk May 29 '14 at 18:37
  • Go with @Hans' answer. His is better than what I would have written. – Euro Micelli May 29 '14 at 18:59

1 Answers1

29

The /codebase option is the exact equivalent of the way you used to register COM servers with Regsvr32.exe. You'll have to pick a specific location for the DLL and the path to that location is written to the registry. That's risky, COM servers have a strong DLL Hell problem since their registration is machine-wide. When you update that DLL then any application that uses the server will be affected. Not often in a good way, this very often breaks an app that was not recompiled to use the updated server.

.NET improves on that by being able to store multiple versions of a DLL as selected by their [AssemblyVersion]. The exact equivalent of the Windows side-by-side cache, the GAC for managed assemblies. So old apps that were not rebuilt will continue to run unaffected, still being able to use the old DLL. That's a good way.

Using /codebase is advisable when you are busy developing the server. You can skip the extra required step that registers the DLL in the GAC. Forgetting to do so is painful, your changes won't seem to work because the test app will load the old version. Regasm will display a warning when you use /codebase, warning you about the DLL Hell potential, you can ignore it.

When you let Visual Studio take care of the registration with the Project > Properties > Build tab, "Register for COM interop" checkbox then you get the exact equivalent of Regasm /codebase /tlb. It is more desirable to do it this way because it also ensures that the old version of the assembly gets unregistered, thus avoiding registry pollution. But VS has to run elevated so it can write to the registry.

Using Isolated COM (aka "registry-free COM") is the best way. It lets you store a copy of the COM server in the same directory as the client program and you don't have to register it at all. That however requires tinkering with the client program, difficult if you don't have any control over the client app or if it is the kind of app that other people like to mess with. Microsoft Office apps for example.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • Do I understand you correctly; you would suggest the developers use the `/codebase` option in order to assure they are always running against the latest copy but that the production environment run against the GAC in order to prevent issues with old applications not being updated after a new version is released? – davidk May 29 '14 at 18:40
  • That's what it says, yes. – Hans Passant May 29 '14 at 18:42
  • Storing COM server DLLs in the same folder as an application is risky though, since quite a few clients will call the selfreg entrypoint and this can result in the most common source of DLL Hell taking hold. Even using SxS manifests it is far safer to use a `bin`, etc. subfolder of the app folder. – Bob77 May 29 '14 at 19:31
  • No, that's most definitely not what client programs should ever do. Doesn't make sense, COM servers are registered so a client can find the DLL. UAC puts a stop to it anyway. – Hans Passant May 29 '14 at 19:35
  • @HansPassant I agree a registry solution would be best. Do you know of any good resources about setting my DLL up this way? – davidk Jun 02 '14 at 18:48