CALCDRIV: Demonstrates an Automation Client Application

Click to open or copy the CALCDRIV project files.

CALCDRIV is a simple Automation (formerly OLE Automation) client application. CALCDRIV drives the MFCCALC sample application, which is an Automation server that provides basic calculator functions and is one of the OLE Software Development Kit samples. MFCCALC has a simple calculator interface that looks like the Calculator application that comes with Microsoft Windows®. MFCCALC can also demonstrate the use of ClassWizard's Automation Client option and a ClassWizard-created dispatch class.

Before you run CALCDRIV, first build the MFCCALC sample, then register it with Windows. If you run MFCCALC once, MFCCALC, like other MFC server applications, will automatically enter itself into the system registry. If you do not follow this procedure, you get an "Unable to create 'MFCCALC.Application' object" message.

When you run CALCDRIV, the Microsoft Foundation Class Library (MFC) Calc Driver dialog box appears and the MFCCALC application is launched. You can use MFCCALC's calculator user interface directly at this point, or you can drive MFCCALC from CALCDRIV as follows:

  1. In the Expression field, type two or more numbers separated by the operator +, -, *, or /. Note that MFCCALC does not interpret parentheses and does not implement operator precedence.

  2. Click Go to have MFCCALC evaluate the expression in one step.

  3. Click Single Step to step through the expression one number/operator at a time.

  4. Click Refresh to request state information for MFCCALC (Last Accum and Last Operand).

ClassWizard Automation Client Option

The CRemoteCalcDlg class of CALCDRIV represents the dispatch interface of MFCCALC. Class CRemoteCalcDlg is derived from CCmdTarget, which has a few automation-specific member functions, such as CreateDispatch.

It is easy to implement a dispatch class such as MFCCALC with ClassWizard if the automation server application has a type library (.tlb) file. To implement the header and implementation files, Calctype.h and Calctype.cpp:

  1. Select the Automation tab in ClassWizard.

  2. Click Read Type Library, which opens the Read Type Library dialog box.

  3. Select Mfccalc.tlb from the MFCCALC sample files as the type library file.

  4. In the Confirm Classes dialog box, change the Class Name from ICalcDlg to CRemoteCalcDlg.

  5. Change the header filename from Mfccalc.h to Calctype.h.

  6. Change the implementation filename from Mfccalc.cpp to Calctype.cpp.

You do not need to make any changes to the Calctype.h and Calctype.cpp files created by ClassWizard.

Using a ClassWizard-Created Dispatch Class

CALCDRIV uses the CRemoteCalcDlg class created by ClassWizard as follows:

This sample demonstrates the following keywords:

AfxMessageBox; AfxOleInit; CDialog::DoModal; CDialog::EndDialog; CDialog::OnInitDialog; CEdit::GetSel; CEdit::SetSel; COleDispatchDriver::AttachDispatch; COleDispatchDriver::CreateDispatch; COleDispatchDriver::GetProperty; COleDispatchDriver::InvokeHelper; COleDispatchDriver::SetProperty; CString::GetLength; CWinApp::Enable3dControls; CWinApp::InitInstance; CWnd::DoDataExchange; CWnd::GetWindowText; CWnd::SetWindowText; GetWindowText; afxMemDF; min; wsprintf