CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – How to use COM components?

.Net supports to use of COM (Component Object Model) functionality within its managed code through its COM Interoperability feature. In this article, we are going to discuss the steps to use COM components within the ‘C#’ code.

Step 1. The first step is to add the COM component as a reference to the C# project.

Step 1.1. Open or create a C# project using Visual Studio 2012 IDE.

Step 1.2. Open “Solution Explorer” pane and right click on “References” node. Visual Studio will display a pop-up menu.

Step 1.3. From pop-up menu, select “Add Reference…” menu item. Visual Studio will display “Reference Manager” dialog.

Visual Studio 2012 - "Reference Manager" dialog
Visual Studio 2012 – “Reference Manager” dialog

Step 1.4. From “Reference Manager” dialog, select “Type Libraries” item under “COM” node. Visual Studio will display list of registered COM components in its middle-pane.

Step 1.5. Select a COM component by selecting its check-box. In this example, I have selected “SimpleATLCOMProjectLib” COM Component.

I have explained the steps to create an ATL COM Component in the article(s) Visual C++: Creating an ATL COM Component.

Step 1.6. Once the component is selected, click on OK button to add the reference to the ‘Visual C#’ project. Now expand “References” node in “Solution Explorer” pane and observe that “SimpleATLCOMProjectLib” entry added under “References” node.

Step 2. We have successfully added the COM component as a reference to the C# project. Now we need to change some properties of the reference “SimpleATLCOMProjectLib”.

Step 2.1. To change the properties of “SimpleATLCOMProjectLib” reference; right click on it. Visual Studio will display a pop-up menu.

Visual Studio 2012 - "Solution Explorer" - "References" pop-up menu
Visual Studio 2012 – “Solution Explorer” – “References” pop-up menu

Step 2.2. Select Properties menu item, from pop-up menu. Visual Studio will display properties of the selected reference in “Properties” pane.

Visual Studio 2012 - properties for COM Component Reference properties
Visual Studio 2012 – properties for COM Component Reference properties

Step 2.3. Change the value of “Embed Interop Types” property to “False”. We should change this to “False”; otherwise Visual Studio will display the following error message when we compile the program.

Interop type 'SimpleATLCOMProjectLib.HelloClass' cannot be embedded. Use the applicable interface instead.

Step 3. Now we are ready to use the COM component. Add the following statement to use this COM component.

using SimpleATLCOMProjectLib;

Create an instance of the “SimpleATLCOMProjectLib.HelloClass” class and call its method “SayHello”. All together the code looks like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SimpleATLCOMProjectLib;

namespace SampleCSharpConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            HelloClass obj = new HelloClass();
            if (obj != null)
                obj.SayHello();              
        }
    }
}

Once we run the application; it will display the “Hello, World!” message box which is displayed from an ATL COM component “SimpleATLCOMProjectLib’.

**

C# – How to use COM components?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top