Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Designing an MFC Automation Server

This section shows you how to create your own automation server with MFC. You'll use MFC AppWizard and ClassWizard to quickly and easily create the necessary class and methods for the demonstration.

The example is an in-process DLL server that exposes a class called SimpleMFC. You'll create four methods—Add, Subtract, Multiply, and Divide—to see how an automation server built with MFC works. The first three methods accept two long numbers and return a long (the result of the calculation). The fourth method also accepts two long values but instead returns a double precision number.

Using MFC AppWizard to Create Automation Servers

Choose File | New from the Developer Studio menu, and then choose MFC AppWizard (DLL). In the Project Name edit box, type MFCAuto.

AppWizard presents a single-step process to create the server. Enable the Automation check box and click Finish (see Figure 27.14).

27fig14.gif

Figure 27.14 Step 1 of 1.

A Closer Look at the MFCAuto Classes

The AppWizard used to generate this project is very simple. The only code it generated is the required CLSID (because you enabled the Automation check box) and the registration of the DLL in the InitInstance() method, as Listing 27.15 shows.

Example 27.15. Registration Code for the Automation Server

BOOL CMFCAutoApp::InitInstance()
{
 // Register all OLE server (factories) as running.  This enables the
 //  OLE libraries to create objects from other applications.
 COleObjectFactory::RegisterAll();

 return TRUE;
}
						

Adding Customization to the MFCAuto Sample Skeleton

To make the server useable, you'll need to create a class derived from CCmdTarget. This is the root automation class provided by MFC.

Now do the following:

  1. In the workspace window, select the MFCAuto project, right-click, and choose New Class.
  2. In the Name text box, type CSimpleMFC.
  3. In the Base Class combo box, select CCmdTarget.
  4. For automation type, select Createable by Type ID and make sure that the text in the text box says MFCAuto.SimpleMFC.

Figure 27.15 shows the values for the new class.

27fig15.gif

Figure 27.15 Adding the CSimpleMFC class.

Notice the addition of a new entry in the workspace window. The CSimpleMFC node is the C++ class you just created. The project's Object Definition Language (ODL) file also has been modified to include information about the new SimpleMFC COM class. The ODL file will be used to create a type library to expose a SimpleMFC class to other applications. To see this ODL file (shown in Listing 27.16), double-click the MFCAuto.ODL icon on the workspace File tab. Included in this file is the UUID for the class. After we finish coding this example, it will include our user-defined methods Add, Subtract, Multiply, and Divide.

Example 27.16. Object Definition Language (ODL) for MFCAuto

// MFCAuto.odl : type library source for MFCAuto.dll
// This file will be processed by the MIDL compiler to produce the
// type library (MFCAuto.tlb).

[ uuid(CF5B9E04-72A3-11D3-B515-CC43834E4167), version(1.0) ]
library MFCAuto
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");

    //  Primary dispatch interface for CSimpleMFC

    [ uuid(CF5B9E11-72A3-11D3-B515-CC43834E4167) ]
    dispinterface ISimpleMFC
    {
        properties:
  // NOTE - ClassWizard will maintain property information here.
  //    Use extreme caution when editing this section.
  //{ { AFX_ODL_PROP(CSimpleMFC)
  //} } AFX_ODL_PROP

        methods:
  // NOTE - ClassWizard will maintain method information here.
  //    Use extreme caution when editing this section.
  //{ { AFX_ODL_METHOD(CSimpleMFC)
  //} } AFX_ODL_METHOD
    };

    //  Class information for CSimpleMFC
    [ uuid(CF5B9E12-72A3-11D3-B515-CC43834E4167) ]
    coclass SimpleMFC
    {
        [default] dispinterface ISimpleMFC;
    };

    //{ { AFX_APPEND_ODL} }
    //} } AFX_APPEND_ODL} }
};
						

The CSimpleMFC class that was generated is fairly simple; however, note the OnFinalRelease() method generated as a result of choosing automation support. As the code documentation states, this notification is called when the last instance of this class is deleted. You can optionally implement this code to perform special cleanup.

Adding Methods to the MFCAuto Example

Listing 27.17 shows the prototypes for the four methods you will be adding. This will come in handy in just a moment.

Example 27.17. CSimpleMFC Instance Method Prototypes

long Add(long First, long Second);
long Subtract(long First, long Second);
long Multiply(long First, long Second);
double Divide(long First, long Second);

Now, add these prototypes with these steps:

  1. Open ClassWizard by choosing View | ClassWizard, or by pressing Ctrl+W.
  2. Select the Automation tab in the MFC ClassWizard dialog box. In the Class Name combo box, select CSimpleMFC.
  3. Click the Add Method button.
  4. In the External Name combo box, type Add (the Internal Name text box is filled in automatically).
  5. In the Return Type combo box, select Long.
  6. Add the first and second methods in the Parameter List list box, using Long as the type for each.

Repeat these steps for each of the four methods and select Double as the return type for the Divide method. You can use Figure 27.16 as a guideline.

27fig16.gif

Figure 27.16 Using ClassWizard to add methods to MFCAuto.

Now, change the implementation code for these methods, as Listing 27.18 shows.

Example 27.18. Implementing the CSimpleMFC Instance Methods

/////////////////////////////////////////////////////////////////////////////
// CSimpleMFC message handlers

long CSimpleMFC::Add(long First, long Second)
{
 return First + Second;
}

long CSimpleMFC::Subtract(long First, long Second)
{
 return First - Second;
}

long CSimpleMFC::Multiply(long First, long Second)
{
 return First * Second;
}

double CSimpleMFC::Divide(long First, long Second)
{
 return (double)First / (double)Second;
}
							

Take another look at the declaration for the CSimpleMFC class shown in Listing 27.19. ClassWizard has added prototypes for the methods to the ODL file. When this class is registered, the methods automatically are visible as part of the SimpleMFC COM class.

Example 27.19. ClassWizard Changes to the ODL File for New Methods

  methods:
   // NOTE - ClassWizard will maintain method information here.
   //    Use extreme caution when editing this section.
   //{{AFX_ODL_METHOD(CSimpleMFC)
   [id(1)] long Add(long First, long Second);
   [id(2)] long Subtract(long First, long Second);
   [id(3)] long Multiply(long First, long Second);
   [id(4)] double Divide(long First, long Second);
   //}}AFX_ODL_METHOD
							

Testing the MFCAuto Example

Compile the server example and then choose Tools | Register Control to register this automation server with the operating system. You can test the example using any application that can create automation servers.

For the test, I've included a file that VBScript tests that can be run against the MFCAuto example. This script, Test.vbs, runs under the Windows scripting host and calls the automation interfaces in the MFCAuto server. Listing 27.20 provides the contents of Test.vbs.

Example 27.20. An Automation Test File Written in VBScript

    ' Test.vbs
    ' VBScript example of an Automation test file.
    ' This file tests the MFCAuto example.

    ' The scripting host shell object is used to supply
    ' interaction with the user.

    dim Result, AutoObj, WshShell

    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    Set AutoObj = CreateObject("MFCAuto.SimpleMFC")

    Result = AutoObj.Add(1, 2)
    WshShell.Popup "1 + 2 = " + cstr(Result)

    Result = AutoObj.Subtract(10, 5)
    WshShell.Popup "10 - 5 = " + cstr(Result)

    Result = AutoObj.Multiply(23, 47)
    WshShell.Popup "23 x 47 = " + cstr(Result)

    Result = AutoObj.Divide(10, 3)
    WshShell.Popup "10 / 3 = " + cstr(Result)
						

The Test.vbs script file is located in the same directory as the MFCAuto project. To run the script file, double-click the file's icon using Windows Explorer. If the MFCAuto project is registered properly, the script displays a series of dialog boxes containing calculation results.

Share ThisShare This

Informit Network