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

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"Complete Code Listing for MyCOMComponent" section for the source code of this component. The techniques used in this article apply to any COM component.
  1. Start Microsoft Visual Basic 6.0.
  2. Create a new ActiveX Dll named MyCOMComponent.
  3. 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
  4. On the File menu, click Make MyCOMComponent.dll.
  5. On the Make Project dialog box, click
    OK. MyCOMComponent.Dll is created.
  6. Close Microsoft Visual Basic 6.0.
  7. 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.
  8. On the Project menu, click Add Reference.
  9. 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.
  10. 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.
  11. 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("1 + 2 = {0}", mySum.ToString());
    Console.ReadLine();
  12. On the Debug menu, click Start to build and run the application.

    The following output appears in the Console window:
    1 + 2 = 3
back to the top

Complete Visual C# .NET Code Listing

using System;

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

Console.Write("1 + 2 = {0}", 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

References

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

back to the top
Propriedades

ID do Artigo: 816152 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários