Click here to Skip to main content
15,892,643 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.6K   2.2K   39  
This tutorial describes a decent way to create a C++ ActiveX component which could be fullly integrated within a .NET environment.
// CppActiveX.idl : type library source for ActiveX Control project.

// This file will be processed by the MIDL compiler tool to
// produce the type library (CppActiveX.tlb) that will become a resource in
// CppActiveX.ocx.

#include <olectl.h>
#include <idispids.h>

#define DISPID_TEST_METHOD (1025314)

[ uuid(4143F1BD-B6F2-4E6D-B45A-06399CE41154), version(1.0),
  helpfile("CppActiveX.hlp"),
  helpstring("CppActiveX ActiveX Control module"),
  control ]
library CppActiveXLib
{
	importlib(STDOLE_TLB);

	//  Primary dispatch interface for CCppActiveXCtrl

	[ uuid(CDE8EC88-7703-4021-9D19-3F23A4B7BFF0),
	  helpstring("Dispatch interface for CppActiveX Control")]
	dispinterface _DCppActiveX
	{
		properties:
		methods:

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

	//  Event dispatch interface for CCppActiveXCtrl

	[ uuid(DA8D1408-D0BE-48BF-961B-158C705C0AFD),
	  helpstring("Event interface for CppActiveX Control") ]
	dispinterface _DCppActiveXEvents
	{
		properties:
			//  Event interface has no properties

		methods:

            [id(1)] void EventHandlerTest(BSTR name, BSTR surname, LONG age);
    };

	//  Class information for CCppActiveXCtrl

	[ uuid(492FE2C4-EB74-4D41-8278-B4E290603118),
	  helpstring("CppActiveX Control"), control ]
	coclass CppActiveX
	{
		[default] dispinterface _DCppActiveX;
		[default, source] dispinterface _DCppActiveXEvents;
	};

};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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