com-interopregfreecom

Exact steps for registration-free COM interop in .NET (invoke copied COM dll without regsvr32)


I want to add a registration-free COM reference to my .NET app. Registration-free means users can run the app without registering the COM component to their system.

I found a number of articles on this topic (e.g. MSDN, this S/O question, etc.) but none contains concrete steps. Here's what I have tried and did not work:

  1. Generate manifest for the COM dll (say foo.dll) using mt.exe as described in this answer.
  2. Add foo.dll and foo.dll.manifest to my app as "Build Action" = "Content" and "Copy to Output Directory" = "Copy Always".
  3. Add reference to interop.foo.dll that came with foo.dll.
  4. Add app manifest file with this section:

    <dependency>
      <dependentAssembly>
        <assemblyIdentity type="win32" name="foo" />
      </dependentAssembly>
    </dependency>
    
  5. Build and run.

There's an error saying application configuration is not correct. Tried different values in <assemblyIdentity> without luck.

Could someone share experience? Thanks.


Solution

  • Hans's answer has the right information, but I wasn't clear what to do when I first read it. After trying many different things I finally got it working and it's very simple:

    1. Register the COM class on the developer machine.
    2. In the app project, add the COM reference.
    3. In reference properties, set these:
      • Isolated = True
      • Copy Local = True
      • Embed Interop Types = False (Hans pointed out in the comments that this is unnecessary but I haven't verified)

    By following these steps, the interop.foo.dll and app manifest are automatically generated. This allows users to use the COM component without registration.

    On the opposite, if you don't register the COM on the dev machine, you can still develop and build the app by adding reference to interop.foo.dll (which can be generated on a machine with the COM class registered and then copied around), but the build result can only run on machines with the COM component registered. There might be a solution around this but requires deeper knowledge of how MSBuild work with COM references, which I did not spend time investigating.

    So in short, the key to runtime registration-free COM is to register the COM class on the dev machine.