It’s relatively easy to create an ActiveX control using the framework. The provided wizard does most of the work for you; the process is briefly described here.
The framework implements its core functionality in a few C++ classes, notably CAutomationObject, COleControl (which inherits from CAutomationObject), and CPropertyPage. All objects inherit from CUnknownObject, which provides the support for aggregation.
To write an ActiveX control, you need to declare a new object that inherits from COleControl. In addition, you need to inherit from some sort of Automation interface that describes the properties and methods for your control, such as IMyControl. This interface description is generated by the MkTypLib utility and is put in an output file created by MkTypLib (the CtlWiz sets up an environment where this file is named MyControlInterfaces.h). COleControl has a number of virtual methods that are declared as pure, which you simply must implement in your control class. These include WindowProc, LoadBinaryState, LoadTextState, SaveBinaryState, SaveTextState, OnDraw, and RegisterClassData.
To write a property page, you declare a new object that inherits from CPropertyPage. This object must implement a DialogProc function. You can also implement Automation objects and collections by declaring a new object that inherits from CAutomationObject.
Because an ActiveX control is an in-process OLE server, you also need one file to describe all your objects, whether they are controls, Automation objects, or property pages. This file includes a table of all objects and information about them. In addition, it has information about what sort of localization your server would like to use, and what sort of licensing support you'd like to have.
Finally, you need a resource file, an .odl file to describe your interfaces and event interfaces, a .def file to hold your linking information, and a file to define all the Globally Unique Identifiers (GUID) that have been declared.
Although you can create all this information by using one of the sample controls as a starting point for your project, it’s easier to use the control wizard to create a new control.
The control wizard is available from within Visual C++, and you can use it to create a new ActiveX control project. It is not a robust program and may fail if something unexpected occurs, but it will save you a considerable amount of time in the beginning.
To install and run the control wizard
When you run the control wizard for this release, you will receive a warning message (which you can ignore) if you create your control in an existing directory. Also, after you load your control’s project and compile it, you will receive some linker warnings about the structure of your .def file. You can resolve these warnings by removing the entries for PRTMODE, CODE, and DATA. You should also modify your EXPORTS section to look like this:
EXPORTS
DllRegisterServer @1 PRIVATE
DllUnregisterServer @2 PRIVATE
DllCanUnloadNow @3 PRIVATE
DllGetClassObject @4 PRIVATE