Creating a COM object in .NET

Information in this article was condensed from the following URLs, with contributions from Tony Chu and Jorge Castillo.

Table of Contents [Hide/Show]

{outline||<1> - }


(coming soon)


(coming soon)


Visual Studio 2005

1. In AssemblyInfo.cs set [assembly: ComVisible(true)]

2. Create an interface with the following attributes.

public interface IEngine
	string HelloWorld();

3. Assign a guid to the GuidAttribute (via Tools menu -> Create Guid). Note: Be sure to remove the opening and closing braces from the guid. For example, if the Create Guid tool gives you {2D8128D8-8A82-45e5-87CA-6EF158154FEF}, use 2D8128D8-8A82-45e5-87CA-6EF158154FEF instead for the GuidAttribute.

4. Create a class that implements the class and with the following attributes.

public class Engine : IEngine
	public string HelloWorld()
		return "Hello world";

5. Generate a key: in a Visual Studio Command Prompt window, navigate to your source code folder, then execute the following command.

sn -k "MyCompany.MyNamespace.snk"

6. In Visual Studio, go to the Properties page for your project -> Signing tab.

7. Build the .NET Assembly

8. In a Command Window, execute the following commands. The gacutil.exe program will be installed in one of the following folders, depending on what's installed on your system. Use the appropriate one in place of GacUtilFolder in the script below.

rem Ensure the install folder exists
mkdir "C:\Program Files\MyDeploymentFolder"

rem Unregister old version
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe" /u "C:\Program Files\MyDeploymentFolder\MyCompany.MyNamespace.dll"

rem Uninstall old version from the GAC
"GacUtilFolder\Gacutil.exe" -u "MyCompany.MyNamespace"

rem Copy the new version to the install folder
COPY MyCompany.MyNamespace.* "C:\Program Files\MyDeploymentFolder\"

rem Install the new version to the GAC
"GacUtilFolder\Gacutil.exe" -i "C:\Program Files\MyDeploymentFolder\MyCompany.MyNamespace.dll"

rem Register the new version
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe" /tlb "MyCompany.MyNamespace.dll"

Visual Basic 6.0

1. In the Project References, add a reference to the COM object you just created in .NET. The name will be the same as the namespace in your C# code.

2. Code against the reference as you would any COM object.