CALCDRIV Sample: Demonstrates an Automation Client Application

The CALCDRIV sample is a basic 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 basic calculator interface that resembles the Calculator application that is included in Windows.

Security noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

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 calcdriv.sln

  3. On the Build menu, click Build.

    Note

    If you do not build and register MFCCALC before you build CALCDRIV, you get the message, "Unable to create 'MFCCALC.Application' object".

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

To drive MFCCALC from CALCDRIV

  1. In the MFC Calc Driver 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 Accumand 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, for example, 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 that describes 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 Windows registration of the server by using REGEDIT /v (the verbose option).

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

  • CDriverDlg implements the CALCDRIV 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

This sample may not exactly demonstrate some features of the Visual C++ wizards, libraries, and compiler, but it still shows how to complete the intended task.

See Also

Other Resources

MFC Samples