home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_2.iso / windows / program / activex / axtsamp.exe / TSBRANCH.EXE / DLLCLIEN / DLLCLIEN.CPP next >
C/C++ Source or Header  |  1996-12-29  |  41KB  |  1,216 lines

  1. /*+==========================================================================
  2.   File:      DLLCLIEN.CPP
  3.  
  4.   Summary:   Based largely on the DLLUSER.EXE application code, this
  5.              module is meant to use COM to load and access some COM
  6.              components in a separate DLL COM Server (DLLSERVE built
  7.              in the sibling DLLSERVE directory).  Thus to run DLLCLIEN
  8.              you must build DLLSERVE first.  Like DLLUSER, this app
  9.              composes its own native composite COM object,
  10.              COUtilityCruiseCar, by aggregating an the existing
  11.              COCruiseCar COM component (provided by the DLLSERVE.DLL
  12.              server) with a native implementation of the IUtility
  13.              interface.  Thus, this app showcases nested aggregation
  14.              using COM's client/server protocol.
  15.  
  16.              Also like COMUSER this client app also instantiates the other
  17.              car-related objects of DLLSERVE and exercises them (ie, for
  18.              Car, UtilityCar, CruiseCar, and UtilityCruiseCar).  To this
  19.              end, DLLCLIEN provides a set of menus for these Car related
  20.              objects with selections for the respective methods of the
  21.              Interfaces exposed by those COM Objects.  DLLCLIEN also
  22.              exploits DLLSERVE's CarSample component to setup operation of
  23.              that server as a code sample that supports the Client's trace
  24.              LOGging macros.  After this is set up the server logs to the
  25.              Client's logging facility.
  26.  
  27.              For a comprehensive tutorial code tour of DLLCLIEN's
  28.              contents and offerings see the tutorial DLLCLIEN.HTM file.
  29.              For more specific technical details on the internal workings
  30.              see the comments dispersed throughout the DLLCLIEN source code.
  31.              For more details on the DLLSERVE.DLL that DLLCLIEN works with
  32.              see the DLLSERVE.HTM file in the main tutorial directory.
  33.  
  34.   Classes:   CMainWindow
  35.  
  36.   Functions: InitApplication, WinMain
  37.  
  38.   Origin:    9-20-95: atrent - Editor-inheritance from the DLLUSER source.
  39.  
  40. ----------------------------------------------------------------------------
  41.   This file is part of the Microsoft ActiveX Tutorial Code Samples.
  42.  
  43.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  44.  
  45.   This source code is intended only as a supplement to Microsoft
  46.   Development Tools and/or on-line documentation.  See these other
  47.   materials for detailed information regarding Microsoft code samples.
  48.  
  49.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  50.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  51.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  52.   PARTICULAR PURPOSE.
  53. ==========================================================================+*/
  54.  
  55. /*--------------------------------------------------------------------------
  56.   We include WINDOWS.H for all Win32 applications.
  57.   We include OLE2.H because we will be calling the COM/OLE libraries.
  58.   We include INITGUID.H only once (here) in the entire app because we
  59.     will be defining GUIDs and want them as constants in the data segment.
  60.   We include COMMDLG.H because we will be using the Open File and
  61.     potentially other Common dialogs.
  62.   We include APPUTIL.H because we will be building this application using
  63.     the convenient Virtual Window and Dialog classes and other
  64.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  65.   We include ICARS.H and CARGUIDS.H for the common car-related Interface
  66.     class, GUID, and CLSID specifications.
  67.   We include DLLCLIEN.H because it has class and resource definitions
  68.     specific to this DLLCLIEN application.
  69.   We include UTCRUCAR.H because it has COUtilityCar object class.
  70. ---------------------------------------------------------------------------*/
  71. #include <windows.h>
  72. #include <ole2.h>
  73. #include <initguid.h>
  74. #include <commdlg.h>
  75. #include <apputil.h>
  76. #include <icars.h>
  77. #include <carguids.h>
  78. #include "dllclien.h"
  79. #include "utcrucar.h"
  80.  
  81.  
  82. // Here is a pointer for use by the global Trace Message logging macros.
  83. CMsgLog* g_pMsgLog;
  84.  
  85.  
  86. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  87.   Method:   CMainWindow::CMainWindow
  88.  
  89.   Summary:  CMainWindow Constructor.
  90.  
  91.   Args:     .
  92.  
  93.   Modifies: .
  94.  
  95.   Returns:  .
  96. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  97. CMainWindow::CMainWindow()
  98. {
  99.   // Ensure these member variable strings are null strings.
  100.   m_szFileName[0] = 0;
  101.   m_szFileTitle[0] = 0;
  102.  
  103.   // Fill in the Open File Name Common Dialog's OPENFILENAME structure.
  104.   m_ofnFile.lStructSize = sizeof(OPENFILENAME);
  105.   m_ofnFile.hwndOwner = m_hWnd;
  106.   m_ofnFile.hInstance = m_hInst;
  107.   m_ofnFile.lpstrFilter = TEXT(OFN_DEFAULTFILES_STR);
  108.   m_ofnFile.lpstrCustomFilter = NULL;
  109.   m_ofnFile.nMaxCustFilter = 0;
  110.   m_ofnFile.nFilterIndex = 1;
  111.   m_ofnFile.lpstrFile = m_szFileName;
  112.   m_ofnFile.nMaxFile = MAX_PATH;
  113.   m_ofnFile.lpstrInitialDir = TEXT(".");
  114.   m_ofnFile.lpstrFileTitle = m_szFileTitle;
  115.   m_ofnFile.nMaxFileTitle = MAX_PATH;
  116.   m_ofnFile.lpstrTitle = TEXT(OFN_DEFAULTTITLE_STR);
  117.   m_ofnFile.lpstrDefExt = NULL;
  118.   m_ofnFile.Flags = OFN_HIDEREADONLY;
  119.  
  120.   m_pCar = NULL;
  121.   m_pUtilityCar = NULL;
  122.   m_pCruiseCar = NULL;
  123.   m_pUtilityCruiseCar = NULL;
  124.   m_pCarSample = NULL;
  125. }
  126.  
  127.  
  128. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  129.   Method:   CMainWindow::~CMainWindow
  130.  
  131.   Summary:  CMainWindow Destructor.  Destruction of the main window
  132.             indicates that the application should quit and thus the
  133.             PostQuitMessage API is called.
  134.  
  135.   Args:     .
  136.  
  137.   Modifies: .
  138.  
  139.   Returns:  .
  140. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  141. CMainWindow::~CMainWindow()
  142. {
  143.   // CMainWindow is derived from CVirWindow which traps the WM_DESTROY
  144.   // message and causes a delete of CMainWindow which in turn causes this
  145.   // destructor to run. The WM_DESTROY results when the window is destoyed
  146.   // after a close of the window. Prior to exiting the main message loop:
  147.  
  148.   // We release any and all of the pointers to instantiated COM objects.
  149.   RELEASE_INTERFACE(m_pCar);
  150.   RELEASE_INTERFACE(m_pUtilityCar);
  151.   RELEASE_INTERFACE(m_pCruiseCar);
  152.   RELEASE_INTERFACE(m_pUtilityCruiseCar);
  153.   RELEASE_INTERFACE(m_pCarSample);
  154.  
  155.   // We delete the CMsgBox and CMsgLog objects that were made in
  156.   // Initinstance.
  157.   DELETE_POINTER(m_pMsgBox);
  158.   DELETE_POINTER(m_pMsgLog);
  159.  
  160.   // We then post a WM_QUIT message to cause an exit of the main thread's
  161.   // message loop and an exit of this instance of the application.
  162.   PostQuitMessage(0);
  163. }
  164.  
  165.  
  166. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  167.   Method:   CMainWindow::InitInstance
  168.  
  169.   Summary:  Instantiates an instance of the main application window.
  170.             This method must be called only once, immediately after
  171.             window class construction.  We take care to delete 'this'
  172.             CMainWindow if we must return the error condition FALSE.
  173.  
  174.   Args:     HINSTANCE hInstance,
  175.               Handle of the application instance.
  176.             int nCmdShow)
  177.               Command to pass to ShowWindow.
  178.  
  179.   Modifies: m_szHelpFile, m_pMsgBox, m_pMsgLog.
  180.  
  181.   Returns:  BOOL.
  182.               TRUE if succeeded.
  183.               FALSE if failed.
  184. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  185. BOOL CMainWindow::InitInstance(
  186.        HINSTANCE hInstance,
  187.        int nCmdShow)
  188. {
  189.   BOOL bOk = FALSE;
  190.   HWND hWnd;
  191.  
  192.   // Create the Message Box and Message Log objects.
  193.   m_pMsgBox = new CMsgBox;
  194.   m_pMsgLog = new CMsgLog;
  195.  
  196.   if (NULL != m_pMsgBox && NULL != m_pMsgLog)
  197.   {
  198.     // Note, the Create method sets the m_hWnd member so we don't
  199.     // need to set it explicitly here first.
  200.  
  201.     // Here is the create of this window.  Size the window reasonably.
  202.     // Create sets both m_hInst and m_hWnd.
  203.     hWnd = Create(
  204.              TEXT(MAIN_WINDOW_CLASS_NAME_STR),
  205.              TEXT(MAIN_WINDOW_TITLE_STR),
  206.              WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
  207.                | WS_MAXIMIZEBOX | WS_THICKFRAME,
  208.              CW_USEDEFAULT,
  209.              CW_USEDEFAULT,
  210.              ::GetSystemMetrics(SM_CXSCREEN)*3/5,
  211.              ::GetSystemMetrics(SM_CYSCREEN)*3/5,
  212.              NULL,
  213.              NULL,
  214.              hInstance);
  215.     if (hWnd)
  216.     {
  217.       // Ensure the new window is shown on screen and its content is painted.
  218.       ::ShowWindow(m_hWnd, nCmdShow);
  219.       ::UpdateWindow(m_hWnd);
  220.  
  221.       // Build a path to where the help file should be (it should be in
  222.       // the same directory as the .EXE but with the .HLP extension.
  223.       MakeFamilyPath(hInstance, m_szHelpFile, TEXT(HELP_FILE_EXT));
  224.  
  225.       // Init the Message Box object.
  226.       if (m_pMsgBox->Init(m_hInst, m_hWnd))
  227.       {
  228.         // Create the Trace Message Log ListBox as a child window that
  229.         //   fits the client area of the Main Window (the TRUE 3rd argument
  230.         //   specifies such an inside child).
  231.         // If you want the Trace Message Log in a separate (but owned)
  232.         //   window, then pass a FALSE instead for the 3rd argument.
  233.         if (m_pMsgLog->Create(m_hInst, m_hWnd, TRUE))
  234.         {
  235.           HRESULT hr;
  236.  
  237.           // Assign the global MsgLog pointer.
  238.           g_pMsgLog = m_pMsgLog;
  239.           // Use macro to log an initial start messsage.
  240.           LOGID(IDS_START_MESSAGE_LOG);
  241.           // Now ask COM for the ISample interface to the server's
  242.           // CarSample component.  This effectively loads the Server DLL.
  243.           LOG("C: Start DLLSERVE Server Trace Logging.");
  244.           hr = CoCreateInstance(
  245.                  CLSID_DllCarSample,
  246.                  NULL,
  247.                  CLSCTX_INPROC_SERVER,
  248.                  IID_ISample,
  249.                  (PPVOID)&m_pCarSample);
  250.           if (SUCCEEDED(hr))
  251.           {
  252.             hr = m_pCarSample->Init(m_hWnd, (PVOID)g_pMsgLog);
  253.             // Signal if we succeeded in initializing the app.
  254.             bOk = SUCCEEDED(hr);
  255.             if (!bOk)
  256.             {
  257.               RELEASE_INTERFACE(m_pCarSample);
  258.               // We ask COM to unload any unused COM Servers.
  259.               CoFreeUnusedLibraries();
  260.             }
  261.           }
  262.           else
  263.             m_pMsgBox->ErrorID(IDS_NOSERVER);
  264.         }
  265.       }
  266.     }
  267.   }
  268.  
  269.   if (!bOk)
  270.   {
  271.     DELETE_POINTER(m_pMsgBox);
  272.     DELETE_POINTER(m_pMsgLog);
  273.   }
  274.  
  275.   return (bOk);
  276. }
  277.  
  278.  
  279. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  280.   Method:   CMainWindow::GetInterface
  281.  
  282.   Summary:  Convenience method that wraps the QueryInterface call
  283.             and accepts a main COM object IUnknown pointer as an argument.
  284.  
  285.   Args:     IUnknown* pObj,
  286.               Pointer to the COM Object we are getting an interface on.
  287.             REFIID riid,
  288.               The GUID for the interface that we are seeking.
  289.             PPVOID ppv);
  290.               Address of the caller's pointer variable that will
  291.               receive the requested interface pointer.
  292.  
  293.   Modifies: .
  294.  
  295.   Returns:  BOOL.
  296.               TRUE if succeeded.
  297.               FALSE if failed.
  298. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  299. BOOL CMainWindow::GetInterface(
  300.        IUnknown* pObj,
  301.        REFIID riid,
  302.        PPVOID ppv)
  303. {
  304.   BOOL bResult = FALSE;
  305.   HRESULT hr;
  306.  
  307.   *ppv=NULL;
  308.  
  309.   if (NULL != pObj)
  310.   {
  311.     LOG("C: --Obtaining Interface Pointer.");
  312.     hr=pObj->QueryInterface(riid, ppv);
  313.  
  314.     if (FAILED(hr))
  315.     {
  316.       LOGERROR("C: ???? QueryInterface", hr);
  317.     }
  318.     else
  319.     {
  320.       LOGF1("C: Interface obtained. *ppv=0x%X", *ppv);
  321.       bResult = TRUE;
  322.     }
  323.   }
  324.   else
  325.   {
  326.     LOG("C: ???? Create an object first.");
  327.   }
  328.  
  329.   return (bResult);
  330. }
  331.  
  332.  
  333. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  334.   Method:   CMainWindow::DoMenu
  335.  
  336.   Summary:  Dispatch and handle the main menu commands.
  337.  
  338.   Args:     WPARAM wParam,
  339.               First message parameter (word sized).
  340.             LPARAM lParam)
  341.               Second message parameter (long sized).
  342.  
  343.   Modifies: m_ofnFile, ...
  344.  
  345.   Returns:  LRESULT
  346.               Standard Windows WindowProc return value.
  347. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  348. LRESULT CMainWindow::DoMenu(
  349.           WPARAM wParam,
  350.           LPARAM lParam)
  351. {
  352.   LRESULT lResult = FALSE;
  353.   HRESULT hr;
  354.   HMENU hMenu  = ::GetMenu(m_hWnd);
  355.   // Here are some interface pointers used to call methods on
  356.   // our COUtilityCar, COCruiseCar, and COUtilityCruiseCar COM objects.
  357.   ICar* pICar;
  358.   IUtility* pIUtility;
  359.   ICruise* pICruise;
  360.  
  361.   switch (LOWORD(wParam))
  362.   {
  363.     //----------------------------------------------------------------------
  364.     // Handle File Menu Commands.
  365.     //----------------------------------------------------------------------
  366.     case IDM_FILE_EXIT:
  367.       // The user commands us to exit this application so we tell the
  368.       // Main window to close itself.
  369.       ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  370.       break;
  371.  
  372.     //----------------------------------------------------------------------
  373.     // Handle Car Menu Commands.
  374.     //----------------------------------------------------------------------
  375.     case IDM_CAR_CREATE:
  376.       LOG("C: === Car Menu: Create.");
  377.       if (NULL == m_pCar)
  378.       {
  379.         // Call COM service to create an instance.
  380.         hr = CoCreateInstance(
  381.                CLSID_DllCar,
  382.                NULL,
  383.                CLSCTX_INPROC_SERVER,
  384.                IID_IUnknown,
  385.                (PPVOID)&m_pCar);
  386.         if (SUCCEEDED(hr))
  387.         {
  388.           ::CheckMenuItem(
  389.               hMenu,
  390.               IDM_CAR_CREATE,
  391.               MF_BYCOMMAND | MF_CHECKED);
  392.         }
  393.         else
  394.         {
  395.           LOGERROR("C: ???? CoCreateInstance",hr);
  396.         }
  397.       }
  398.       else
  399.         LOG("C: ???? Car already exists.");
  400.       break;
  401.     case IDM_CAR_RELEASE:
  402.       LOG("C: === Car Menu: Release.");
  403.       if (NULL != m_pCar)
  404.       {
  405.         RELEASE_INTERFACE(m_pCar);
  406.         // We ask COM to unload any unused COM Servers.
  407.         CoFreeUnusedLibraries();
  408.         ::CheckMenuItem(
  409.             hMenu,
  410.             IDM_CAR_CREATE,
  411.             MF_BYCOMMAND | MF_UNCHECKED);
  412.       }
  413.       else
  414.         LOG("C: ???? No Car to Release.");
  415.       break;
  416.     case IDM_CAR_SHIFT:
  417.       LOG("C: === Car Menu: ICar::Shift");
  418.       if (GetInterface(m_pCar, IID_ICar, (PPVOID)&pICar))
  419.       {
  420.         LOG("C: --Calling pICar->Shift");
  421.         pICar->Shift(1);
  422.         LOG("C: --Releasing pICar");
  423.         pICar->Release();
  424.       }
  425.       break;
  426.     case IDM_CAR_CLUTCH:
  427.       LOG("C: === Car Menu: ICar::Clutch");
  428.       if (GetInterface(m_pCar, IID_ICar, (PPVOID)&pICar))
  429.       {
  430.         LOG("C: --Calling pICar->Clutch");
  431.         pICar->Clutch(100);
  432.         LOG("C: --Releasing pICar");
  433.         pICar->Release();
  434.       }
  435.       break;
  436.     case IDM_CAR_SPEED:
  437.       LOG("C: === Car Menu: ICar::Speed");
  438.       if (GetInterface(m_pCar, IID_ICar, (PPVOID)&pICar))
  439.       {
  440.         LOG("C: --Calling pICar->Speed");
  441.         pICar->Speed(20);
  442.         LOG("C: --Releasing pICar");
  443.         pICar->Release();
  444.       }
  445.       break;
  446.     case IDM_CAR_STEER:
  447.       LOG("C: === Car Menu: ICar::Steer");
  448.       if (GetInterface(m_pCar, IID_ICar, (PPVOID)&pICar))
  449.       {
  450.         LOG("C: --Calling pICar->Steer");
  451.         pICar->Steer(0);
  452.         LOG("C: --Releasing pICar");
  453.         pICar->Release();
  454.       }
  455.       break;
  456.  
  457.     //----------------------------------------------------------------------
  458.     // Handle UtilityCar Menu Commands.
  459.     //----------------------------------------------------------------------
  460.     case IDM_UCAR_CREATE:
  461.       LOG("C: === UtilityCar Menu: Create.");
  462.       if (NULL == m_pUtilityCar)
  463.       {
  464.         // Call COM service to create an instance.
  465.         hr = CoCreateInstance(
  466.                CLSID_DllUtilityCar,
  467.                NULL,
  468.                CLSCTX_INPROC_SERVER,
  469.                IID_IUnknown,
  470.                (PPVOID)&m_pUtilityCar);
  471.         if (SUCCEEDED(hr))
  472.         {
  473.           ::CheckMenuItem(
  474.               hMenu,
  475.               IDM_UCAR_CREATE,
  476.               MF_BYCOMMAND | MF_CHECKED);
  477.         }
  478.         else
  479.         {
  480.           LOGERROR("C: ???? CoCreateInstance",hr);
  481.         }
  482.       }
  483.       else
  484.         LOG("C: ???? UtilityCar already exists.");
  485.       break;
  486.     case IDM_UCAR_RELEASE:
  487.       LOG("C: === UtilityCar Menu: Release.");
  488.       if (NULL != m_pUtilityCar)
  489.       {
  490.         RELEASE_INTERFACE(m_pUtilityCar);
  491.         // We ask COM to unload any unused COM Servers.
  492.         CoFreeUnusedLibraries();
  493.         ::CheckMenuItem(
  494.             hMenu,
  495.             IDM_UCAR_CREATE,
  496.             MF_BYCOMMAND | MF_UNCHECKED);
  497.       }
  498.       else
  499.         LOG("C: ???? No UtilityCar to Release.");
  500.       break;
  501.     case IDM_UCAR_SHIFT:
  502.       LOG("C: === UtilityCar Menu: ICar::Shift");
  503.       if (GetInterface(m_pUtilityCar, IID_ICar, (PPVOID)&pICar))
  504.       {
  505.         LOG("C: --Calling pICar->Shift");
  506.         pICar->Shift(2);
  507.         LOG("C: --Releasing pICar");
  508.         pICar->Release();
  509.       }
  510.       break;
  511.     case IDM_UCAR_CLUTCH:
  512.       LOG("C: === UtilityCar Menu: ICar::Clutch");
  513.       if (GetInterface(m_pUtilityCar, IID_ICar, (PPVOID)&pICar))
  514.       {
  515.         LOG("C: --Calling pICar->Clutch");
  516.         pICar->Clutch(100);
  517.         LOG("C: --Releasing pICar");
  518.         pICar->Release();
  519.       }
  520.       break;
  521.     case IDM_UCAR_SPEED:
  522.       LOG("C: === UtilityCar Menu: ICar::Speed");
  523.       if (GetInterface(m_pUtilityCar, IID_ICar, (PPVOID)&pICar))
  524.       {
  525.         LOG("C: --Calling pICar->Speed");
  526.         pICar->Speed(30);
  527.         LOG("C: --Releasing pICar");
  528.         pICar->Release();
  529.       }
  530.       break;
  531.     case IDM_UCAR_STEER:
  532.       LOG("C: === UtilityCar Menu: ICar::Steer");
  533.       if (GetInterface(m_pUtilityCar, IID_ICar, (PPVOID)&pICar))
  534.       {
  535.         LOG("C: --Calling pICar->Steer");
  536.         pICar->Steer(10);
  537.         LOG("C: --Releasing pICar");
  538.         pICar->Release();
  539.       }
  540.       break;
  541.     case IDM_UCAR_OFFROAD:
  542.       LOG("C: === UtilityCar Menu: IUtility::Offroad");
  543.       if (GetInterface(m_pUtilityCar, IID_IUtility, (PPVOID)&pIUtility))
  544.       {
  545.         LOG("C: --Calling pIUtility->Offroad");
  546.         pIUtility->Offroad(1);
  547.         LOG("C: --Releasing pIUtility");
  548.         pIUtility->Release();
  549.       }
  550.       break;
  551.     case IDM_UCAR_WINCH:
  552.       LOG("C: === UtilityCar Menu: IUtility::Winch");
  553.       if (GetInterface(m_pUtilityCar, IID_IUtility, (PPVOID)&pIUtility))
  554.       {
  555.         LOG("C: --Calling pIUtility->Winch");
  556.         pIUtility->Winch(0);
  557.         LOG("C: --Releasing pIUtility");
  558.         pIUtility->Release();
  559.       }
  560.       break;
  561.  
  562.     //----------------------------------------------------------------------
  563.     // Handle CruiseCar Menu Commands.
  564.     //----------------------------------------------------------------------
  565.     case IDM_CCAR_CREATE:
  566.       LOG("C: === CruiseCar Menu: Create.");
  567.       if (NULL == m_pCruiseCar)
  568.       {
  569.         // Call COM service to create an instance.
  570.         hr = CoCreateInstance(
  571.                CLSID_DllCruiseCar,
  572.                NULL,
  573.                CLSCTX_INPROC_SERVER,
  574.                IID_IUnknown,
  575.                (PPVOID)&m_pCruiseCar);
  576.         if (SUCCEEDED(hr))
  577.         {
  578.           ::CheckMenuItem(
  579.               hMenu,
  580.               IDM_CCAR_CREATE,
  581.               MF_BYCOMMAND | MF_CHECKED);
  582.         }
  583.         else
  584.         {
  585.           LOGERROR("C: ???? CoCreateInstance",hr);
  586.         }
  587.       }
  588.       else
  589.         LOG("C: ???? CruiseCar already exists.");
  590.       break;
  591.     case IDM_CCAR_RELEASE:
  592.       LOG("C: === CruiseCar Menu: Release.");
  593.       if (NULL != m_pCruiseCar)
  594.       {
  595.         RELEASE_INTERFACE(m_pCruiseCar);
  596.         // We ask COM to unload any unused COM Servers.
  597.         CoFreeUnusedLibraries();
  598.         ::CheckMenuItem(
  599.             hMenu,
  600.             IDM_CCAR_CREATE,
  601.             MF_BYCOMMAND | MF_UNCHECKED);
  602.       }
  603.       else
  604.         LOG("C: ???? No CruiseCar to Release.");
  605.       break;
  606.     case IDM_CCAR_SHIFT:
  607.       LOG("C: === CruiseCar Menu: ICar::Shift");
  608.       if (GetInterface(m_pCruiseCar, IID_ICar, (PPVOID)&pICar))
  609.       {
  610.         LOG("C: --Calling pICar->Shift");
  611.         pICar->Shift(4);
  612.         LOG("C: --Releasing pICar");
  613.         pICar->Release();
  614.       }
  615.       break;
  616.     case IDM_CCAR_CLUTCH:
  617.       LOG("C: === CruiseCar Menu: ICar::Clutch");
  618.       if (GetInterface(m_pCruiseCar, IID_ICar, (PPVOID)&pICar))
  619.       {
  620.         LOG("C: --Calling pICar->Clutch");
  621.         pICar->Clutch(100);
  622.         LOG("C: --Releasing pICar");
  623.         pICar->Release();
  624.       }
  625.       break;
  626.     case IDM_CCAR_SPEED:
  627.       LOG("C: === CruiseCar Menu: ICar::Speed");
  628.       if (GetInterface(m_pCruiseCar, IID_ICar, (PPVOID)&pICar))
  629.       {
  630.         LOG("C: --Calling pICar->Speed");
  631.         pICar->Speed(60);
  632.         LOG("C: --Releasing pICar");
  633.         pICar->Release();
  634.       }
  635.       break;
  636.     case IDM_CCAR_STEER:
  637.       LOG("C: === CruiseCar Menu: ICar::Steer");
  638.       if (GetInterface(m_pCruiseCar, IID_ICar, (PPVOID)&pICar))
  639.       {
  640.         LOG("C: --Calling pICar->Steer");
  641.         pICar->Steer(0);
  642.         LOG("C: --Releasing pICar");
  643.         pICar->Release();
  644.       }
  645.       break;
  646.     case IDM_CCAR_ENGAGE:
  647.       LOG("C: === CruiseCar Menu: ICruise::Engage");
  648.       if (GetInterface(m_pCruiseCar, IID_ICruise, (PPVOID)&pICruise))
  649.       {
  650.         LOG("C: --Calling pICruise->Engage");
  651.         pICruise->Engage(TRUE);
  652.         LOG("C: --Releasing pICruise");
  653.         pICruise->Release();
  654.       }
  655.       break;
  656.     case IDM_CCAR_ADJUST:
  657.       LOG("C: === CruiseCar Menu: ICruise::Adjust");
  658.       if (GetInterface(m_pCruiseCar, IID_ICruise, (PPVOID)&pICruise))
  659.       {
  660.         LOG("C: --Calling pICruise->Adjust");
  661.         pICruise->Adjust(FALSE);
  662.         LOG("C: --Releasing pICruise");
  663.         pICruise->Release();
  664.       }
  665.       break;
  666.  
  667.     //----------------------------------------------------------------------
  668.     // Handle UtilityCruiseCar Menu Commands.
  669.     //----------------------------------------------------------------------
  670.     case IDM_UCRU_CREATE:
  671.       LOG("C: === UtilityCruiseCar Menu: Create.");
  672.       if (NULL == m_pUtilityCruiseCar)
  673.       {
  674.         // Call a create function to create an instance.
  675.         hr = CreateUtilityCruiseCar(
  676.                NULL,
  677.                IID_IUnknown,
  678.                (PPVOID)&m_pUtilityCruiseCar);
  679.         if (SUCCEEDED(hr))
  680.         {
  681.           ::CheckMenuItem(
  682.               hMenu,
  683.               IDM_UCRU_CREATE,
  684.               MF_BYCOMMAND | MF_CHECKED);
  685.         }
  686.         else
  687.         {
  688.           LOGERROR("C: ???? UtilityCruiseCar creation",hr);
  689.         }
  690.       }
  691.       else
  692.         LOG("C: ???? UtilityCruiseCar already exists.");
  693.       break;
  694.     case IDM_UCRU_RELEASE:
  695.       LOG("C: === UtilityCruiseCar Menu: Release.");
  696.       if (NULL != m_pUtilityCruiseCar)
  697.       {
  698.         RELEASE_INTERFACE(m_pUtilityCruiseCar);
  699.         // We ask COM to unload any unused COM Servers.
  700.         CoFreeUnusedLibraries();
  701.         ::CheckMenuItem(
  702.             hMenu,
  703.             IDM_UCRU_CREATE,
  704.             MF_BYCOMMAND | MF_UNCHECKED);
  705.       }
  706.       else
  707.         LOG("C: ???? No UtilityCruiseCar to Release.");
  708.       break;
  709.     case IDM_UCRU_SHIFT:
  710.       LOG("C: === UtilityCruiseCar Menu: ICar::Shift");
  711.       if (GetInterface(m_pUtilityCruiseCar, IID_ICar, (PPVOID)&pICar))
  712.       {
  713.         LOG("C: --Calling pICar->Shift");
  714.         pICar->Shift(1);
  715.         LOG("C: --Releasing pICar");
  716.         pICar->Release();
  717.       }
  718.       break;
  719.     case IDM_UCRU_CLUTCH:
  720.       LOG("C: === UtilityCruiseCar Menu: ICar::Clutch");
  721.       if (GetInterface(m_pUtilityCruiseCar, IID_ICar, (PPVOID)&pICar))
  722.       {
  723.         LOG("C: --Calling pICar->Clutch");
  724.         pICar->Clutch(80);
  725.         LOG("C: --Releasing pICar");
  726.         pICar->Release();
  727.       }
  728.       break;
  729.     case IDM_UCRU_SPEED:
  730.       LOG("C: === UtilityCruiseCar Menu: ICar::Speed");
  731.       if (GetInterface(m_pUtilityCruiseCar, IID_ICar, (PPVOID)&pICar))
  732.       {
  733.         LOG("C: --Calling pICar->Speed");
  734.         pICar->Speed(10);
  735.         LOG("C: --Releasing pICar");
  736.         pICar->Release();
  737.       }
  738.       break;
  739.     case IDM_UCRU_STEER:
  740.       LOG("C: === UtilityCruiseCar Menu: ICar::Steer");
  741.       if (GetInterface(m_pUtilityCruiseCar, IID_ICar, (PPVOID)&pICar))
  742.       {
  743.         LOG("C: --Calling pICar->Steer");
  744.         pICar->Steer(10);
  745.         LOG("C: --Releasing pICar");
  746.         pICar->Release();
  747.       }
  748.       break;
  749.     case IDM_UCRU_ENGAGE:
  750.       LOG("C: === UtilityCruiseCar Menu: ICruise::Engage");
  751.       if (GetInterface(m_pUtilityCruiseCar, IID_ICruise, (PPVOID)&pICruise))
  752.       {
  753.         LOG("C: --Calling pICruise->Engage");
  754.         pICruise->Engage(FALSE);
  755.         LOG("C: --Releasing pICruise");
  756.         pICruise->Release();
  757.       }
  758.       break;
  759.     case IDM_UCRU_ADJUST:
  760.       LOG("C: === UtilityCruiseCar Menu: ICruise::Adjust");
  761.       if (GetInterface(m_pUtilityCruiseCar, IID_ICruise, (PPVOID)&pICruise))
  762.       {
  763.         LOG("C: --Calling pICruise->Adjust");
  764.         pICruise->Adjust(FALSE);
  765.         LOG("C: --Releasing pICruise");
  766.         pICruise->Release();
  767.       }
  768.       break;
  769.     case IDM_UCRU_OFFROAD:
  770.       LOG("C: === UtilityCruiseCar Menu: IUtility::Offroad");
  771.       if (GetInterface(m_pUtilityCruiseCar, IID_IUtility, (PPVOID)&pIUtility))
  772.       {
  773.         LOG("C: --Calling pIUtility->Offroad");
  774.         pIUtility->Offroad(3);
  775.         LOG("C: --Releasing pIUtility");
  776.         pIUtility->Release();
  777.       }
  778.       break;
  779.     case IDM_UCRU_WINCH:
  780.       LOG("C: === UtilityCruiseCar Menu: IUtility::Winch");
  781.       if (GetInterface(m_pUtilityCruiseCar, IID_IUtility, (PPVOID)&pIUtility))
  782.       {
  783.         LOG("C: --Calling pIUtility->Winch");
  784.         pIUtility->Winch(0);
  785.         LOG("C: --Releasing pIUtility");
  786.         pIUtility->Release();
  787.       }
  788.       break;
  789.  
  790.     //----------------------------------------------------------------------
  791.     // Handle Log Menu Commands.
  792.     //----------------------------------------------------------------------
  793.     case IDM_LOG_LOGCLEAR:
  794.       // Clear the message log.
  795.       m_pMsgLog->Clear();
  796.       // Use macro to log messages.
  797.       LOGID(IDS_START_MESSAGE_LOG);
  798.       break;
  799.     case IDM_LOG_LOGGING:
  800.       // Toggle the state of the Message Logging.
  801.       // Toggle the checkmark indicator on the menu selection as well.
  802.       {
  803.         HMENU hMenu  = ::GetMenu(m_hWnd);
  804.         BOOL bLogging = ::GetMenuState(
  805.                             hMenu,
  806.                             IDM_LOG_LOGGING,
  807.                             MF_BYCOMMAND) & MF_CHECKED;
  808.         if (bLogging)
  809.         {
  810.           m_pMsgLog->Logging(FALSE);
  811.           ::CheckMenuItem(
  812.               hMenu,
  813.               IDM_LOG_LOGGING,
  814.               MF_BYCOMMAND | MF_UNCHECKED);
  815.         }
  816.         else
  817.         {
  818.           m_pMsgLog->Logging(TRUE);
  819.           ::CheckMenuItem(
  820.               hMenu,
  821.               IDM_LOG_LOGGING,
  822.               MF_BYCOMMAND | MF_CHECKED);
  823.         }
  824.       }
  825.       break;
  826.     case IDM_LOG_COPYCLIP:
  827.       // Copy trace message log to clipboard.
  828.       m_pMsgLog->Copy();
  829.       break;
  830.  
  831.     //----------------------------------------------------------------------
  832.     // Handle Help Menu Commands.
  833.     //----------------------------------------------------------------------
  834.     case IDM_HELP_CONTENTS:
  835.       // We have some stubbed support here for bringing up the online
  836.       //   Help for this application.
  837.       if (::FileExist(m_szHelpFile))
  838.         ::WinHelp(m_hWnd, m_szHelpFile, HELP_CONTEXT, IDH_CONTENTS);
  839.       else
  840.         m_pMsgBox->ErrorID(IDS_NOHELPFILE);
  841.       break;
  842.     case IDM_HELP_TUTORIAL:
  843.       // Call the APPUTIL utility function, ReadTutorial, to Browse the HTML
  844.       // tutorial narrative file associated with this tutorial code sample.
  845.       ReadTutorial(m_hInst, m_hWnd, TEXT(HTML_FILE_EXT));
  846.       break;
  847.     case IDM_HELP_TUTSERVER:
  848.       // Call the APPUTIL utility function, ReadTutorial, to Browse the HTML
  849.       // tutorial narrative file associated with the COM server.
  850.       ReadTutorial(m_hInst, m_hWnd, TEXT(SERVER_TUTFILE_STR));
  851.       break;
  852.     case IDM_HELP_TUTREGISTER:
  853.       // Call the APPUTIL utility function, ReadTutorial, to Browse the HTML
  854.       // tutorial narrative file associated with the Registration utility.
  855.       ReadTutorial(m_hInst, m_hWnd, TEXT(REGISTER_TUTFILE_STR));
  856.       break;
  857.     case IDM_HELP_READSOURCE:
  858.       // Call the APPUTIL utility function ReadSource to allow the
  859.       // user to open and read any of the source files of DLLCLIEN.
  860.       ReadSource(m_hWnd, &m_ofnFile);
  861.       break;
  862.     case IDM_HELP_ABOUT:
  863.       {
  864.         CAboutBox dlgAboutBox;
  865.  
  866.         LOG("C: === Help Menu: About DLLCLIEN.");
  867.         // Show the standard About Box dialog for this EXE by telling the
  868.         // dialog C++ object to show itself by invoking its ShowDialog
  869.         // method.  Pass it this EXE instance and the parent window handle.
  870.         // Use a dialog resource ID for the dialog template stored in
  871.         // this EXE module's resources.
  872.         dlgAboutBox.ShowDialog(
  873.           m_hInst,
  874.           MAKEINTRESOURCE(IDM_HELP_ABOUT),
  875.           m_hWnd);
  876.       }
  877.       break;
  878.     case IDM_HELP_ABOUTSERVER:
  879.       // Call the CarSample to show its AboutBox dialog.
  880.       LOG("C: === Help Menu: About DLLSERVE.");
  881.       m_pCarSample->AboutBox(m_hWnd);
  882.       break;
  883.  
  884.     default:
  885.       // Defer all messages NOT handled here to the Default Window Proc.
  886.       lResult = ::DefWindowProc(m_hWnd, WM_COMMAND, wParam, lParam);
  887.       break;
  888.   }
  889.  
  890.   return(lResult);
  891. }
  892.  
  893.  
  894. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  895.   Method:   CMainWindow::WindowProc
  896.  
  897.   Summary:  Main window procedure for this window object.  See CVirWindow
  898.             in the APPUTIL library (APPUTIL.CPP) for details on how this
  899.             method gets called by the global WindowProc.
  900.  
  901.   Args:     UINT uMsg,
  902.               Windows message that is "sent" to this window.
  903.             WPARAM wParam,
  904.               First message parameter (word sized).
  905.             LPARAM lParam)
  906.               Second message parameter (long sized).
  907.  
  908.   Modifies: ...
  909.  
  910.   Returns:  LRESULT
  911.               Standard Windows WindowProc return value.
  912. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  913. LRESULT CMainWindow::WindowProc(
  914.           UINT uMsg,
  915.           WPARAM wParam,
  916.           LPARAM lParam)
  917. {
  918.   LRESULT lResult = FALSE;
  919.  
  920.   switch (uMsg)
  921.   {
  922.     case WM_CREATE:
  923.       {
  924.         // Setup for painting text in this window.
  925.         HDC hdc = GetDC(m_hWnd);
  926.         ::GetTextMetrics(hdc, &m_tm);
  927.         ::ReleaseDC(m_hWnd, hdc);
  928.       }
  929.       break;
  930.  
  931.     case WM_MEASUREITEM:
  932.       // Get setup for painting text in this window.
  933.       {
  934.         LPMEASUREITEMSTRUCT lpmis = (LPMEASUREITEMSTRUCT) lParam;
  935.         lpmis->itemHeight = m_tm.tmHeight + m_tm.tmExternalLeading;
  936.         lpmis->itemWidth = m_wWidth;
  937.         lResult = TRUE;
  938.       }
  939.  
  940.     case WM_SIZE:
  941.       // Handle a resize of this window.
  942.       m_wWidth = LOWORD(lParam);
  943.       m_wHeight = HIWORD(lParam);
  944.       // Resize the Message Log ListBox
  945.       m_pMsgLog->Resize(m_wWidth, m_wHeight);
  946.       break;
  947.  
  948.     case WM_COMMAND:
  949.       // Dispatch and handle any Menu command messages received.
  950.       lResult = DoMenu(wParam, lParam);
  951.       break;
  952.  
  953.     case WM_CLOSE:
  954.       // The user selected Close on the main window's System menu
  955.       // or Exit on the File menu.
  956.     case WM_QUIT:
  957.       // If the app is being quit then close any associated help windows.
  958.       // ::WinHelp(m_hWnd, m_szHelpFile, HELP_QUIT, 0);
  959.     default:
  960.       // Defer all messages NOT handled here to the Default Window Proc.
  961.       lResult = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  962.       break;
  963.   }
  964.  
  965.   return(lResult);
  966. }
  967.  
  968.  
  969. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  970.   Function: UnicodeOk
  971.  
  972.   Summary:  Checks if the platform will handle unicode versions of
  973.             Win32 string API calls.
  974.  
  975.   Args:     void
  976.  
  977.   Returns:  BOOL
  978.               TRUE if unicode support; FALSE if not.
  979. ------------------------------------------------------------------------F-F*/
  980. BOOL UnicodeOk(void)
  981. {
  982.   BOOL bOk = TRUE;
  983.   TCHAR szUserName[MAX_STRING_LENGTH];
  984.   DWORD dwSize = MAX_STRING_LENGTH;
  985.  
  986.   if (!GetUserName(szUserName, &dwSize))
  987.     bOk = ERROR_CALL_NOT_IMPLEMENTED == GetLastError() ? FALSE : TRUE;
  988.  
  989.   return bOk;
  990. }
  991.  
  992.  
  993. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  994.   Function: InitApplication
  995.  
  996.   Summary:  Initializes the application and registers its main window
  997.             class. InitApplication is called only once (in WinMain).
  998.  
  999.   Args:     HINSTANCE hInstance)
  1000.               Handle to the first instance of the application.
  1001.  
  1002.   Returns:  BOOL.
  1003.               TRUE if success.
  1004.               FALSE if fail.
  1005. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  1006. BOOL InitApplication(
  1007.        HINSTANCE hInstance)
  1008. {
  1009.   BOOL bOK;
  1010.   // The window class for all instances of the main frame window.
  1011.   WNDCLASSEX wcf;
  1012.  
  1013.   // Assign the appropriate values for this main frame window class.
  1014.   wcf.cbSize        = sizeof(WNDCLASSEX);
  1015.   wcf.style         = CS_HREDRAW | CS_VREDRAW; // Class style(s).
  1016.   wcf.lpfnWndProc   = &WindowProc;             // Global Window Procedure for
  1017.                                                //   all windows of this class.
  1018.   wcf.cbClsExtra    = 0;                       // No per-class extra data.
  1019.   wcf.cbWndExtra    = 0;                       // No per-window extra data.
  1020.   wcf.hInstance     = hInstance;               // Owner of this class.
  1021.   wcf.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);       // Default color.
  1022.   wcf.lpszMenuName  = TEXT(MAIN_WINDOW_CLASS_MENU_STR); // Menu name from .RC.
  1023.   wcf.lpszClassName = TEXT(MAIN_WINDOW_CLASS_NAME_STR); // Class name from .RC.
  1024.   wcf.hCursor       = LoadCursor(NULL, IDC_ARROW);      // Cursor.
  1025.   wcf.hIcon         = LoadIcon(                         // Icon name from .RC.
  1026.                         hInstance,
  1027.                         TEXT("AppIcon"));
  1028.   wcf.hIconSm       = LoadImage(                        // Load small icon.
  1029.                         hInstance,
  1030.                         TEXT("AppIcon"),
  1031.                         IMAGE_ICON,
  1032.                         16, 16,
  1033.                         0);
  1034.  
  1035.   // Register the window class and return FALSE if unsuccesful.
  1036.   bOK = RegisterClassEx(&wcf);
  1037.   if (!bOK)
  1038.   {
  1039.     // Assume we are running on NT where RegisterClassEx() is
  1040.     // not implemented, so let's try calling RegisterClass().
  1041.     bOK = RegisterClass((LPWNDCLASS)&wcf.style);
  1042.   }
  1043.  
  1044.   return (bOK);
  1045. }
  1046.  
  1047.  
  1048. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  1049.   Function: WinMain
  1050.  
  1051.   Summary:  The Windows main entry point function for this application.
  1052.             Initializes the application, the COM Libraries, and starts
  1053.             the main application message loop.
  1054.  
  1055.   Args:     HINSTANCE hInstance,
  1056.               Instance handle; a new one for each invocation of this app.
  1057.             HINSTANCE hPrevInstance,
  1058.               Instance handle of the previous instance. NULL in Win32.
  1059.             LPSTR lpCmdLine,
  1060.               Windows passes a pointer to the application's
  1061.               invocation command line.
  1062.             int nCmdShow)
  1063.               Bits telling the show state of the application.
  1064.  
  1065.   Returns:  int
  1066.               msg.wParam (upon exit of message loop).
  1067.               FALSE if this instance couldn't initialize and run.
  1068. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  1069. extern "C" int PASCAL WinMain(
  1070.                         HINSTANCE hInstance,
  1071.                         HINSTANCE hPrevInstance,
  1072.                         LPSTR lpCmdLine,
  1073.                         int nCmdShow)
  1074. {
  1075.   CMainWindow* pWin = NULL;
  1076.   MSG msg;
  1077.   HACCEL hAccel;
  1078.   int iRun = FALSE;
  1079.  
  1080.   // If we were compiled for UNICODE and the platform seems OK with this
  1081.   // then proceed.  Else we error and exit the app.
  1082.   if (UnicodeOk())
  1083.   {
  1084.     // Call to initialize the COM Library.  Use the SUCCEEDED macro
  1085.     // to detect success.  If fail then exit app with error message.
  1086.     if (SUCCEEDED(CoInitialize(NULL)))
  1087.     {
  1088.       // If we succeeded in initializing the COM Library we proceed to
  1089.       // initialize the application.  If we can't init the application
  1090.       // then we signal shut down with an error message exit.
  1091.       iRun = InitApplication(hInstance);
  1092.       if (iRun)
  1093.       {
  1094.         // Assume we'll set iRun to TRUE when initialization is done.
  1095.         iRun = FALSE;
  1096.         // We are still go for running so we try to create a nifty new
  1097.         // CMainWindow object for this app instance.
  1098.         pWin = new CMainWindow;
  1099.         if (NULL != pWin)
  1100.         {
  1101.           // Now we initialize an instance of the new CMainWindow.
  1102.           // This includes creating the main window.  Note: if
  1103.           // InitInstance fails then it would have already deleted
  1104.           // pWin so we wouldn't need to delete it here.
  1105.           if (pWin->InitInstance(hInstance, nCmdShow))
  1106.           {
  1107.             // Load the keyboard accelerators from the resources.
  1108.             hAccel = LoadAccelerators(hInstance, TEXT("AppAccel"));
  1109.             if (NULL != hAccel)
  1110.             {
  1111.               // Signal App Initialization is successfully done.
  1112.               iRun = TRUE;
  1113.             }
  1114.           }
  1115.         }
  1116.       }
  1117.  
  1118.       if (iRun)
  1119.       {
  1120.         // If we initialized the app instance properly then we are still
  1121.         // go for running.  We then start up the main message pump for
  1122.         // the application.
  1123.         while (GetMessage(&msg, NULL, 0, 0))
  1124.         {
  1125.           if (!TranslateAccelerator(
  1126.                  pWin->GetHwnd(),
  1127.                  hAccel,
  1128.                  &msg))
  1129.           {
  1130.             TranslateMessage(&msg);
  1131.             DispatchMessage(&msg);
  1132.           }
  1133.         }
  1134.  
  1135.         // We ask COM to unload any unused COM Servers.
  1136.         CoFreeUnusedLibraries();
  1137.  
  1138.         // We'll pass to Windows the reason why we exited the message loop.
  1139.         iRun = msg.wParam;
  1140.       }
  1141.       else
  1142.       {
  1143.         // We failed to initialize the application--issue an error
  1144.         // messagebox.
  1145.         TCHAR szMsg[MAX_STRING_LENGTH];
  1146.  
  1147.         // Load the error message string from the resources.
  1148.         if (LoadString(
  1149.               hInstance,
  1150.               IDS_APPINITFAILED,
  1151.               szMsg,
  1152.               MAX_STRING_LENGTH))
  1153.         {
  1154.           // Put up error message box saying that application couldn't be
  1155.           // initialized.  Parent window is desktop (ie, NULL).
  1156.           MessageBox(
  1157.             NULL,
  1158.             szMsg,
  1159.             TEXT(ERROR_TITLE_STR),
  1160.             MB_OK | MB_ICONEXCLAMATION);
  1161.         }
  1162.         DELETE_POINTER(pWin->m_pMsgBox);
  1163.         DELETE_POINTER(pWin->m_pMsgLog);
  1164.         DELETE_POINTER(pWin);
  1165.       }
  1166.  
  1167.       // We're exiting this app (either normally or by init failure) so
  1168.       // shut down the COM Library.
  1169.       CoUninitialize();
  1170.     }
  1171.     else
  1172.     {
  1173.       // We failed to Initialize the COM Library.
  1174.       TCHAR szMsg[MAX_STRING_LENGTH];
  1175.  
  1176.       // Load the error message string from the resources.
  1177.       if (LoadString(
  1178.             hInstance,
  1179.             IDS_COMINITFAILED,
  1180.             szMsg,
  1181.             MAX_STRING_LENGTH))
  1182.       {
  1183.         // Put up error message box saying that COM Library
  1184.         // couldn't be initialized.  Parent window is desktop (ie, NULL).
  1185.         // And exit the failed application.
  1186.         MessageBox(
  1187.           NULL,
  1188.           szMsg,
  1189.           TEXT(ERROR_TITLE_STR),
  1190.           MB_OK | MB_ICONEXCLAMATION);
  1191.       }
  1192.     }
  1193.   }
  1194.   else
  1195.   {
  1196.     // If we were compiled for UNICODE but the platform has problems with
  1197.     // this then indicate an error and exit the app immediately.
  1198.     CHAR szMsg[MAX_STRING_LENGTH];
  1199.  
  1200.     if (LoadStringA(
  1201.           hInstance,
  1202.           IDS_NOUNICODE,
  1203.           szMsg,
  1204.           MAX_STRING_LENGTH))
  1205.     {
  1206.       MessageBoxA(
  1207.         NULL,
  1208.         szMsg,
  1209.         ERROR_TITLE_STR,
  1210.         MB_OK | MB_ICONEXCLAMATION);
  1211.     }
  1212.   }
  1213.  
  1214.   return iRun;
  1215. }
  1216.