CALCDRIV Sample: Demonstrates an Automation Client Application

The CALCDRIV sample 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. MFCCALC has a simple calculator interface that looks like the Calculator application that comes with Microsoft Windows.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

To build and run the CALCDRIV sample

  1. Follow the instructions in MFCCALC to build and run that project.

  2. Open the solution calcdriv.sln

  3. On the Build menu, click Build.

    Note

    If you do not build and register MFCCALC before building CALCDRIV, 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 MFC Calc Driver's Expression box, provide two or more numbers separated by the operator +, -, *, or /.

    Note

    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 or operator at a time.

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

Using a Dispatch Class

CALCDRIV uses the CRemoteCalcDlg class as follows:

  • 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.

  • CDriverDlg embeds a CRemoteCalcDlg object, m_calc. The CRemoteCalcDlg object, like the CDriverDlg object in which it is embedded, is alive for most of the duration of CALCDRIV. The CRemoteCalcDlg is constructed when the dialog object is constructed.

  • CDriverDlg::OnInitDialog calls CCmdTarget::CreateDispatch for the CRemoteCalcDlg object. CreateDispatch requires the dispatch name as the first parameter. Typically, the developer of an automation server application provides documentation describing the names of the dispatch interfaces and the properties and methods of the interfaces. Another way to find the names of the dispatch interfaces of an automation server application is to look at the server's Windows registration, using REGEDIT /v (the verbose option).

  • CDriverDlg implements CALCDRIV's expression evaluator by calling the Button method of MFCCALC, which is an emulator for the various buttons in the calculator's dialog box.

  • CDriverDlg implements CALCDRIV's Refresh function by calling the GetOpnd and GetAccum methods of MFCCALC.

  • The CDriverDlg destructor calls the Quit method exposed by MFCCALC to shut down MFCCALC when CALCDRIV closes.

Keywords

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::InitInstance; CWnd::DoDataExchange; CWnd::GetWindowText; CWnd::SetWindowText; GetWindowText; afxMemDF; min; wsprintf

Note

Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.

See Also

Other Resources

MFC Samples