Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 975
NS.X. Avatar asked Jun 01 '12 22:06

NS.X.


People also ask

What is register free COM?

Registration-free COM interop activates a component without using the Windows registry to store assembly information. Instead of registering a component on a computer during deployment, you create Win32-style manifest files at design time that contain information about binding and activation.

Is Interop free?

NET Framework to create COM components, you can use registration-free COM interop.


2 Answers

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.

like image 187
NS.X. Avatar answered Nov 08 '22 14:11

NS.X.


Your manifest is not correct. It should contain the <file> element instead of the <dependentAssembly> element. With <comClass> elements that declare the creatable co-classes.

Generating the manifest is a built-in feature in the VS build system. To make it work, the COM component needs to be installed correctly on your dev machine. Do not try a reg-free COM manifest unless you can make it work without one on your machine, you'll fight two problems instead of one. So for starters make sure that the server is properly installed, using the vendor's installer or deployment instructions. And make sure that a test program that creates an object and call a method operates correctly.

Exercising this is useful, with a known-good COM server that's available on any machine. Create a new project from the Console Application project template. Project + Add Reference, Browse tab, select c:\windows\system32\shell32.dll. Select the added Shell32 reference and change its Isolated property to True. Build + Rebuild.

Look in the bin\Debug build directory of your project and open the app.exe.manifest file with Notepad. Observe the file and comClass elements, that's what you'll need in yours as well. If this doesn't help then turn to the owner of the COM server, he should be able to provide you with the manifest you need.

like image 4
Hans Passant Avatar answered Nov 08 '22 14:11

Hans Passant