Microsoft KB Archive/816152

= How to use COM components in Visual Studio .NET with Visual C# .NET or in Visual Studio 2005 with Visual C# 2005 =

Article ID: 816152

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 315847.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Using COM Components From Visual Studio .NET
 * Complete Visual C# .NET Code Listing
 * Complete Code Listing For MyCOMComponent
 * Troubleshoot
 * REFERENCES



SUMMARY
This step-by-step article describes how to use a COM component from in Microsoft Visual Studio .NET by using Microsoft Visual C# .NET or in Microsoft Visual Studio 2005 by using Microsoft Visual C# 2005.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you may require:
 * Visual C# .NET or Visual C# 2005

This article assumes that you are familiar with the following topics:
 * Visual C# .NET or Visual C# 2005
 * COM

back to the top

Using COM Components from Visual Studio .NET
You can use COM components from in Microsoft Visual Studio .NET code by using the Microsoft .NET Framework Component Object Model (COM) interoperability layer (or COM Interop). Using Visual Studio .NET or Visual Studio 2005, you can easily access and use COM components.

Note This article uses a simple COM component named MyCOMComponent for demonstration purposes. This component contains a single class, Class1, with a single method, Add. The Add method adds two numbers together and returns the sum. See the&quot;Complete Code Listing for MyCOMComponent&quot; section for the source code of this component. The techniques used in this article apply to any COM component.  Start Microsoft Visual Basic 6.0. Create a new ActiveX Dll named MyCOMComponent.  Paste the following in the Class1 Code window. 'Sample COM Component Source Code 'Written in Visual Basic 6.0 ' 'Project Name: MyCOMComponent 'Project Type: COM DLL 'File Name: MyCOMComponent.dll ' 'Class Name: Class1

Option Explicit

Public Function Add(ByVal Num1 As Long, ByVal Num2 As Long) As Long Add = Num1 + Num2 End Function  On the File menu, click Make MyCOMComponent.dll. On the Make Project dialog box, click OK. MyCOMComponent.Dll is created. Close Microsoft Visual Basic 6.0. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Console Application in Visual C# .NET or in Visual C# 2005 and name the project MyCOMClient .</li> On the Project menu, click Add Reference.</li> In the Add Reference dialog box, click the COM tab. Notice that the ListView control lists all the COM components that are registered on the local computer in alphabetical order.</li> Locate and select the MyCOMComponent.dll file, click Open, and then click OK to close the dialog-box.

Note In Visual Studio 2005, you do not have to click Open.</li>  In the Class1 Code window, add the following code to the Main function: int mySum = 0; MyCOMComponent.Class1Class myCOM = new MyCOMComponent.Class1Class; mySum = myCOM.Add(1,2);

Console.Write(&quot;1 + 2 = {0}&quot;, mySum.ToString); Console.ReadLine; </li> On the Debug menu, click Start to build and run the application.

The following output appears in the Console window:

1 + 2 = 3

</li></ol>

back to the top

Complete Visual C# .NET Code Listing
using System;

namespace MyCOMClient {   ///     /// Summary description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           int mySum = 0; MyCOMComponent.Class1Class myCOM = new MyCOMComponent.Class1Class; mySum = myCOM.Add(1,2);

Console.Write(&quot;1 + 2 = {0}&quot;, mySum.ToString); Console.ReadLine; }

} } back to the top

Complete Code Listing For MyCOMComponent
'Sample COM Component Source Code 'Written in Visual Basic 6.0 ' 'Project Name: MyCOMComponent 'Project Type: COM DLL 'File Name: MyCOMComponent.dll ' 'Class Name: Class1

Option Explicit

Public Function Add(ByVal Num1 As Long, ByVal Num2 As Long) As Long Add = Num1 + Num2 End Function back to the top

Troubleshoot
As with any COM component, you must register MyCOMComponent before you can use them. Make sure that you use a tool such as Regsvr32.exe to register MyCOMComponent.

back to the top

<div class="references_section">