Exposing a .NET Component as a COM Component

C# components are accessible as COM components with the use of a couple .NET Framework tools to create an unmanaged proxy and enter the proper settings in the registry. This enables unmanaged code to use .NET components as if they were COM components. Listing 33.4 shows a C# component to be exposed as a COM component.

Listing 33.4. A C# Component Exposed as a COM Component: CallFromCom.dll
using System;

public interface ICSharp
{
    string GetResponseFromCSharp();
}

namespace CallFromCom
{
    /// <summary>
    /// C# DLL to be called as a COM object.
    /// </summary>
    public class CallCSharp: ICSharp
    {
        public string GetResponseFromCSharp()
        {
            return "Hello from C#!";
        }
    }
}

The code in Listing 33.4 appears as ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.