Creating an ActiveX Control

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.

Using the Control Wizard

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

  1. Follow the installation instructions in the readme file for this SDK.

  2. In Visual C++, open the Tools menu and click Customize.

  3. Select the Add-ins and Macro Files tab.

  4. Browse for and open ADBSDK\bin\AFCWiz.dll. This creates a custom toolbar with an icon for the control wizard. Alternately, you can place the control wizard icon in a menu or on a toolbar by dragging it from the Command tab (with the Add-ins category selected) to the desired menu or toolbar.

  5. To run the control wizard, just click the control wizard icon.

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