How to create and use a COM+ application

This is a quick summary of how to create a COM+ application in C# and use it from PowerShell.


Step 1

Create a new Class Library project like so:

01newproject

Step 2

Add a reference to System.EnterpriseServices to your project.

02referencemanager

Step 3

Create a class. Make it a child of the ServicedComponent class.

03class

Step 4

Create an interface for your class. Make sure the cursor is within your class when choosing "Extract Interface..." from the Edit -> Refactor menu.

04extractinterface


05extractinterface1

Note how extracting the interface changed your class also to inherit from the newly created interface:

06extractinterface2

Step 5

Look at the interface. It should reflect the contents of your class including any fields or methods you have created.

07interface

Step 6

Configure AssemblyInfo.cs:

  • Set [assembly: ComVisible(false)] to true.
  • Add an [assembly: ApplicationName()].
  • You can also add [assembly: ApplicationActivation()] and [assembly: ApplicationAccessControl()] tags if you like. But those can also be configured after installation of the application.


08ssemblyinfo

Step 7

Configure the build settings for your platform.

10configurationmanager

Choose the correct CPU type for your platform. You can see this reflected on the build page of the project properties.

11propertiesbuild

Step 8

Create a key for a strong name using the strong name tool sn.exe:

09snk

Then use this key file to sign the assembly.

12propertiessigning

Step 9

Build the application.

13build

Step 10

Copy the newly-built DLL to a location where it can be seen by everyone who might want to use it. Then register it with COM using RegSvcs.exe.

(If you cannot find RegSvcs.exe, it's located in C:\windows\Microsoft.NET\Framework64\v4.0.30319 or a related directory depending on system architecture and .NET version.)

14copyregister

Start dcomcnfg.exe immediately to look at the result.

15componentservices

Step 11

Use the class.

16comobject

It's not doing great things but it works.



 © Andrew Brehm 2016