COM (Component Object Model) is a legacy Microsoft technology for building binary software components that can interact across programming languages. Although .NET has largely replaced COM in modern applications, there are still many scenarios
This post walks you through the steps to create a simple COM-visible component in C# using Visual Studio.
Build COM Component in C# using Visual Studio
Prerequisites
- Visual Studio (any recent version with .NET Desktop Development workload)
- .NET Framework (not .NET Core or .NET 5+)
- Administrator access (to register the COM DLL)
Getting Started
COM stands for Component Object Model, which is a Microsoft-developed framework for building reusable binary software components that can interact regardless of the language they were written in. A COM component is typically:
- A binary software component model developed by Microsoft.
- Used for enabling language-independent communication between objects.
- Typically implemented in unmanaged code like C++.
- Distributed as DLLs or EXEs with .tlb (type libraries) that describe the interfaces.
Build COM Component in C#
Building a COM component in C# involves several key steps, including class creation, registration, and optional interop considerations. Here's a step-by-step guide to help you create a .NET COM component.
Create a Class Library Project
Creating a Class Library Project in Visual Studio is straightforward. Here's a step-by-step guide for Visual Studio
- Open Visual Studio.
- Go to
File > New > Project
. - Choose Class Library (.NET Framework).
- Name your project (e.g., ComInteropDemo) and click Create.
- Select a .NET Framework version (e.g., 4.7.2). COM interop is only supported in .NET Framework.
Define COM Interface
Defining COM (Component Object Model) Interface in Visual Studio typically involves creating an interface in IDL (Interface Definition Language). Define your own COM interface with necessary attributes to expose them to COM like below.
using System;
using System.Runtime.InteropServices;
namespace ComInteropDemo
{
// Assign a unique GUID (use Tools > Create GUID in Visual Studio)
[ComVisible(true)]
[Guid("A1234567-B89C-1234-D567-890123456789")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMyComClass
{
void SayHello(string name);
}
}
Define Your COM Class
Defining a COM class in C# involves creating a .NET class that can be exposed to COM clients. This requires using specific attributes and registering the assembly for COM interop. Create your COM Class like below.
using System;
using System.Runtime.InteropServices;
namespace ComInteropDemo
{
[ComVisible(true)]
[Guid("B1234567-B89C-1234-D567-890123456789")]
[ClassInterface(ClassInterfaceType.None)]
public class MyComClass : IMyComClass
{
public void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
}
}
Enable COM Visibility
To enable COM visibility in a Visual Studio project, you need to configure your project settings and code to allow your .NET assembly to be exposed to COM clients. Here's how to do it:- Go to the Application tab.
- Click Assembly Information.
- Check Make assembly COM-visible.
- Go to the Build tab.
- Check Register for COM interop (for debugging/development).
- Ensure the Platform target is x86 or x64 (not "Any CPU").
Build the Project
- Right-click the project > Build.
- This will generate a
.dll
in thebin\Debug
orbin\Release
directory.
Register the Assembly for COM
After building the project, you need to register the project assembly for COM. To register the assembly in .NET, you typically need to do the following
- You need to use the RegAsm tool, available with the .NET Framework SDK:
- Run this from a Developer Command Prompt for Visual Studio with Administrator privileges.
- Execute the below
-
Syncax
Examplecd path\to\your\compiled\assembly regasm MyComAssembly.dll /codebase /tlb
regasm ComInteropDemo\bin\Debug\ComInteropDemo.dll
/codebase
: Adds a registry entry pointing to your assembly's location (if it's not in the GAC)./tlb
Generates a type library (.tlb) file for COM clients.- Add to the GAC If you want to avoid using
/codebase
, install the assembly to the Global Assembly Cache:-
Syncax
Examplegacutil -i MyComAssembly.dll
gacutil -i ComInteropDemo\bin\Debug\ComInteropDemo.dll
Unregistering the Assembly
regasm /unregister MyComAssembly.dll
Summary
While COM might seem dated, it's still an essential interop tool for bridging modern .NET code with legacy systems. By following these steps, you’ve created a functional COM component in C# that can be used by any COM-aware language. I hope this post was helpful to you.
Thanks