Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Create and Use a C++ ActiveX component within a .NET environment

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
12 Feb 2009CPOL3 min read 70.4K   2.2K   39   6
This tutorial describes a decent way to create a C++ ActiveX component which could be fullly integrated within a .NET environment.

Introduction

Recently, I needed to make an ActiveX component in C++ to be used in my C# .NET program. However, I could not find any useful tutorial which could guide me through the dark world of C++ MFC. I started to do some investigation on how things work in C++/MFC and how C# reacts to importing an ActiveX component. If you like to know how to program in C++ or use the MFC library, then stop reading this tutorial. This tutorial is just a guide on how to create a decent ActiveX component that .NET environment can easily convert.

Background

More information about ActiveX can be found here.

Prepare your ActiveX

The first step we need to do is create the C++ MFC project:

Image 1

Give the project a name, and click on "OK", and later on "Finish". The funny thing about ActiveX is that you haven't yet coded anything, but the project is already filled with a lot of code. The ActiveX project uses MFC in a shared DLL. Change it into Static Library. Open the properties of the project, and change the "Use MFC" option into "Use MFC in a Static Library".

Image 2

So, why is this step required? Registering the ActiveX component on a different machine where no Visual Studio has been installed will fail. The ActiveX component will miss the MFC DLLs. If this is not clear yet, just play around with the Depends.Exe located here: C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin.

The ActiveX project creates five header files and six source files. These header/source files are required for this part:

CppActiveXCtrl.cpp
CppActiveXCtrl.h
CppActiveX.idl

First, create a method in the CppActiveXCtrl.h file, just where the AboutBox method is declared.

afx_msg void AboutBox();
afx_msg LONG TestMyMethod( LONG param1, LONG param2, BSTR param3 );

Create the method in the source file of this header. Return some value (it's just an example).

LONG CCppActiveXCtrl::TestMyMethod( LONG param1, LONG param2, BSTR param3 )
{
    return -5;
}

Every input method which could be used must have a unique identifier. So, create some unique identifiers. Be careful not to use Windows IDs which are declared in olectl.h.

Create this const in the header files CppActiveXCtrl.h and CppActiveX.idl [Note: you can also make a global ID for this (just whatever you like)]:

static const int DISPID_TEST_METHOD = 1025314;

or (select any style you like):

#define DISPID_TEST_METHOD (1025314)

Go to CppActiveX.idl and implement the following code in the dispinterface of the ActiveX. You can also find the AboutBox method here. Use the ID for your method as declared before. Every method should have its unique identifier.

dispinterface _DCppActiveX
{
   properties:
   methods:

   [id(DISPID_ABOUTBOX)] void AboutBox();
   [id(DISPID_TEST_METHOD)] LONG TestMyMethod(LONG param1, 
                            LONG param2, BSTR param3);

Go back to the CppActiveXCtrl.cpp file. Go to the Dispatch map part of this file. Add the following for your TestMyMethod. See this example:

BEGIN_DISPATCH_MAP(CCppActiveXCtrl, COleControl)
DISP_FUNCTION_ID(CCppActiveXCtrl, "AboutBox", 
                 DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
DISP_FUNCTION_ID(CCppActiveXCtrl, "TestMyMethod", 
                 DISPID_TEST_METHOD, TestMyMethod, 
                 VT_I4, VTS_I4 VTS_I4 VTS_BSTR)
END_DISPATCH_MAP()

Note: make sure you use the correct type of input. Otherwise, this could lead to some nasty exceptions.

OK, one method and one event should make it clear. Go to the Class View of Visual Studio, and select CCppActiveXCtrl. Hover on Add, and add this event:

Image 3

Add the following event with some default parameters. Just pick some....

Image 4

Now, compile the project.

Prepare your .NET project

Create a C# "Windows Application" project. I'm not going to explain how to create a C# .NET project here.

During the compilation of the ActiveX project, the ActiveX component "ocx" file will be registered automatically. If not, use the command line:

regsvr32 /i CppActiveX.ocx

Select the main form, and with the right mouse button, add an item.

Image 5

Select the COM components, and find the CppActiveX control. Press "OK" to add it to the toolbox.

Image 6

Drag the added ActiveX control to your form. Note: if you change the input methods for your ActiveX project, make sure you delete the obj directory as well as the bin directory.

Interop.CppActiveXLib.dll
AxInterop.CppActiveXLib.dll

Here is an example of the code when using the OCX file:

C#
public Form1()
{
    InitializeComponent();
    int k = axCppActiveX1.TestMyMthod(4,4,"Hi");
    axCppActiveX1.EventHandlerTest += 
      new AxCppActiveXLib._DCppActiveXEvents_EventHandlerTestEventHandler(
      axCppActiveX1_EventHandlerTest);
}

private void axCppActiveX1_EventHandlerTest( object sender, 
        _DCppActiveXEvents_EventHandlerTestEvent e )
{
    int age = e.age;
    string name = e.name;
    string surname = e.surname;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
I'm a C++ and C# .Net software engineer.

Comments and Discussions

 
GeneralMy vote of 3 Pin
basma osman5-Sep-10 1:11
basma osman5-Sep-10 1:11 
GeneralMIDL2035 Error Pin
JDMACK30-Jan-09 9:46
JDMACK30-Jan-09 9:46 
GeneralRe: MIDL2035 Error Pin
Hamed Ebrahimmalek8-Feb-09 20:45
Hamed Ebrahimmalek8-Feb-09 20:45 
JokeYour english Pin
Emil - Gabriel23-Jan-09 1:02
Emil - Gabriel23-Jan-09 1:02 
General[Message Deleted] [modified] PinPopular
Hamed Ebrahimmalek23-Jan-09 1:57
Hamed Ebrahimmalek23-Jan-09 1:57 
GeneralRe: Your english Pin
Emil - Gabriel23-Jan-09 2:10
Emil - Gabriel23-Jan-09 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.