Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

ATL Wizards

Two wizards are supplied by Visual C++ to simplify the building of custom COM objects with ATL:

These wizards are discussed in the next two sections.

Using the ATL COM AppWizard

The easiest way to create an ATL project is to use the ATL COM AppWizard. This wizard creates a skeleton project for you based on the type of project you select (see Figure 29.1).

29fig01.gif

Figure 29.1 The ATL COM AppWizard.

You can build three types of projects using the ATL COM AppWizard:

If you chose to build your project as a DLL, you have three additional options that are enabled in the lower half of the wizard page:

Given a project named JensCoffee, the ATL COM AppWizard will create the following files for your ATL project:

stdafx.h Just as in an MFC project, stdafx.h has all the standard #include directives that will be built into a precompiled header.
stdafx.cpp Just as in an MFC project, stdafx.cpp is used to create the precompiled header.
JensCoffee.dsp The Visual C++ project file.
JensCoffee.dsw The Visual C++ workspace file.
JensCoffee.opt The Visual C++ workspace options file.
JensCoffee.ncb A binary file used by the Visual C++ ClassWizard.
JensCoffee.rc The project resource file, which initially contains version information for the project.
Resource.h The project resource header file.
JensCoffee.idl Contains MIDL definitions for the project.
JensCoffee.cpp Contains basic functions necessary for the module to interact with COM. DLL projects have a number of DllXxxx functions. EXE projects have CExeModule member functions.
JensCoffee.h This file is empty initially, but it is replaced by a more meaningful file after the MIDL compiler is run against the project's *.idl file.
JensCoffeeps.mk The makefile used to build the proxy/stub DLL if required for custom marshaling support.
JensCoffeeps.def The module definition file for the proxy/stub DLL.

If you create a DLL project that allows the proxy/stub code to be merged, the following files are created:

dlldata.c A source file that must be added to the project in order to merge proxy/stub code into the DLL.
dlldata.h The header file for dlldata.c.

The files are not automatically added to your project—you must follow the steps outlined later in the section "Merging the Proxy/Stub Code with Your DLL."

This file is created for all DLL-based projects:

JensCoffee.def The module definition file for the project.

Finally, the following file is created for all EXE and service projects:

JensCoffee.rgs The script file used to insert information into the System Registry.

Using the ATL Object Wizard

As discussed earlier, you use the ATL Object Wizard to add an ATL COM class to your project. You launch the ATL Object Wizard by choosing Insert, New ATL Object from the Visual C++ menu. The ATL Object Wizard appears, as shown in Figure 29.2.

29fig02.gif

Figure 29.2 The ATL Object Wizard.

You can choose from four categories of ATL classes to insert into your project:

Seven types of classes are offered in the Objects category:

After a component type has been selected, a property sheet is displayed that enables you to define attributes for the component. Some component types display specialized property pages specific to their object type. The MTS component includes an MTS page used only for that particular type of component, for example. All component types present a Names property page, as shown in Figure 29.3.

29fig03.gif

Figure 29.3 The Names property page contains naming attributes for ATL components.

Eight items are displayed on the Names property page:

The second tab for most component types is the Attributes property page, which is used to collect information about the component, such as its threading model (see Figure 29.4).

29fig04.gif

Figure 29.4 The Attributes property page contains characteristics for ATL components.

The Attributes property page enables you to define the following properties for the component:

Given a component named Latte, the ATL Object Wizard creates the following files:

Latte.cpp Contains the implementation of the CLatte class. Initially, this file is empty.
Latte.h Contains the header file for the CLatte class.
Latte.rgs The script file used to insert information about CLatte into the Registry.

The ATL Object Wizard also modifies the following files:

JensCoffee.cpp Modified to add CLSID_Latte to the module's object map.
JensCoffee.idl Modified to add the new ILatte interface.

Merging the Proxy/Stub Code with Your DLL

If your component uses custom interfaces, you must supply a DLL that contains a marshaling proxy and stub code. Normally, you compile a separate proxy/stub DLL that must be distributed and registered with your component. This DLL is created using the proxy/stub makefile that is created as part of the initial project. This file has the name <project>ps.mk. The JensCoffee project, for example, has a makefile named JensCoffeeps.mk that is used to create the proxy/stub DLL.

As discussed earlier in this chapter, the ATL COM AppWizard provides an option to allow proxy/stub DLL to be merged into the component's server DLL. If you select this option, the dlldatax.c and dlldatax.h files are added to your project. They will not be included in the build, however.

If you want to merge the proxy/stub into your DLL, you must follow these steps:

  1. Go to the FileView tab in the Project Workspace and right-click on the dlldatax.c file. Choose Settings from the pop-up menu. The Project Settings dialog box appears.
  2. On the General tab in the Project Settings dialog box, clear the Exclude File from Build check box. Keep the dialog box open.
  3. Repeat this procedure for the dlldatax.h header file.
  4. Click on the dlldatax.c file icon in the Project Workspace.
  5. Keep the dialog box open and select the C++ tab.
  6. Choose the Precompiled Headers category from the drop-down list, and select the Not Using Precompiled Headers radio button.
  7. Choose the Preprocessor category, and add _MERGE_PROXYSTUB as a preprocessor definition. Make sure the new symbol is separated from the previous symbol by a comma.
  8. Click OK to close the dialog box.

After the project has been built, the DLL that contains the component also will contain the code required for proxy/stub marshaling.

Share ThisShare This

Informit Network