Search code examples
c#vb.netvb6late-bindingnative-methods

C# Implement Late Binding for Native Code


We are working with an existing native application (most likely written in VB) that loads assemblies and calls methods with "Late Binding." We do NOT have access to its source code.

We want to implement this interface in C#, and have the native application call our C# assembly.

Is this something that's possible?

Is this anything we have to do beyond matching the method names and method signatures to make it work?


Solution

  • These are the steps required to make it work:

    1. Mark your class [ComVisible(true)], and make sure to give it a unique [Guid] attribute
    2. Match your ProgId to your class which is normally MyNamespace.MyClass, but you can add an attribute on your class to override this as well
    3. Put the proper [DispId] attributes for each method that will be called
    4. Compile your assembly and run regasm.exe on your assembly

    And voila! Native code can call your C# via "late binding". I, of course, had to setup several registry keys to make my native application know how to load my assembly, but all is working.