Getting .NET COM Interop DLLs Working

I recently finished working on a macro program where the software’s interface is COM.  Everything worked great until I wanted to transfer the macro over to a test client computer.  The program I was writing the macro for requires some registry entries, so I assumed that was all I needed to do.  After about 2 hours of having issues I figured out how to get the macro working on computers other than the one I developed the macro on.

What happens automatically when developing COM innerop projects in Visual Studio is that the IDE automatically registers the DLL file behind the scenes, so when the program that is going to use the macro starts up, it can find the DLL file (assuming the “register for COM Interop” is checked in project properties).

When transferring over the macro DLL(s), the client computer does not have the correct registry entries, so when the program attempts to load the macro, it will fail because it can’t find the macro DLL(s).

Things I learned:

  • regsrv does not work on .NET DLLS, you must use regasm.exe (included in the .NET framework).
  • just running regasm.exe with the dll name is not enough (there are two methods to getting the DLL visible to COM)

The method I used:
This method is good if you only want one program to see your DLLs, this does not register it with the GAC.
In your project properties inside Visual Studio 2005:

  • Select the “Signing” tab, check the “Sign the assembly” checkbox.
  • From the drop-down box, select <new>, fill out the fields in the pop-up (eg. key name and password).
  • Creating a key for your DLL means that it will disallow people to create a DLL with the same name as yours and so the .NET framework can properly manage dependencies.  If you don’t do this step you will get a warning when you try to run the regasm step below.
  • Figure out where the regasm file is so we can use it.  This step isn’t necessary if you use a console window inside Visual Studio. (for .net 2.0 it is in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe), regasm can’t be called with “regasm …” inside a normal console window, unless you added your own dos “shortcut” to the exe.
  • Copy all of your output files to where you want them to be.  So for my macro, I had a directory inside the application’s add-on folder.
  • Create a  text/BAT file  in the same directory
  • include this command in the BAT file:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm macroname.dll /codebase /regfile:install.reg
  • run the bat file in a console window, it will generate a reg file that you can use to install the necessary registry entries to make the DLL visible to your selected DLL file.  Remember that the BAT file needs to be executed in the directory that you intend to run the macro from.  When the REG file is created, it adds various direct paths to the directory, so we need the regasm utility to use the correct path.

Now you can merge the REG file from anywhere in the user’s computer.  It will register the DLL file so that your specified program will find it.  When we write the regasm command line, take note of a few things.  The codebase switch is the directory where the DLLs are located.  the regfile switch is the filename you want the registry install file.  You can run regasm without the regfile switch, but it will install the registry keys directly into the registry.  This is fine, but for me having a reg file is more convenient.