How Does a COM Program Locate a .NET DLL Registere

2019-06-19 00:46发布

问题:

One customer wants to consume our .NET DLLs from VB6. They are designed to support reverse interop and all works fine... except: There are two separate VB6 programs in two different directories. It seems it's necessary to do one of:

  1. Copy the .NET DLL into both directories, or
  2. Install the .NET DLL in the GAC

This is the customer's observation and also supported by the RegAsm documentation:

After registering an assembly using Regasm.exe, you can install it in the global assembly cache so that it can be activated from any COM client. If the assembly is only going to be activated by a single application, you can place it in that application's directory.

I'm confused on this point.

First point of confusion:

As far as I understand, the COM runtime locates the DLL using the Prog ID / Class ID. When I look in the registry at the Class ID entry, I see the full path to the .NET DLL in the CodeBase key. Why is it that a COM program using the Prog ID / Class ID doesn't locate the .NET DLL using the CodeBase?

Second point of confusion:

The GAC is specific to .NET. How is it involved in resolving COM references?

回答1:

You're correct COM uses the ProgId to get to the ClassId to get to the COM server to load. In the case of .NET COM dlls, the COM server is actually MSCOREE, not the .NET dll (the value of the default key under {CLSID}/localserver32). MSCOREE, not COM, then can use whatever rules it wants to find the .NET assembly.

At this point, I don't know what .NET really does - that would require testing. You could watch yourself using FUSLOGVW. I can however guess it loads the assembly like it loads any other .NET assembly.

Assuming it just calls Assembly.Load() with the ClassName value, it'll follow the .NET binding rules. First look in the GAC, if not found will probe - so if a codebase is defined, it'll only look there, otherwise it will probe based on the application base (by default the directory of the app [but not for ASP.NET]).

I think that fits with what your read in regasm.

Your question is so old I assume it's been OBE, but the rules you'd use for putting assemblies in the app directory, use a codebase, or in the GAC are the same with and w/o com interop. Every situation is different and I haven't done enough .NET to give up any deep insights. I favor xcopy installs so I'd go with in the app directory (and use registration free COM) but there are other considerations like if the two vb apps must use the same version of the COM object.