Search code examples
.netcom-interop

Can I move COM classes between assemblies?


I have an assembly that exposes an interface and a class to COM via ComVisible. This class is consumed by a VB6 application.

Now, due to some reorganization, I want this class to be provided by a different assembly. As long as I keep the Guid and ProgID, could I move the code to this new assembly, register it, and have the VB6 application find the class without having to recompile it?


Solution

  • As long as I keep the Guid and ProgID

    Be careful, there are Guids, plural. At a minimum you have one for the interface type, the IID, and one for the class that implements the interface, the CLSID. Additional ones if you support events or have more than one interface or class in the assembly. If you also register the type library (Regasm.exe /tlb option) then you also have a guid for the assembly itself that is used for the type library TLBID.

    Get any one of these wrong, or rely on .NET auto-generating them, and the replacement won't work.

    The best way to ensure the replacement is identical is to decompile the type libraries and compare them for identity. From the Visual Studio Command Prompt, first generate the type library if you don't already have it with Tlbexp.exe. Then run Oleview.exe, File + View Typelib to get the content of the type library, decompiled to IDL. Copy/paste the right pane content into a text file. Repeat for the replacement DLL and compare. Don't worry about differences in comments.