DetailPage-MSS-KB

Microsoft small business knowledge base

Article ID: 816152 - Last Review: May 16, 2007 - Revision: 2.5

On This Page

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.


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

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

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();
		}

	}
}

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

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.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
Advanced COM Interop
http://msdn2.microsoft.com/en-us/library/8bwh56xe(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/8bwh56xe(vs.71).aspx)


APPLIES TO
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbcontrol kbwindowsforms kbcominterop kbinterop kbhowtomaster KB816152
Share
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support