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 / freclien / freclien.cpp next >
Encoding:
C/C++ Source or Header  |  1997-08-30  |  22.7 KB  |  635 lines

  1. /*+==========================================================================
  2.   File:      FRECLIEN.CPP
  3.  
  4.   Summary:   Based largely on the DLLCLIEN.EXE application code, this
  5.              module is meant to use use muitiple threads in the client to
  6.              load and access a free threaded COM component in a separate
  7.              in-process COM Server (FRESERVE built in the sibling FRESERVE
  8.              directory).  Thus to run FRECLIEN you must build FRESERVE
  9.              first. This client application is meant to exercise the
  10.              FRESERVE in-process server using multiple client threads.
  11.              Three such worker threads are created to function the
  12.              principal COM object in the FRESERVE server. This object is
  13.              a COBall object which maintains logic and data to simulate a
  14.              ball bouncing inside of an enclosed 2-dimensional area. The
  15.              worker threads all continuously attempt to move this virtual
  16.              ball.
  17.  
  18.              In this client application the main process thread also
  19.              asynchronously queries the ball for its display data and
  20.              renders this data into a moving GUI image on the screen.
  21.              There is no GUI behavior in the server--it is all in this
  22.              client.  This client instantiates one instance of the
  23.              server's COBall COM object and exercises it with multiple
  24.              threads. There is a minimal menu in FRECLIEN. All the action
  25.              is automatic. The main application window's client area is
  26.              used for visual display of the moving ball.
  27.  
  28.              For a comprehensive tutorial code tour of FRECLIEN's contents
  29.              and offerings see the tutorial FRECLIEN.HTM file. For
  30.              more specific technical details on the internal workings see
  31.              the comments dispersed throughout the FRECLIEN source code.
  32.              For more details on the FRESERVE.DLL that FRECLIEN works with
  33.              see the FRESERVE.HTM file in the main tutorial directory.
  34.  
  35.   Classes:   CMainWindow
  36.  
  37.   Functions: InitApplication, WinMain
  38.  
  39.   Origin:    4-6-96: atrent - Editor-inheritance from the DLLCLIEN source.
  40.              Also borrows from the GDIDEMO sample in the Win32 samples of
  41.              the Win32 SDK.
  42.  
  43. ----------------------------------------------------------------------------
  44.   This file is part of the Microsoft COM Tutorial Code Samples.
  45.  
  46.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  47.  
  48.   This source code is intended only as a supplement to Microsoft
  49.   Development Tools and/or on-line documentation.  See these other
  50.   materials for detailed information regarding Microsoft code samples.
  51.  
  52.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  53.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  54.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  55.   PARTICULAR PURPOSE.
  56. ==========================================================================+*/
  57.  
  58. /*--------------------------------------------------------------------------
  59.   We include WINDOWS.H for all Win32 applications.
  60.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  61.   We include INITGUID.H only once (here) in the entire app because we
  62.     will be defining GUIDs and want them as constants in the data segment.
  63.   We include COMMDLG.H because we will be using the Open File and
  64.     potentially other Common dialogs.
  65.   We include APPUTIL.H because we will be building this application using
  66.     the convenient Virtual Window and Dialog classes and other
  67.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  68.   We include IBALL.H and BALLGUID.H for the common Ball-related Interface
  69.     class, GUID, and CLSID specifications.
  70.   We include FRECLIEN.H because it has class and resource definitions
  71.     specific to this FRECLIEN application.
  72.   We include GUIBALL.H because it has the C++ class used for GUI display
  73.     of the moving ball.
  74. ---------------------------------------------------------------------------*/
  75. #include <windows.h>
  76. #include <ole2.h>
  77. #include <initguid.h>
  78. #include <commdlg.h>
  79. #include <apputil.h>
  80. #include <iball.h>
  81. #include <ballguid.h>
  82. #include "guiball.h"
  83. #include "freclien.h"
  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_pMsgBox  = NULL;
  121.   m_pGuiBall = NULL;
  122. }
  123.  
  124.  
  125. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  126.   Method:   CMainWindow::~CMainWindow
  127.  
  128.   Summary:  CMainWindow Destructor.  Destruction of the main window
  129.             indicates that the application should quit and thus the
  130.             PostQuitMessage API is called.
  131.  
  132.   Args:     .
  133.  
  134.   Modifies: .
  135.  
  136.   Returns:  .
  137. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  138. CMainWindow::~CMainWindow()
  139. {
  140.   // CMainWindow is derived from CVirWindow which traps the WM_DESTROY
  141.   // message and causes a delete of CMainWindow which in turn causes this
  142.   // destructor to run. The WM_DESTROY results when the window is destoyed
  143.   // after a close of the window. Prior to exiting the main message loop:
  144.  
  145.   // We delete the CGuiBall and CMsgBox objects that were made in
  146.   // Initinstance.
  147.   DELETE_POINTER(m_pGuiBall);
  148.   DELETE_POINTER(m_pMsgBox);
  149.  
  150.   // We then post a WM_QUIT message to cause an exit of the main thread's
  151.   // message loop and an exit of this instance of the application.
  152.   PostQuitMessage(0);
  153. }
  154.  
  155.  
  156. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  157.   Method:   CMainWindow::InitInstance
  158.  
  159.   Summary:  Instantiates an instance of the main application window.
  160.             This method must be called only once, immediately after
  161.             window class construction.  We take care to delete 'this'
  162.             CMainWindow if we must return the error condition FALSE.
  163.  
  164.   Args:     HINSTANCE hInstance,
  165.               Handle of the application instance.
  166.             int nCmdShow)
  167.               Command to pass to ShowWindow.
  168.  
  169.   Modifies: m_szHelpFile, m_pMsgBox.
  170.  
  171.   Returns:  BOOL.
  172.               TRUE if succeeded.
  173.               FALSE if failed.
  174. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  175. BOOL CMainWindow::InitInstance(
  176.        HINSTANCE hInstance,
  177.        int nCmdShow)
  178. {
  179.   BOOL bOk = FALSE;
  180.   HWND hWnd = NULL;
  181.  
  182.   // Create the Message Box and Message Log objects.
  183.   m_pMsgBox = new CMsgBox;
  184.  
  185.   // Create the CGuiBall object.
  186.   m_pGuiBall = new CGuiBall;
  187.  
  188.   if (NULL != m_pMsgBox && NULL != m_pGuiBall)
  189.   {
  190.     // Note, the Create method sets the m_hWnd member so we don't
  191.     // need to set it explicitly here first. Here is the create of this
  192.     // window.  Size the window reasonably. Create sets both m_hInst and
  193.     // m_hWnd. This creates the main client window.
  194.     hWnd = Create(
  195.              TEXT(MAIN_WINDOW_CLASS_NAME_STR),
  196.              TEXT(MAIN_WINDOW_TITLE_STR),
  197.              WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
  198.                | WS_MAXIMIZEBOX | WS_THICKFRAME,
  199.              CW_USEDEFAULT,
  200.              CW_USEDEFAULT,
  201.              ::GetSystemMetrics(SM_CXSCREEN)*2/5,
  202.              ::GetSystemMetrics(SM_CYSCREEN)*2/5,
  203.              NULL,
  204.              NULL,
  205.              hInstance);
  206.     if (NULL != hWnd)
  207.     {
  208.       // Init the new GuiBall.
  209.       bOk = m_pGuiBall->Init(m_hWnd);
  210.       if (bOk)
  211.       {
  212.         // Ensure the new window is shown on screen and content
  213.         // is painted.
  214.         ::ShowWindow(m_hWnd, nCmdShow);
  215.         ::UpdateWindow(m_hWnd);
  216.  
  217.         // Build a path to where the help file should be (it should be in
  218.         // the same directory as the .EXE but with the .HTM extension.
  219.         MakeFamilyPath(hInstance, m_szHelpFile, TEXT(HELP_FILE_EXT));
  220.  
  221.         // Init the Message Box object.
  222.         bOk = m_pMsgBox->Init(m_hInst, m_hWnd);
  223.       }
  224.     }
  225.   }
  226.  
  227.   if (!bOk)
  228.   {
  229.     DELETE_POINTER(m_pMsgBox);
  230.     DELETE_POINTER(m_pGuiBall);
  231.   }
  232.  
  233.   return (bOk);
  234. }
  235.  
  236.  
  237. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  238.   Method:   CMainWindow::DoMenu
  239.  
  240.   Summary:  Dispatch and handle the main menu commands.
  241.  
  242.   Args:     WPARAM wParam,
  243.               First message parameter (word sized).
  244.             LPARAM lParam)
  245.               Second message parameter (long sized).
  246.  
  247.   Modifies: m_ofnFile, ...
  248.  
  249.   Returns:  LRESULT
  250.               Standard Windows WindowProc return value.
  251. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  252. LRESULT CMainWindow::DoMenu(
  253.           WPARAM wParam,
  254.           LPARAM lParam)
  255. {
  256.   LRESULT lResult = FALSE;
  257.   HMENU hMenu  = ::GetMenu(m_hWnd);
  258.  
  259.   switch (LOWORD(wParam))
  260.   {
  261.     //----------------------------------------------------------------------
  262.     // Handle File Menu Commands.
  263.     //----------------------------------------------------------------------
  264.     case IDM_FILE_EXIT:
  265.       // The user commands us to exit this application so we tell the
  266.       // Main window to close itself.
  267.       ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  268.       break;
  269.  
  270.     //----------------------------------------------------------------------
  271.     // Handle Help Menu Commands.
  272.     //----------------------------------------------------------------------
  273.     case IDM_HELP_CONTENTS:
  274.       // We have some stubbed support here for bringing up the online
  275.       // Help for this application.
  276.       ReadHelp(m_hWnd, m_szHelpFile);
  277.       break;
  278.     case IDM_HELP_TUTORIAL:
  279.       // Call the APPUTIL utility function, ReadTutorial, to Browse the HTML
  280.       // tutorial narrative file associated with this tutorial code sample.
  281.       ReadTutorial(m_hInst, m_hWnd, TEXT(HTML_FILE_EXT));
  282.       break;
  283.     case IDM_HELP_TUTSERVER:
  284.       // Call the APPUTIL utility function, ReadTutorial, to Browse the HTML
  285.       // tutorial narrative file associated with the COM server.
  286.       ReadTutorial(m_hInst, m_hWnd, TEXT(SERVER_TUTFILE_STR));
  287.       break;
  288.     case IDM_HELP_READSOURCE:
  289.       // Call the APPUTIL utility function ReadSource to allow the
  290.       // user to open and read any of the source files of FRECLIEN.
  291.       ReadSource(m_hWnd, &m_ofnFile);
  292.       break;
  293.     case IDM_HELP_ABOUT:
  294.       {
  295.         CAboutBox dlgAboutBox;
  296.  
  297.         // Show the standard About Box dialog for this EXE by telling the
  298.         // dialog C++ object to show itself by invoking its ShowDialog
  299.         // method.  Pass it this EXE instance and the parent window handle.
  300.         // Use a dialog resource ID for the dialog template stored in
  301.         // this EXE module's resources.
  302.         dlgAboutBox.ShowDialog(
  303.           m_hInst,
  304.           MAKEINTRESOURCE(IDM_HELP_ABOUT),
  305.           m_hWnd);
  306.       }
  307.       break;
  308.  
  309.     default:
  310.       // Defer all messages NOT handled here to the Default Window Proc.
  311.       lResult = ::DefWindowProc(m_hWnd, WM_COMMAND, wParam, lParam);
  312.       break;
  313.   }
  314.  
  315.   return(lResult);
  316. }
  317.  
  318.  
  319. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  320.   Method:   CMainWindow::WindowProc
  321.  
  322.   Summary:  Main window procedure for this window object.  See CVirWindow
  323.             in the APPUTIL library (APPUTIL.CPP) for details on how this
  324.             method gets called by the global WindowProc.
  325.  
  326.   Args:     UINT uMsg,
  327.               Windows message that is "sent" to this window.
  328.             WPARAM wParam,
  329.               First message parameter (word sized).
  330.             LPARAM lParam)
  331.               Second message parameter (long sized).
  332.  
  333.   Modifies: ...
  334.  
  335.   Returns:  LRESULT
  336.               Standard Windows WindowProc return value.
  337. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  338. LRESULT CMainWindow::WindowProc(
  339.           UINT uMsg,
  340.           WPARAM wParam,
  341.           LPARAM lParam)
  342. {
  343.   LRESULT lResult = FALSE;
  344.  
  345.   switch (uMsg)
  346.   {
  347.     case WM_CREATE:
  348.       break;
  349.  
  350.     case WM_MEASUREITEM:
  351.       // Get setup for painting text in this window.
  352.       {
  353.         LPMEASUREITEMSTRUCT lpmis = (LPMEASUREITEMSTRUCT) lParam;
  354.         lpmis->itemHeight = m_tm.tmHeight + m_tm.tmExternalLeading;
  355.         lpmis->itemWidth = m_wWidth;
  356.         lResult = TRUE;
  357.       }
  358.  
  359.     case WM_SIZE:
  360.       // Handle a resize of this window.
  361.       m_wWidth = LOWORD(lParam);
  362.       m_wHeight = HIWORD(lParam);
  363.       // Handle a resize of this window.
  364.       // Restart the ball from upper left, clear window.
  365.       m_pGuiBall->Restart();
  366.       break;
  367.  
  368.     case WM_TIMER:
  369.       // This is our timed attempt to continuously paint the moving ball.
  370.       // It doesn't move it. Other non-GUI threads move the virtual ball.
  371.       m_pGuiBall->PaintBall();
  372.       break;
  373.  
  374.     case WM_COMMAND:
  375.       // Dispatch and handle any Menu command messages received.
  376.       lResult = DoMenu(wParam, lParam);
  377.       break;
  378.  
  379.     case WM_CHAR:
  380.       if (wParam == 0x1b)
  381.       {
  382.         // Exit this app if user hits ESC key.
  383.         PostMessage(m_hWnd,WM_CLOSE,0,0);
  384.         break;
  385.       }
  386.     case WM_LBUTTONUP:
  387.     case WM_PAINT:
  388.       // If something major happened or user clicks or hits key then
  389.       // repaint the whole window.
  390.       m_pGuiBall->PaintWin();
  391.       break;
  392.  
  393.     case WM_CLOSE:
  394.       // The user selected Close on the main window's System menu
  395.       // or Exit on the File menu.
  396.     case WM_QUIT:
  397.       // If the app is being quit then close any associated help windows.
  398.     default:
  399.       // Defer all messages NOT handled here to the Default Window Proc.
  400.       lResult = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  401.       break;
  402.   }
  403.  
  404.   return(lResult);
  405. }
  406.  
  407.  
  408. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  409.   Function: UnicodeOk
  410.  
  411.   Summary:  Checks if the platform will handle unicode versions of
  412.             Win32 string API calls.
  413.  
  414.   Args:     void
  415.  
  416.   Returns:  BOOL
  417.               TRUE if unicode support; FALSE if not.
  418. ------------------------------------------------------------------------F-F*/
  419. BOOL UnicodeOk(void)
  420. {
  421.   BOOL bOk = TRUE;
  422.   TCHAR szUserName[MAX_STRING_LENGTH];
  423.   DWORD dwSize = MAX_STRING_LENGTH;
  424.  
  425.   if (!GetUserName(szUserName, &dwSize))
  426.     bOk = ERROR_CALL_NOT_IMPLEMENTED == GetLastError() ? FALSE : TRUE;
  427.  
  428.   return bOk;
  429. }
  430.  
  431.  
  432. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  433.   Function: InitApplication
  434.  
  435.   Summary:  Initializes the application and registers its main window
  436.             class. InitApplication is called only once (in WinMain).
  437.  
  438.   Args:     HINSTANCE hInstance)
  439.               Handle to the first instance of the application.
  440.  
  441.   Returns:  BOOL.
  442.               TRUE if success.
  443.               FALSE if fail.
  444. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  445. BOOL InitApplication(
  446.        HINSTANCE hInstance)
  447. {
  448.   BOOL bOk;
  449.   // The window class for all instances of the main frame window.
  450.   WNDCLASSEX wcf;
  451.  
  452.   // Assign the appropriate values for this main frame window class.
  453.   wcf.cbSize        = sizeof(WNDCLASSEX);
  454.   wcf.cbClsExtra    = 0;            // No per-class extra data.
  455.   wcf.cbWndExtra    = 0;            // No per-window extra data.
  456.   wcf.hInstance     = hInstance;    // Application module instance.
  457.   wcf.lpfnWndProc   = &WindowProc;  // Global Window Procedure (defined in
  458.                                     // APPUTIL for all CVirWindows).
  459.   wcf.hCursor       = LoadCursor(NULL, IDC_ARROW); // Load app cursor.
  460.   wcf.hIcon         = (HICON) LoadIcon(            // Load app icon.
  461.                                 hInstance,
  462.                                 TEXT("AppIcon"));
  463.   wcf.hIconSm       = (HICON) LoadImage(           // Load small icon.
  464.                                 hInstance,
  465.                                 TEXT("AppIcon"),
  466.                                 IMAGE_ICON,
  467.                                 16, 16,
  468.                                 0);
  469.   wcf.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); // Backgnd color.
  470.   wcf.style         = CS_HREDRAW | CS_VREDRAW;     // Class style(s).
  471.   wcf.lpszClassName = TEXT(MAIN_WINDOW_CLASS_NAME_STR); // Class name.
  472.   wcf.lpszMenuName  = TEXT(MAIN_WINDOW_CLASS_MENU_STR); // Menu name.
  473.  
  474.   // Register the window class and return FALSE if unsuccesful.
  475.   bOk = RegisterClassEx(&wcf);
  476.  
  477.   return (bOk);
  478. }
  479.  
  480.  
  481. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  482.   Function: WinMain
  483.  
  484.   Summary:  The Windows main entry point function for this application.
  485.             Initializes the application, the COM Libraries, and starts
  486.             the main application message loop.
  487.  
  488.   Args:     HINSTANCE hInstance,
  489.               Instance handle; a new one for each invocation of this app.
  490.             HINSTANCE hPrevInstance,
  491.               Instance handle of the previous instance. NULL in Win32.
  492.             LPSTR lpCmdLine,
  493.               Windows passes a pointer to the application's
  494.               invocation command line.
  495.             int nCmdShow)
  496.               Bits telling the show state of the application.
  497.  
  498.   Returns:  int
  499.               msg.wParam (upon exit of message loop).
  500.               FALSE if this instance couldn't initialize and run.
  501. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  502. extern "C" int PASCAL WinMain(
  503.                         HINSTANCE hInstance,
  504.                         HINSTANCE hPrevInstance,
  505.                         LPSTR lpCmdLine,
  506.                         int nCmdShow)
  507. {
  508.   CMainWindow* pWin = NULL;
  509.   MSG msg;
  510.   HACCEL hAccel;
  511.   int iRun = FALSE;
  512.  
  513.   // If we were compiled for UNICODE and the platform seems OK with this
  514.   // then proceed.  Else we error and exit the app.
  515.   if (UnicodeOk())
  516.   {
  517.     // Call to initialize the COM Library.  Use the SUCCEEDED macro
  518.     // to detect success.  If fail, then exit app with error message.
  519.     // Initialize COM here in the main process thread to establish this
  520.     // main thread as the founding member of the multi-thread apartment
  521.     // (MTA) of this process. Other worker free-threads will also live
  522.     // in this multi-threaded apartment. The app can have only one such
  523.     // multi-threaded apartment but could have a mixed model with other
  524.     // single-threaded apartments running. This sample has no other
  525.     // single-threaded apartments.
  526.     if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
  527.     {
  528.       // Ensure that DCOM (Distributed COM) is installed and enabled.
  529.       if (DComOk())
  530.       {
  531.         // If we succeeded in initializing the COM Library and DCOM is
  532.         // available, we proceed to initialize the application.  If we
  533.         // can't init the application then we signal shut down with
  534.         // an error message exit.
  535.         iRun = InitApplication(hInstance);
  536.         if (iRun)
  537.         {
  538.           // Assume we'll set iRun to TRUE when initialization is done.
  539.           iRun = FALSE;
  540.           // We are still go for running so we try to create a nifty new
  541.           // CMainWindow object for this app instance.
  542.           pWin = new CMainWindow;
  543.           if (NULL != pWin)
  544.           {
  545.             // Now we initialize an instance of the new CMainWindow.
  546.             // This includes creating the main window.
  547.             if (pWin->InitInstance(hInstance, nCmdShow))
  548.             {
  549.               // Load the keyboard accelerators from the resources.
  550.               hAccel = LoadAccelerators(hInstance, TEXT("AppAccel"));
  551.               if (NULL != hAccel)
  552.               {
  553.                 // Signal App Initialization is successfully done.
  554.                 iRun = TRUE;
  555.               }
  556.             }
  557.           }
  558.         }
  559.  
  560.         if (iRun)
  561.         {
  562.           // If we initialized the app instance properly then we are still
  563.           // go for running.  We then start up the main message pump for
  564.           // the application.
  565.           while (GetMessage(&msg, NULL, 0, 0))
  566.           {
  567.             if (!TranslateAccelerator(pWin->GetHwnd(), hAccel, &msg))
  568.             {
  569.               TranslateMessage(&msg);
  570.               DispatchMessage(&msg);
  571.             }
  572.           }
  573.  
  574.           // We also ask COM to unload any unused COM Servers, including our
  575.           // friend, FRESERVE.
  576.           CoFreeUnusedLibraries();
  577.  
  578.           // We'll pass to Windows the reason why we exited the message loop.
  579.           iRun = msg.wParam;
  580.         }
  581.         else
  582.         {
  583.           // We failed to initialize the application. Put up error message
  584.           // box saying that application couldn't be initialized.  Parent
  585.           // window is desktop (ie, NULL). Exit the failed application
  586.           // (ie, by returning FALSE to WinMain).
  587.           ErrorBox(hInstance, NULL, IDS_APPINITFAILED);
  588.  
  589.           // Delete the CMainWindow object.
  590.           DELETE_POINTER(pWin);
  591.         }
  592.  
  593.         // We're exiting this app (either normally or by init failure) so
  594.         // shut down the COM Library.
  595.         CoUninitialize();
  596.       }
  597.       else
  598.       {
  599.         // If DCOM isn't installed or is not enabled, then
  600.         // indicate an error and exit the app immediately.
  601.         ErrorBox(hInstance, NULL, IDS_NODCOM);
  602.       }
  603.     }
  604.     else
  605.     {
  606.       // We failed to Initialize the COM Library. Put up error message
  607.       // box saying that COM Library couldn't be initialized.  Parent
  608.       // window is desktop (ie, NULL). Exit the failed application
  609.       // (ie, by returning FALSE to WinMain).
  610.       ErrorBox(hInstance, NULL, IDS_COMINITFAILED);
  611.     }
  612.   }
  613.   else
  614.   {
  615.     // If we were compiled for UNICODE but the platform has problems with
  616.     // this then indicate an error and exit the app immediately.
  617.     CHAR szMsg[MAX_STRING_LENGTH];
  618.  
  619.     if (LoadStringA(
  620.           hInstance,
  621.           IDS_NOUNICODE,
  622.           szMsg,
  623.           MAX_STRING_LENGTH))
  624.     {
  625.       MessageBoxA(
  626.         NULL,
  627.         szMsg,
  628.         ERROR_TITLE_STR,
  629.         MB_OK | MB_ICONEXCLAMATION);
  630.     }
  631.   }
  632.  
  633.   return iRun;
  634. }
  635.