home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_2.iso / windows / program / activex / axtsamp.exe / TSBRANCH.EXE / FRECLIEN / FRECLIEN.CPP next >
C/C++ Source or Header  |  1997-01-10  |  24KB  |  661 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 ActiveX 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 .HLP 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.       if (::FileExist(m_szHelpFile))
  277.         ::WinHelp(m_hWnd, m_szHelpFile, HELP_CONTEXT, IDH_CONTENTS);
  278.       else
  279.         m_pMsgBox->ErrorID(IDS_NOHELPFILE);
  280.       break;
  281.     case IDM_HELP_TUTORIAL:
  282.       // Call the APPUTIL utility function, RunTutorial, to Browse the HTML
  283.       // tutorial narrative file associated with this tutorial code sample.
  284.       RunTutorial(m_hInst, m_hWnd, TEXT(HTML_FILE_EXT));
  285.       break;
  286.     case IDM_HELP_TUTSERVER:
  287.       // Call the APPUTIL utility function, RunTutorial, to Browse the HTML
  288.       // tutorial narrative file associated with the COM server.
  289.       RunTutorial(m_hInst, m_hWnd, TEXT(SERVER_TUTFILE_STR));
  290.       break;
  291.     case IDM_HELP_READSOURCE:
  292.       // Call the APPUTIL utility function ReadSource to allow the
  293.       // user to open and read any of the source files of FRECLIEN.
  294.       ReadSource(m_hWnd, &m_ofnFile);
  295.       break;
  296.     case IDM_HELP_ABOUT:
  297.       {
  298.         CAboutBox dlgAboutBox;
  299.  
  300.         // Show the standard About Box dialog for this EXE by telling the
  301.         // dialog C++ object to show itself by invoking its ShowDialog
  302.         // method.  Pass it this EXE instance and the parent window handle.
  303.         // Use a dialog resource ID for the dialog template stored in
  304.         // this EXE module's resources.
  305.         dlgAboutBox.ShowDialog(
  306.           m_hInst,
  307.           MAKEINTRESOURCE(IDM_HELP_ABOUT),
  308.           m_hWnd);
  309.       }
  310.       break;
  311.  
  312.     default:
  313.       // Defer all messages NOT handled here to the Default Window Proc.
  314.       lResult = ::DefWindowProc(m_hWnd, WM_COMMAND, wParam, lParam);
  315.       break;
  316.   }
  317.  
  318.   return(lResult);
  319. }
  320.  
  321.  
  322. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  323.   Method:   CMainWindow::WindowProc
  324.  
  325.   Summary:  Main window procedure for this window object.  See CVirWindow
  326.             in the APPUTIL library (APPUTIL.CPP) for details on how this
  327.             method gets called by the global WindowProc.
  328.  
  329.   Args:     UINT uMsg,
  330.               Windows message that is "sent" to this window.
  331.             WPARAM wParam,
  332.               First message parameter (word sized).
  333.             LPARAM lParam)
  334.               Second message parameter (long sized).
  335.  
  336.   Modifies: ...
  337.  
  338.   Returns:  LRESULT
  339.               Standard Windows WindowProc return value.
  340. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  341. LRESULT CMainWindow::WindowProc(
  342.           UINT uMsg,
  343.           WPARAM wParam,
  344.           LPARAM lParam)
  345. {
  346.   LRESULT lResult = FALSE;
  347.  
  348.   switch (uMsg)
  349.   {
  350.     case WM_CREATE:
  351.       break;
  352.  
  353.     case WM_MEASUREITEM:
  354.       // Get setup for painting text in this window.
  355.       {
  356.         LPMEASUREITEMSTRUCT lpmis = (LPMEASUREITEMSTRUCT) lParam;
  357.         lpmis->itemHeight = m_tm.tmHeight + m_tm.tmExternalLeading;
  358.         lpmis->itemWidth = m_wWidth;
  359.         lResult = TRUE;
  360.       }
  361.  
  362.     case WM_SIZE:
  363.       // Handle a resize of this window.
  364.       m_wWidth = LOWORD(lParam);
  365.       m_wHeight = HIWORD(lParam);
  366.       // Handle a resize of this window.
  367.       // Restart the ball from upper left, clear window.
  368.       m_pGuiBall->Restart();
  369.       break;
  370.  
  371.     case WM_TIMER:
  372.       // This is our timed attempt to continuously paint the moving ball.
  373.       // It doesn't move it. Other non-GUI threads move the virtual ball.
  374.       m_pGuiBall->PaintBall();
  375.       break;
  376.  
  377.     case WM_COMMAND:
  378.       // Dispatch and handle any Menu command messages received.
  379.       lResult = DoMenu(wParam, lParam);
  380.       break;
  381.  
  382.     case WM_CHAR:
  383.       if (wParam == 0x1b)
  384.       {
  385.         // Exit this app if user hits ESC key.
  386.         PostMessage(m_hWnd,WM_CLOSE,0,0);
  387.         break;
  388.       }
  389.     case WM_LBUTTONUP:
  390.     case WM_PAINT:
  391.       // If something major happened or user clicks or hits key then
  392.       // repaint the whole window.
  393.       m_pGuiBall->PaintWin();
  394.       break;
  395.  
  396.     case WM_CLOSE:
  397.       // The user selected Close on the main window's System menu
  398.       // or Exit on the File menu.
  399.     case WM_QUIT:
  400.       // If the app is being quit then close any associated help windows.
  401.       // ::WinHelp(m_hWnd, m_szHelpFile, HELP_QUIT, 0);
  402.     default:
  403.       // Defer all messages NOT handled here to the Default Window Proc.
  404.       lResult = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  405.       break;
  406.   }
  407.  
  408.   return(lResult);
  409. }
  410.  
  411.  
  412. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  413.   Function: UnicodeOk
  414.  
  415.   Summary:  Checks if the platform will handle unicode versions of
  416.             Win32 string API calls.
  417.  
  418.   Args:     void
  419.  
  420.   Returns:  BOOL
  421.               TRUE if unicode support; FALSE if not.
  422. ------------------------------------------------------------------------F-F*/
  423. BOOL UnicodeOk(void)
  424. {
  425.   BOOL bOk = TRUE;
  426.   TCHAR szUserName[MAX_STRING_LENGTH];
  427.   DWORD dwSize = MAX_STRING_LENGTH;
  428.  
  429.   if (!GetUserName(szUserName, &dwSize))
  430.     bOk = ERROR_CALL_NOT_IMPLEMENTED == GetLastError() ? FALSE : TRUE;
  431.  
  432.   return bOk;
  433. }
  434.  
  435.  
  436. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  437.   Function: InitApplication
  438.  
  439.   Summary:  Initializes the application and registers its main window
  440.             class. InitApplication is called only once (in WinMain).
  441.  
  442.   Args:     HINSTANCE hInstance)
  443.               Handle to the first instance of the application.
  444.  
  445.   Returns:  BOOL.
  446.               TRUE if success.
  447.               FALSE if fail.
  448. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  449. BOOL InitApplication(
  450.        HINSTANCE hInstance)
  451. {
  452.   BOOL bOK;
  453.   // The window class for all instances of the main frame window.
  454.   WNDCLASSEX wcf;
  455.  
  456.   // Assign the appropriate values for this main frame window class.
  457.   wcf.cbSize        = sizeof(WNDCLASSEX);
  458.   wcf.style         = CS_HREDRAW | CS_VREDRAW; // Class style(s).
  459.   wcf.lpfnWndProc   = &WindowProc;             // Global Window Procedure for
  460.                                                // all windows of this class.
  461.   wcf.cbClsExtra    = 0;                       // No per-class extra data.
  462.   wcf.cbWndExtra    = 0;                       // No per-window extra data.
  463.   wcf.hInstance     = hInstance;               // Owner of this class.
  464.   wcf.hbrBackground = GetStockObject(WHITE_BRUSH);      // Default color.
  465.   wcf.lpszMenuName  = TEXT(MAIN_WINDOW_CLASS_MENU_STR); // Menu name from .RC.
  466.   wcf.lpszClassName = TEXT(MAIN_WINDOW_CLASS_NAME_STR); // Class name from .RC.
  467.   wcf.hCursor       = LoadCursor(NULL, IDC_ARROW);      // Cursor.
  468.   wcf.hIcon         = LoadIcon(                         // Icon name from .RC.
  469.                         hInstance,
  470.                         TEXT("AppIcon"));
  471.   wcf.hIconSm       = LoadImage(                        // Load small icon.
  472.                         hInstance,
  473.                         TEXT("AppIcon"),
  474.                         IMAGE_ICON,
  475.                         16, 16,
  476.                         0);
  477.  
  478.   // Register the window class and return FALSE if unsuccesful.
  479.   bOK = RegisterClassEx(&wcf);
  480.   if (!bOK)
  481.   {
  482.     // If RegisterClassEx() didn't work then try RegisterClass().
  483.     bOK = RegisterClass((LPWNDCLASS)&wcf.style);
  484.   }
  485.  
  486.   return (bOK);
  487. }
  488.  
  489.  
  490. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  491.   Function: WinMain
  492.  
  493.   Summary:  The Windows main entry point function for this application.
  494.             Initializes the application, the COM Libraries, and starts
  495.             the main application message loop.
  496.  
  497.   Args:     HINSTANCE hInstance,
  498.               Instance handle; a new one for each invocation of this app.
  499.             HINSTANCE hPrevInstance,
  500.               Instance handle of the previous instance. NULL in Win32.
  501.             LPSTR lpCmdLine,
  502.               Windows passes a pointer to the application's
  503.               invocation command line.
  504.             int nCmdShow)
  505.               Bits telling the show state of the application.
  506.  
  507.   Returns:  int
  508.               msg.wParam (upon exit of message loop).
  509.               FALSE if this instance couldn't initialize and run.
  510. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  511. extern "C" int PASCAL WinMain(
  512.                         HINSTANCE hInstance,
  513.                         HINSTANCE hPrevInstance,
  514.                         LPSTR lpCmdLine,
  515.                         int nCmdShow)
  516. {
  517.   CMainWindow* pWin = NULL;
  518.   MSG msg;
  519.   HACCEL hAccel;
  520.   int iRun = FALSE;
  521.  
  522.   // If we were compiled for UNICODE and the platform seems OK with this
  523.   // then proceed.  Else we error and exit the app.
  524.   if (UnicodeOk())
  525.   {
  526.     // Call to initialize the COM Library.  Use the SUCCEEDED macro
  527.     // to detect success.  If fail, then exit app with error message.
  528.     // Initialize COM here in the main process thread to establish this
  529.     // main thread as the founding member of the multi-apartment thread
  530.     // of this process. Other worker free-threads will also live in this
  531.     // multi-threaded apartment. The app can have only one such multi-
  532.     // threaded apartment but could have a mixed model with other
  533.     // single-threaded apartments running. This sample has no other
  534.     // single-threaded apartments.
  535.     if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
  536.     {
  537.       // If we succeeded in initializing the COM Library we proceed to
  538.       // initialize the application.  If we can't init the application
  539.       // then we signal shut down with an error message exit.
  540.       iRun = InitApplication(hInstance);
  541.       if (iRun)
  542.       {
  543.         // Assume we'll set iRun to TRUE when initialization is done.
  544.         iRun = FALSE;
  545.         // We are still go for running so we try to create a nifty new
  546.         // CMainWindow object for this app instance.
  547.         pWin = new CMainWindow;
  548.         if (NULL != pWin)
  549.         {
  550.           // Now we initialize an instance of the new CMainWindow.
  551.           // This includes creating the main window.  Note: if
  552.           // InitInstance fails then it would have already deleted
  553.           // pWin so we wouldn't need to delete it here.
  554.           if (pWin->InitInstance(hInstance, nCmdShow))
  555.           {
  556.             // Load the keyboard accelerators from the resources.
  557.             hAccel = LoadAccelerators(hInstance, TEXT("AppAccel"));
  558.             if (NULL != hAccel)
  559.             {
  560.               // Signal App Initialization is successfully done.
  561.               iRun = TRUE;
  562.             }
  563.           }
  564.         }
  565.       }
  566.  
  567.       if (iRun)
  568.       {
  569.         // If we initialized the app instance properly then we are still
  570.         // go for running.  We then start up the main message pump for
  571.         // the application.
  572.         while (GetMessage(&msg, NULL, 0, 0))
  573.         {
  574.           if (!TranslateAccelerator(pWin->GetHwnd(), hAccel, &msg))
  575.           {
  576.             TranslateMessage(&msg);
  577.             DispatchMessage(&msg);
  578.           }
  579.         }
  580.  
  581.         // We also ask COM to unload any unused COM Servers, including our
  582.         // friend, FRESERVE.
  583.         CoFreeUnusedLibraries();
  584.  
  585.         // We'll pass to Windows the reason why we exited the message loop.
  586.         iRun = msg.wParam;
  587.       }
  588.       else
  589.       {
  590.         // We failed to initialize the application--issue an error
  591.         // messagebox.
  592.         TCHAR szMsg[MAX_STRING_LENGTH];
  593.  
  594.         // Load the error message string from the resources.
  595.         if (LoadString(
  596.               hInstance,
  597.               IDS_APPINITFAILED,
  598.               szMsg,
  599.               MAX_STRING_LENGTH))
  600.         {
  601.           // Put up error message box saying that application couldn't be
  602.           // initialized.  Parent window is desktop (ie, NULL).
  603.           MessageBox(
  604.             NULL,
  605.             szMsg,
  606.             TEXT(ERROR_TITLE_STR),
  607.             MB_OK | MB_ICONEXCLAMATION);
  608.         }
  609.         DELETE_POINTER(pWin);
  610.       }
  611.  
  612.       // We're exiting this app (either normally or by init failure) so
  613.       // shut down the COM Library.
  614.       CoUninitialize();
  615.     }
  616.     else
  617.     {
  618.       // We failed to Initialize the COM Library.
  619.       TCHAR szMsg[MAX_STRING_LENGTH];
  620.  
  621.       // Load the error message string from the resources.
  622.       if (LoadString(
  623.             hInstance,
  624.             IDS_COMINITFAILED,
  625.             szMsg,
  626.             MAX_STRING_LENGTH))
  627.       {
  628.         // Put up error message box saying that COM Library
  629.         // couldn't be initialized.  Parent window is desktop (ie, NULL).
  630.         // And exit the failed application.
  631.         MessageBox(
  632.           NULL,
  633.           szMsg,
  634.           TEXT(ERROR_TITLE_STR),
  635.           MB_OK | MB_ICONEXCLAMATION);
  636.       }
  637.     }
  638.   }
  639.   else
  640.   {
  641.     // If we were compiled for UNICODE but the platform has problems with
  642.     // this then indicate an error and exit the app immediately.
  643.     CHAR szMsg[MAX_STRING_LENGTH];
  644.  
  645.     if (LoadStringA(
  646.           hInstance,
  647.           IDS_NOUNICODE,
  648.           szMsg,
  649.           MAX_STRING_LENGTH))
  650.     {
  651.       MessageBoxA(
  652.         NULL,
  653.         szMsg,
  654.         ERROR_TITLE_STR,
  655.         MB_OK | MB_ICONEXCLAMATION);
  656.     }
  657.   }
  658.  
  659.   return iRun;
  660. }
  661.