Deploying a COM Component from C#

Here is some code to help you to deploy a COM component from C#. I needed to programmatically deploy a project that would be registered as COM object (not COM+) and be installed into GAC. The following two classes will help in this objective.

FusionInstall.cs is a class that Microsoft has made available to do the actual add and remove from GAC it uses InteropServices into two dlls one is fusion.dll which does the heavy lifting and the other is kernel32.dll for a file search (not used).

To install an assembly and register into GAC

  1. Add the Installer1.cs class and FusionInstall.cs classes to the target assembly project.
  2. Change the namespace of both from GoofyTest to your dlls namespace.
  3. Add the proper references and using to the dll.
  4. Make sure your target class is decorated with:
    [ClassInterface(ClassInterfaceType.AutoDual)]so it will register properly as a COM object.
  5. Create a Type Library that is named the same as dll using code similar to
    C:\WINNT\Microsoft.NET\Framework\v1.0.3705\regasm.exe GoofyTest.dll /tlb:GoofyTest.tlb /verbose
    This will create a tlb file. Make sure it is in the same directory as target dll. Now run above line with /u to uninstall the target dll, we just want to steal the tlb file.
  6. Add a Deployment Project to the Solution.
  7. Right Click on Project and get View Custom Actions. Add the target dll to Install and Uninstall Custom Actions. Thus when the installer runs it will call into our Installer1.cs code because we override!
  8. Add your files (sample: GoofyTest.dll and GoofyTest.tlb) to the Application folder. I used the actual files not the Primary Output.
  9. The cool thing about System.EnterpriseServices.RegistrationHelper is that it was designed to add an application package and dlls to the Component Services but if it notices that your dll is not derived from System.EnterpriseServices.ServicedComponent It falls thru to registration as regular COM component just what we want! 
  10. After successful compile to test, run the following command from folder where your msi file is located. Change parameters to match your dll: msiexec /i Setup1.msi /l GoofyInstall.log
    This allows you to see the log. Pay no attention to 3 pixel errors, that is Microsofts problem not yours.
  11. Run Fuslogvw.exe if you want to see log failures.