SUMMARY
This article describes how to use an ActiveX component in
Microsoft Visual Studio .NET by using Visual C++ .NET or in Microsoft Visual Studio 2005 by using Visual C++ 2005.
back to the topRequirements
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you may need:
- 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
- ActiveX
back to the
top Using ActiveX Components from Visual Studio .NET
You can use ActiveX components from in Microsoft Visual Studio
.NET or Microsoft Visual Studio 2005 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 ActiveX components.
Note This article uses a simple ActiveX component that is named
MyActiveXComponent for demonstration purposes. This component contains a single
class,
MyClass, with a single method,
Add. The
Add method adds two numbers together and returns the sum. See the
Complete Code Listing for MyActiveXComponent
section of this article for the source code of this component. The
techniques in this article apply to any ActiveX component.
Note The following steps are written for Visual C++ .NET 2003 and Visual C++ 2005. The
Add Reference feature is new in Visual C++ .NET 2003 and in Visual C++ 2005. The
Add Reference feature adds the
/FU switch with the DLL path to
the compiler command line for the DLL that you are adding a reference to. If
you add a reference to a COM library (as this article does) the DLL that the
/FU switch points to is the interop DLL that the developement environment
generates. For additional information about using
Add Reference functionality in Visual Studio .NET 2002, click the following
article number to view the article in the Microsoft Knowledge Base:
310674
HOW TO: Add References to a Managed Visual C++ Project
- Start Visual Studio .NET 2003 or Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- Expand Visual C++ under Project
Types, and then click .NET.
Note In Visual Studio 2005, you do not have to click .NET. - Click Console Application (.NET) under
Templates, and then name the project
MyActiveXClient.
Note In Visual Studio 2005, clilck CLR Console Application under Templates. - On the Project menu, click Add
Reference.
Note In Visual Studio 2005, click Reference on the Project menu, and then click Add New Reference. - In the Add Reference dialog box, click the
COM tab, and then click Browse.
- Locate the MyActiveXComponent.dll file, click Open, and then click
OK.
Note In Visual Studio 2005, you do not have to click Open. - Open MyActiveXClient.cpp, and then paste the following using statement before the _tmain function:
using namespace Interop;
- In MyActiveXClient.cpp file, add following code to the _tmain function:
int mySum = 0;
MyActiveXComponent::MyClassClass *myActiveX = new MyActiveXComponent::MyClassClass();
mySum = myActiveX->Add(1,2);
Console::Write(S"1 + 2 = {0}",mySum.ToString());
Console::ReadLine();
The earlier code declares and creates an instance of the ActiveX
component. The code calls the Add method, and then displays the sum in the Console
window. - On the Debug menu, click
Start to test the application. The following output appears in
the Console window:
1 + 2 = 3
back to the
topComplete Visual C++ .NET Code Listing
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace Interop;
int _tmain()
{
try
{
int mySum = 0;
MyActiveXComponent::MyClassClass *myActiveX = new MyActiveXComponent::MyClassClass();
mySum = myActiveX->Add(1,2);
Console::Write(S"1 + 2 = {0}",mySum.ToString());
Console::ReadLine();
}
catch(Exception *ex)
{
Console::WriteLine(ex->Message);
}
return 0;
}Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
back to the topComplete Code Listing for MyActiveXComponent
'Sample ActiveX Component Source Code
'Written in Visual Basic 6.0
'
'Project Name: MyActiveXComponent
'Project Type: ActiveX DLL
'File Name: MyActiveXComponent.dll
'
'Class Name: MyClass
Option Explicit
Public Function Add(ByVal Num1 As Long, ByVal Num2 As Long) As Long
Add = Num1 + Num2
End Function
back to the topTroubleshoot
As with any other COM components, you must register the
MyActiveXComponent component before you use it. Make sure that you use a tool
such as Regsvr32.exe to register MyActiveXComponent.dll.
back to the top