Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Creating a COM object in .NET

RSS
Modified on Mon, Feb 09, 2009, 9:56 AM by Administrator Categorized as Visual Basic 6, Visual Studio and Developer Tools, ·Net Framework
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> - }

Background

(coming soon)

Explanation

(coming soon)

Procedure

Visual Studio 2005

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

2. Create an interface with the following attributes.

[ComVisible(true), 
GuidAttribute("my-guid"), 
InterfaceType(ComInterfaceType.InterfaceIsDual)]
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.

[ComVisible(true), 
GuidAttribute("my-guid"), 
ProgId("MyCompany.MyNamespace.MyClassName"), 
ClassInterface(ClassInterfaceType.None)]
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.

  • Select the Sign the assembly checkbox
  • Specify the SNK file you just created

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.

  • If you have Visual Studio 2005 installed — C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe
  • If you have just the .NET Framework 2.0 installed — C:\Program Files\Microsoft.NET\SDK\v2.0\Bin\gacutil.exe

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.

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.