Make a .NET Assembly run as COM-Server

Because it’s the second project about COM i decided to write a short article about it. Com is great for extending old projects with great and creative .NET code 😉

How to achieve that:

1. Write a normal Class Library with Visual Studio

2. Mark your class with this attributes

public class UserHelper : IUserHelper

//Your Class Code


3. Implement an interface for your class with all your methods and properties that should be accessible via COM

public interface IUserHelper
void SomeMethod(int param1, string param2);

4. Build your library

5. Use regasm.exe to register the dll. I use

regasm.exe "PathToDll.dll" /codebase /regfile

5. Execute the generated .reg file for creating all needed registry keys

6. Test your COM Server via a little vbScript

obj.METHODNAME Param1,"Param2"
MsgBox "Well done!"

Notes: Never ever use the VS tools like  „Make Assembly Com-Visible“ or „Register for COM interop“. It drives me crazy and is a waste of time at all. Keep in mind that this example is for platform target „Any CPU“. You should register it with reagsm.exe 32 bit wich is located in C:\Windows\Microsoft.NET\Framework\v4.0.30319. 64 bit version of regasm.exe is here: C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Great article in .NET Pro (German):

Regasm.exe reference: