home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / tutsamp / licclien / licclien.cpp next >
C/C++ Source or Header  |  1997-08-30  |  47KB  |  1,390 lines

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