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 / locserve / locserve.cpp < prev    next >
C/C++ Source or Header  |  1997-08-30  |  33KB  |  988 lines

  1. /*+==========================================================================
  2.   File:      LOCSERVE.CPP
  3.  
  4.   Summary:   Based largely on the DLLCLIEN.EXE source code, this
  5.              module implements the main framework for an Win32 .EXE
  6.              application.  This application is meant to run hidden as a
  7.              local COM server that offers the Car, UtilityCar, and
  8.              CruiseCar components.  Though this local server offers a
  9.              simple main window and menu system these are not seen during
  10.              normal operation.  This server can be run stand-alone for
  11.              debugging purposes by explicitly using the -embedding command
  12.              line switch and invoking LOCSERVE prior to the client.
  13.  
  14.              This local server supports self-registration and this module
  15.              recognizes the -RegServer and -UnregServer command line
  16.              switches and has the appropriate functions (RegisterServer
  17.              and UnregisterServer) to register this local server in the
  18.              system Registry.
  19.  
  20.              For a comprehensive tutorial code tour of LOCSERVE's
  21.              contents and offerings see the tutorial LOCSERVE.HTM file.
  22.              For more specific technical details on the internal workings
  23.              see the comments dispersed throughout the LOCSERVE source code.
  24.              For more details on the LOCCLIEN.EXE that LOCSERVE works with
  25.              see the LOCCLIEN.HTM file in the main tutorial directory.
  26.  
  27.   Classes:   CMainWindow
  28.  
  29.   Functions: InitApplication, WinMain
  30.  
  31.   Origin:    11-14-95: atrent - Editor-inheritance from the DLLCLIEN source.
  32.  
  33. ----------------------------------------------------------------------------
  34.   This file is part of the Microsoft COM Tutorial Code Samples.
  35.  
  36.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  37.  
  38.   This source code is intended only as a supplement to Microsoft
  39.   Development Tools and/or on-line documentation.  See these other
  40.   materials for detailed information regarding Microsoft code samples.
  41.  
  42.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  43.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  44.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  45.   PARTICULAR PURPOSE.
  46. ==========================================================================+*/
  47.  
  48. /*--------------------------------------------------------------------------
  49.   We include WINDOWS.H for all Win32 applications.
  50.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  51.   We include INITGUID.H only once (here) in the entire app because we
  52.     will be defining GUIDs and want them as constants in the data segment.
  53.   We include APPUTIL.H because we will be building this application using
  54.     the convenient Virtual Window and Dialog classes and other
  55.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  56.   We include MICARS.H and CARGUIDS.H for the common car-related Interface
  57.     class, GUID, and CLSID specifications.
  58.   We include LOCSERVE.H because it has class and resource definitions
  59.     specific to this LOCSERVE application.
  60.   We include SERVER.H because it has the necessary internal class and
  61.     resource definitions for this server.
  62.   We include FACTORY.H because it has the necessary internal class factory
  63.     declarations for this component server.
  64. ---------------------------------------------------------------------------*/
  65. #include <windows.h>
  66. #include <ole2.h>
  67. #include <initguid.h>
  68. #include <apputil.h>
  69. #include <micars.h>
  70. #include <carguids.h>
  71. #include "locserve.h"
  72. #include "server.h"
  73. #include "factory.h"
  74.  
  75.  
  76. // We encapsulate the control of this COM server (eg, lock and object
  77. // counting) in a server control C++ object.  Here is it's pointer.
  78. CServer*  g_pServer = NULL;
  79.  
  80. // Here is a pointer for use by the global Trace Message logging macros.
  81. CSendLog* g_pMsgLog = NULL;
  82.  
  83.  
  84. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  85.   Method:   CMainWindow::CMainWindow
  86.  
  87.   Summary:  CMainWindow Constructor.
  88.  
  89.   Args:     .
  90.  
  91.   Modifies: m_pMsgBox, m_pMsgLog.
  92.  
  93.   Returns:  .
  94. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  95. CMainWindow::CMainWindow()
  96. {
  97.   // Null the Message object pointers.
  98.   m_pMsgBox = NULL;
  99.   m_pMsgLog = NULL;
  100. }
  101.  
  102.  
  103. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  104.   Method:   CMainWindow::~CMainWindow
  105.  
  106.   Summary:  CMainWindow Destructor.  Destruction of the main window
  107.             indicates that the application should quit and thus the
  108.             PostQuitMessage API is called.
  109.  
  110.   Args:     .
  111.  
  112.   Modifies: .
  113.  
  114.   Returns:  .
  115. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  116. CMainWindow::~CMainWindow()
  117. {
  118.   // CMainWindow is derived from CVirWindow which traps the WM_DESTROY
  119.   // message and causes a delete of CMainWindow which in turn causes this
  120.   // destructor to run. The WM_DESTROY results when the window is destoyed
  121.   // after a close of the window. Prior to exiting the main message loop:
  122.  
  123.   // Close down the factories (ie, Revoke and release the Class Factories).
  124.   if (NULL != g_pServer)
  125.     g_pServer->CloseFactories();
  126.  
  127.   LOG("L: Exiting LOCSERVE local server application.");
  128.  
  129.   // We delete the CMsgBox and CMsgLog objects that were made in
  130.   // Initinstance and the server control object, CServer.
  131.   DELETE_POINTER(g_pServer);
  132.   DELETE_POINTER(m_pMsgBox);
  133.   DELETE_POINTER(m_pMsgLog);
  134.  
  135.   // We then post a WM_QUIT message to cause an exit of the main thread's
  136.   // message loop and an exit of this instance of the application.
  137.   PostQuitMessage(0);
  138. }
  139.  
  140.  
  141. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  142.   Function: SetRegKeyValue
  143.  
  144.   Summary:  Internal utility function to set a Key, Subkey, and value
  145.             in the system Registry under HKEY_CLASSES_ROOT.
  146.  
  147.   Args:     LPTSTR pszKey,
  148.             LPTSTR pszSubkey,
  149.             LPTSTR pszValue)
  150.  
  151.   Returns:  BOOL
  152.               TRUE if success; FALSE if not.
  153. ------------------------------------------------------------------------F-F*/
  154. BOOL SetRegKeyValue(
  155.        LPTSTR pszKey,
  156.        LPTSTR pszSubkey,
  157.        LPTSTR pszValue)
  158. {
  159.   BOOL bOk = FALSE;
  160.   LONG ec;
  161.   HKEY hKey;
  162.   TCHAR szKey[MAX_STRING_LENGTH];
  163.  
  164.   lstrcpy(szKey, pszKey);
  165.  
  166.   if (NULL != pszSubkey)
  167.   {
  168.     lstrcat(szKey, TEXT("\\"));
  169.     lstrcat(szKey, pszSubkey);
  170.   }
  171.  
  172.   ec = RegCreateKeyEx(
  173.          HKEY_CLASSES_ROOT,
  174.          szKey,
  175.          0,
  176.          NULL,
  177.          REG_OPTION_NON_VOLATILE,
  178.          KEY_ALL_ACCESS,
  179.          NULL,
  180.          &hKey,
  181.          NULL);
  182.  
  183.   if (ERROR_SUCCESS == ec)
  184.   {
  185.     if (NULL != pszValue)
  186.     {
  187.       ec = RegSetValueEx(
  188.              hKey,
  189.              NULL,
  190.              0,
  191.              REG_SZ,
  192.              (BYTE *)pszValue,
  193.              (lstrlen(pszValue)+1)*sizeof(TCHAR));
  194.     }
  195.     if (ERROR_SUCCESS == ec)
  196.       bOk = TRUE;
  197.     RegCloseKey(hKey);
  198.   }
  199.  
  200.   return bOk;
  201. }
  202.  
  203.  
  204. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  205.   Method:   CMainWindow::RegisterServer
  206.  
  207.   Summary:  Member function used by this server to register itself in the
  208.             system registry.
  209.  
  210.   Args:     void.
  211.  
  212.   Returns:  BOOL
  213.               TRUE
  214. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  215. BOOL CMainWindow::RegisterServer(void)
  216. {
  217.   BOOL  bOk = TRUE;
  218.   TCHAR szID[GUID_SIZE+1];
  219.   TCHAR szCLSID[GUID_SIZE+32];
  220.   TCHAR szModulePath[MAX_PATH];
  221.  
  222.   // Obtain the path to this module's executable file for later use.
  223.   GetModuleFileName(
  224.     g_pServer->m_hInstServer,
  225.     szModulePath,
  226.     sizeof(szModulePath)/sizeof(TCHAR));
  227.  
  228.   /*-------------------------------------------------------------------------
  229.     Create registry entries for the LocCar Component.
  230.   -------------------------------------------------------------------------*/
  231.   // Create some base key strings.
  232.   StringFromGUID2(CLSID_LocCar, szID, GUID_SIZE);
  233.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  234.   lstrcat(szCLSID, szID);
  235.  
  236.   // Create ProgID keys.
  237.   SetRegKeyValue(
  238.     TEXT("CTS.LocCar.1"),
  239.     NULL,
  240.     TEXT("LocCar Component - LOCSERVE Code Sample"));
  241.   SetRegKeyValue(
  242.     TEXT("CTS.LocCar.1"),
  243.     TEXT("CLSID"),
  244.     szID);
  245.  
  246.   // Create VersionIndependentProgID keys.
  247.   SetRegKeyValue(
  248.     TEXT("CTS.LocCar"),
  249.     NULL,
  250.     TEXT("LocCar Component - LOCSERVE Code Sample"));
  251.   SetRegKeyValue(
  252.     TEXT("CTS.LocCar"),
  253.     TEXT("CurVer"),
  254.     TEXT("CTS.LocCar.1"));
  255.   SetRegKeyValue(
  256.     TEXT("CTS.LocCar"),
  257.     TEXT("CLSID"),
  258.     szID);
  259.  
  260.   // Create entries under CLSID.
  261.   SetRegKeyValue(
  262.     szCLSID,
  263.     NULL,
  264.     TEXT("LocCar Component - LOCSERVE Code Sample"));
  265.   SetRegKeyValue(
  266.     szCLSID,
  267.     TEXT("ProgID"),
  268.     TEXT("CTS.LocCar.1"));
  269.   SetRegKeyValue(
  270.     szCLSID,
  271.     TEXT("VersionIndependentProgID"),
  272.     TEXT("CTS.LocCar"));
  273.   SetRegKeyValue(
  274.     szCLSID,
  275.     TEXT("NotInsertable"),
  276.     NULL);
  277.   SetRegKeyValue(
  278.     szCLSID,
  279.     TEXT("LocalServer32"),
  280.     szModulePath);
  281.  
  282.   /*-------------------------------------------------------------------------
  283.     Create registry entries for the LocUtilityCar Component.
  284.   -------------------------------------------------------------------------*/
  285.   // Create some base key strings.
  286.   StringFromGUID2(CLSID_LocUtilityCar, szID, GUID_SIZE);
  287.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  288.   lstrcat(szCLSID, szID);
  289.  
  290.   // Create ProgID keys.
  291.   SetRegKeyValue(
  292.     TEXT("CTS.LocUtilityCar.1"),
  293.     NULL,
  294.     TEXT("LocUtilityCar Component - LOCSERVE Code Sample"));
  295.   SetRegKeyValue(
  296.     TEXT("CTS.LocUtilityCar.1"),
  297.     TEXT("CLSID"),
  298.     szID);
  299.  
  300.   // Create VersionIndependentProgID keys.
  301.   SetRegKeyValue(
  302.     TEXT("CTS.LocUtilityCar"),
  303.     NULL,
  304.     TEXT("LocUtilityCar Component - LOCSERVE Code Sample"));
  305.   SetRegKeyValue(
  306.     TEXT("CTS.LocUtilityCar"),
  307.     TEXT("CurVer"),
  308.     TEXT("CTS.LocUtilityCar.1"));
  309.   SetRegKeyValue(
  310.     TEXT("CTS.LocUtilityCar"),
  311.     TEXT("CLSID"),
  312.     szID);
  313.  
  314.   // Create entries under CLSID.
  315.   SetRegKeyValue(
  316.     szCLSID,
  317.     NULL,
  318.     TEXT("LocUtilityCar Component - LOCSERVE Code Sample"));
  319.   SetRegKeyValue(
  320.     szCLSID,
  321.     TEXT("ProgID"),
  322.     TEXT("CTS.LocUtilityCar.1"));
  323.   SetRegKeyValue(
  324.     szCLSID,
  325.     TEXT("VersionIndependentProgID"),
  326.     TEXT("CTS.LocUtilityCar"));
  327.   SetRegKeyValue(
  328.     szCLSID,
  329.     TEXT("NotInsertable"),
  330.     NULL);
  331.   SetRegKeyValue(
  332.     szCLSID,
  333.     TEXT("LocalServer32"),
  334.     szModulePath);
  335.  
  336.   /*-------------------------------------------------------------------------
  337.     Create registry entries for the LocCruiseCar Component.
  338.   -------------------------------------------------------------------------*/
  339.   // Create some base key strings.
  340.   StringFromGUID2(CLSID_LocCruiseCar, szID, GUID_SIZE);
  341.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  342.   lstrcat(szCLSID, szID);
  343.  
  344.   // Create ProgID keys.
  345.   SetRegKeyValue(
  346.     TEXT("CTS.LocCruiseCar.1"),
  347.     NULL,
  348.     TEXT("LocCruiseCar Component - LOCSERVE Code Sample"));
  349.   SetRegKeyValue(
  350.     TEXT("CTS.LocCruiseCar.1"),
  351.     TEXT("CLSID"),
  352.     szID);
  353.  
  354.   // Create VersionIndependentProgID keys.
  355.   SetRegKeyValue(
  356.     TEXT("CTS.LocCruiseCar"),
  357.     NULL,
  358.     TEXT("LocCruiseCar Component - LOCSERVE Code Sample"));
  359.   SetRegKeyValue(
  360.     TEXT("CTS.LocCruiseCar"),
  361.     TEXT("CurVer"),
  362.     TEXT("CTS.LocCruiseCar.1"));
  363.   SetRegKeyValue(
  364.     TEXT("CTS.LocCruiseCar"),
  365.     TEXT("CLSID"),
  366.     szID);
  367.  
  368.   // Create entries under CLSID.
  369.   SetRegKeyValue(
  370.     szCLSID,
  371.     NULL,
  372.     TEXT("LocCruiseCar Component - LOCSERVE Code Sample"));
  373.   SetRegKeyValue(
  374.     szCLSID,
  375.     TEXT("ProgID"),
  376.     TEXT("CTS.LocCruiseCar.1"));
  377.   SetRegKeyValue(
  378.     szCLSID,
  379.     TEXT("VersionIndependentProgID"),
  380.     TEXT("CTS.LocCruiseCar"));
  381.   SetRegKeyValue(
  382.     szCLSID,
  383.     TEXT("NotInsertable"),
  384.     NULL);
  385.   SetRegKeyValue(
  386.     szCLSID,
  387.     TEXT("LocalServer32"),
  388.     szModulePath);
  389.  
  390.   return bOk;
  391. }
  392.  
  393.  
  394. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  395.   Method:   CMainWindow::UnregisterServer
  396.  
  397.   Summary:  Member function used by this server to unregister itself from
  398.             the system Registry.
  399.  
  400.   Args:     void.
  401.  
  402.   Returns:  BOOL
  403.               TRUE
  404. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  405. BOOL CMainWindow::UnregisterServer(void)
  406. {
  407.   BOOL  bOk = TRUE;
  408.   TCHAR szID[GUID_SIZE+1];
  409.   TCHAR szCLSID[GUID_SIZE+32];
  410.   TCHAR szTemp[MAX_PATH+GUID_SIZE];
  411.  
  412.   /*-------------------------------------------------------------------------
  413.     Delete registry entries for the LocCar Component.
  414.   -------------------------------------------------------------------------*/
  415.   //Create some base key strings.
  416.   StringFromGUID2(CLSID_LocCar, szID, GUID_SIZE);
  417.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  418.   lstrcat(szCLSID, szID);
  419.  
  420.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCar\\CurVer"));
  421.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCar\\CLSID"));
  422.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCar"));
  423.  
  424.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCar.1\\CLSID"));
  425.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCar.1"));
  426.  
  427.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("ProgID"));
  428.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  429.  
  430.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("VersionIndependentProgID"));
  431.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  432.  
  433.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("NotInsertable"));
  434.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  435.  
  436.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("LocalServer32"));
  437.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  438.  
  439.   RegDeleteKey(HKEY_CLASSES_ROOT, szCLSID);
  440.  
  441.   /*-------------------------------------------------------------------------
  442.     Delete registry entries for the LocUtilityCar Component.
  443.   -------------------------------------------------------------------------*/
  444.   //Create some base key strings.
  445.   StringFromGUID2(CLSID_LocUtilityCar, szID, GUID_SIZE);
  446.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  447.   lstrcat(szCLSID, szID);
  448.  
  449.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocUtilityCar\\CurVer"));
  450.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocUtilityCar\\CLSID"));
  451.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocUtilityCar"));
  452.  
  453.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocUtilityCar.1\\CLSID"));
  454.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocUtilityCar.1"));
  455.  
  456.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("ProgID"));
  457.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  458.  
  459.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("VersionIndependentProgID"));
  460.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  461.  
  462.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("NotInsertable"));
  463.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  464.  
  465.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("LocalServer32"));
  466.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  467.  
  468.   RegDeleteKey(HKEY_CLASSES_ROOT, szCLSID);
  469.  
  470.   /*-------------------------------------------------------------------------
  471.     Delete registry entries for the LocCruiseCar Component.
  472.   -------------------------------------------------------------------------*/
  473.   //Create some base key strings.
  474.   StringFromGUID2(CLSID_LocCruiseCar, szID, GUID_SIZE);
  475.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  476.   lstrcat(szCLSID, szID);
  477.  
  478.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCruiseCar\\CurVer"));
  479.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCruiseCar\\CLSID"));
  480.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCruiseCar"));
  481.  
  482.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCruiseCar.1\\CLSID"));
  483.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.LocCruiseCar.1"));
  484.  
  485.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("ProgID"));
  486.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  487.  
  488.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("VersionIndependentProgID"));
  489.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  490.  
  491.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("NotInsertable"));
  492.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  493.  
  494.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("LocalServer32"));
  495.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  496.  
  497.   RegDeleteKey(HKEY_CLASSES_ROOT, szCLSID);
  498.  
  499.   return bOk;
  500. }
  501.  
  502.  
  503. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  504.   Method:   CMainWindow::DoMenu
  505.  
  506.   Summary:  Dispatch and handle the main menu commands.
  507.  
  508.   Args:     WPARAM wParam,
  509.               First message parameter (word sized).
  510.             LPARAM lParam)
  511.               Second message parameter (long sized).
  512.  
  513.   Modifies: ...
  514.  
  515.   Returns:  LRESULT
  516.               Standard Windows WindowProc return value.
  517. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  518. LRESULT CMainWindow::DoMenu(
  519.           WPARAM wParam,
  520.           LPARAM lParam)
  521. {
  522.   LRESULT lResult = FALSE;
  523.   HMENU hMenu  = ::GetMenu(m_hWnd);
  524.  
  525.   switch (LOWORD(wParam))
  526.   {
  527.     //----------------------------------------------------------------------
  528.     // Handle File Menu Commands.
  529.     //----------------------------------------------------------------------
  530.     case IDM_FILE_EXIT:
  531.       // The user commands us to exit this application so we tell the
  532.       // Main window to close itself.
  533.       ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
  534.       break;
  535.  
  536.     //----------------------------------------------------------------------
  537.     // Handle Log Menu Commands.
  538.     //----------------------------------------------------------------------
  539.     case IDM_LOG_LOGCLEAR:
  540.       // Clear the message log.
  541.       g_pMsgLog->Clear();
  542.       // Use macro to log messages.
  543.       LOGID(IDS_LOGTO_SERVER);
  544.       break;
  545.     case IDM_LOG_COPYCLIP:
  546.       // Copy trace message log to clipboard.
  547.       g_pMsgLog->Copy();
  548.       break;
  549.  
  550.     //----------------------------------------------------------------------
  551.     // Handle Help Menu Commands.
  552.     //----------------------------------------------------------------------
  553.     case IDM_HELP_ABOUT:
  554.       {
  555.         CAboutBox dlgAboutBox;
  556.         HWND hWnd = FindWindow(NULL, TEXT(CLIENT_WINDOW_TITLE));
  557.         if (NULL == hWnd)
  558.           hWnd = m_hWnd;
  559.  
  560.         LOG("L: === Help Menu: About LOCSERVE.");
  561.         // Show the standard About Box dialog for this EXE by telling the
  562.         // dialog C++ object to show itself by invoking its ShowDialog
  563.         // method.  Pass it this EXE instance and the parent window handle.
  564.         // Use a dialog resource ID for the dialog template stored in
  565.         // this EXE module's resources.
  566.         dlgAboutBox.ShowDialog(
  567.           m_hInst,
  568.           MAKEINTRESOURCE(IDM_HELP_ABOUT),
  569.           hWnd);
  570.       }
  571.       break;
  572.  
  573.     default:
  574.       // Defer all messages NOT handled here to the Default Window Proc.
  575.       lResult = ::DefWindowProc(m_hWnd, WM_COMMAND, wParam, lParam);
  576.       break;
  577.   }
  578.  
  579.   return(lResult);
  580. }
  581.  
  582.  
  583. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  584.   Method:   CMainWindow::InitInstance
  585.  
  586.   Summary:  Instantiates an instance of the main application window.
  587.             This method must be called only once, immediately after
  588.             window class construction.  We take care to delete 'this'
  589.             CMainWindow if we must return the error condition FALSE.
  590.  
  591.   Args:     HINSTANCE hInstance,
  592.               Handle of the application instance.
  593.             int nShow)
  594.               Command to pass to ShowWindow.
  595.  
  596.   Modifies: m_pMsgBox, m_pMsgLog.
  597.  
  598.   Returns:  BOOL.
  599.               TRUE if succeeded.
  600.               FALSE if failed.
  601. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  602. BOOL CMainWindow::InitInstance(
  603.        HINSTANCE hInstance,
  604.        int nShow)
  605. {
  606.   BOOL bOk = FALSE;
  607.   HWND hWnd;
  608.  
  609.   // Create the Message Box object.
  610.   m_pMsgBox = new CMsgBox;
  611.   m_pMsgLog = new CSendLog;
  612.  
  613.   if (NULL != m_pMsgBox && NULL != m_pMsgLog)
  614.   {
  615.     // Note, the Create method sets the m_hWnd member so we don't
  616.     // need to set it explicitly here first.
  617.  
  618.     // Here is the create of this window.  Size the window reasonably.
  619.     // Create sets both m_hInst and m_hWnd.
  620.     hWnd = Create(
  621.              TEXT(MAIN_WINDOW_CLASS_NAME_STR),
  622.              TEXT(MAIN_WINDOW_TITLE_STR),
  623.              WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
  624.                | WS_MAXIMIZEBOX | WS_THICKFRAME,
  625.              CW_USEDEFAULT,
  626.              CW_USEDEFAULT,
  627.              ::GetSystemMetrics(SM_CXSCREEN)*3/5,
  628.              ::GetSystemMetrics(SM_CYSCREEN)*3/5,
  629.              NULL,
  630.              NULL,
  631.              hInstance);
  632.     if (hWnd)
  633.     {
  634.       // Assign the server's copy of the main window handle.
  635.       g_pServer->m_hWndServer = m_hWnd;
  636.  
  637.       // Init the Message Box object.
  638.       if (m_pMsgBox->Init(m_hInst, m_hWnd))
  639.       {
  640.         bOk = TRUE;
  641.         // If the Client's Main window is found then set up logging to it.
  642.         hWnd = FindWindow(NULL, TEXT(CLIENT_WINDOW_TITLE));
  643.         if (NULL != hWnd)
  644.         {
  645.           // Tell the CSendLog object that we are logging to client.
  646.           m_pMsgLog->LogToServer(FALSE);
  647.           // Assign the global MsgLog pointer.
  648.           g_pMsgLog = m_pMsgLog;
  649.           m_pMsgLog->SetClient(m_hInst, m_hWnd, hWnd);
  650.           LOGID(IDS_LOGTO_CLIENT);
  651.         }
  652.         else
  653.         {
  654.           // If the client window can't be found then create and show the
  655.           // log display in this local server.
  656.  
  657.           // Indicate we are logging to our own server log facility.
  658.           m_pMsgLog->LogToServer(TRUE);
  659.  
  660.           // Ensure the new window is shown on screen and its content
  661.           // is painted.
  662.           ::ShowWindow(m_hWnd, nShow);
  663.           ::UpdateWindow(m_hWnd);
  664.  
  665.           // Create the Trace Message Log ListBox as a child window that
  666.           // fits the client area of the Main Window (the TRUE 3rd argument
  667.           // specifies such an inside child). If you want the Trace Message
  668.           // Log in a separate (but owned) window, then pass a FALSE
  669.           // instead for the 3rd argument.
  670.           if (m_pMsgLog->CreateServerLog(m_hInst, m_hWnd, TRUE))
  671.           {
  672.             // Assign the global MsgLog pointer.
  673.             g_pMsgLog = m_pMsgLog;
  674.             // Use macro to log an initial start messsage.
  675.             LOGID(IDS_LOGTO_SERVER);
  676.           }
  677.           else
  678.             bOk = FALSE;
  679.         }
  680.       }
  681.     }
  682.   }
  683.  
  684.   if (!bOk)
  685.   {
  686.     DELETE_POINTER(m_pMsgBox);
  687.     DELETE_POINTER(m_pMsgLog);
  688.   }
  689.  
  690.   return (bOk);
  691. }
  692.  
  693.  
  694. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  695.   Method:   CMainWindow::WindowProc
  696.  
  697.   Summary:  Main window procedure for this window object.  See CVirWindow
  698.             in the APPUTIL library (APPUTIL.CPP) for details on how this
  699.             method gets called by the global WindowProc.
  700.  
  701.   Args:     UINT uMsg,
  702.               Windows message that is "sent" to this window.
  703.             WPARAM wParam,
  704.               First message parameter (word sized).
  705.             LPARAM lParam)
  706.               Second message parameter (long sized).
  707.  
  708.   Modifies: ...
  709.  
  710.   Returns:  LRESULT
  711.               Standard Windows WindowProc return value.
  712. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  713. LRESULT CMainWindow::WindowProc(
  714.           UINT uMsg,
  715.           WPARAM wParam,
  716.           LPARAM lParam)
  717. {
  718.   LRESULT lResult = FALSE;
  719.  
  720.   switch (uMsg)
  721.   {
  722.     case WM_CREATE:
  723.       break;
  724.  
  725.     case WM_COMMAND:
  726.       // Dispatch and handle any Menu command messages received.
  727.       lResult = DoMenu(wParam, lParam);
  728.       break;
  729.  
  730.     case WM_CLOSE:
  731.       // The user selected Close on the main window's System menu
  732.       // or Exit on the File menu.
  733.     case WM_QUIT:
  734.       // If the app is being quit then close any associated help windows.
  735.     default:
  736.       // Defer all messages NOT handled here to the Default Window Proc.
  737.       lResult = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  738.       break;
  739.   }
  740.  
  741.   return(lResult);
  742. }
  743.  
  744.  
  745. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  746.   Function: UnicodeOk
  747.  
  748.   Summary:  Checks if the platform will handle unicode versions of
  749.             Win32 string API calls.
  750.  
  751.   Args:     void
  752.  
  753.   Returns:  BOOL
  754.               TRUE if unicode support; FALSE if not.
  755. ------------------------------------------------------------------------F-F*/
  756. BOOL UnicodeOk(void)
  757. {
  758.   BOOL bOk = TRUE;
  759.   TCHAR szUserName[MAX_STRING_LENGTH];
  760.   DWORD dwSize = MAX_STRING_LENGTH;
  761.  
  762.   if (!GetUserName(szUserName, &dwSize))
  763.     bOk = ERROR_CALL_NOT_IMPLEMENTED == GetLastError() ? FALSE : TRUE;
  764.  
  765.   return bOk;
  766. }
  767.  
  768.  
  769. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  770.   Function: InitApplication
  771.  
  772.   Summary:  Initializes the application and registers its main window
  773.             class. InitApplication is called only once (in WinMain).
  774.  
  775.   Args:     HINSTANCE hInstance)
  776.               Handle to the first instance of the application.
  777.  
  778.   Returns:  BOOL.
  779.               TRUE if success.
  780.               FALSE if fail.
  781. ------------------------------------------------------------------------F-F*/
  782. BOOL InitApplication(
  783.        HINSTANCE hInstance)
  784. {
  785.   BOOL bOk;
  786.   // The window class for all instances of the main frame window.
  787.   WNDCLASSEX wcf;
  788.  
  789.   // Assign the appropriate values for this main frame window class.
  790.   wcf.cbSize        = sizeof(WNDCLASSEX);
  791.   wcf.cbClsExtra    = 0;            // No per-class extra data.
  792.   wcf.cbWndExtra    = 0;            // No per-window extra data.
  793.   wcf.hInstance     = hInstance;    // Application module instance.
  794.   wcf.lpfnWndProc   = &WindowProc;  // Global Window Procedure (defined in
  795.                                     // APPUTIL for all CVirWindows).
  796.   wcf.hCursor       = LoadCursor(NULL, IDC_ARROW); // Load app cursor.
  797.   wcf.hIcon         = (HICON) LoadIcon(            // Load app icon.
  798.                                 hInstance,
  799.                                 TEXT("AppIcon"));
  800.   wcf.hIconSm       = (HICON) LoadImage(           // Load small icon.
  801.                                 hInstance,
  802.                                 TEXT("AppIcon"),
  803.                                 IMAGE_ICON,
  804.                                 16, 16,
  805.                                 0);
  806.   wcf.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);  // Default backgnd color.
  807.   wcf.style         = CS_HREDRAW | CS_VREDRAW;     // Class style(s).
  808.   wcf.lpszClassName = TEXT(MAIN_WINDOW_CLASS_NAME_STR); // Class name.
  809.   wcf.lpszMenuName  = TEXT(MAIN_WINDOW_CLASS_MENU_STR); // Menu name.
  810.  
  811.   // Register the window class and return FALSE if unsuccesful.
  812.   bOk = RegisterClassEx(&wcf);
  813.  
  814.   return (bOk);
  815. }
  816.  
  817.  
  818. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  819.   Function: WinMain
  820.  
  821.   Summary:  The Windows main entry point function for this application.
  822.             Initializes the application, the COM Libraries, and starts
  823.             the main application message loop.
  824.  
  825.   Args:     HINSTANCE hInstance,
  826.               Instance handle; a new one for each invocation of this app.
  827.             HINSTANCE hPrevInstance,
  828.               Instance handle of the previous instance. NULL in Win32.
  829.             LPSTR lpCmdLine,
  830.               Windows passes a pointer to the application's
  831.               invocation command line.
  832.             int nCmdShow)
  833.               Bits telling the show state of the application.
  834.  
  835.   Returns:  int
  836.               msg.wParam (upon exit of message loop).
  837.               FALSE if this instance couldn't initialize and run.
  838. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  839. extern "C" int WINAPI WinMain(
  840.                         HINSTANCE hInstance,
  841.                         HINSTANCE hPrevInstance,
  842.                         LPSTR lpCmdLine,
  843.                         int nCmdShow)
  844. {
  845.   CMainWindow* pWin = NULL;
  846.   int iRun = TRUE;
  847.   MSG msg;
  848.  
  849.   // If we were compiled for UNICODE and the platform seems OK with this
  850.   // then proceed.  Else we error and exit the app.
  851.   if (UnicodeOk())
  852.   {
  853.     // Call to initialize the COM Library.  Use the SUCCEEDED macro
  854.     // to detect success.  If fail then exit app with error message.
  855.     if (SUCCEEDED(CoInitialize(NULL)))
  856.     {
  857.       // If we succeeded in initializing the COM Library we proceed to
  858.       // initialize the application.  If we can't init the application
  859.       // then we signal shut down with an error message exit.
  860.       iRun = InitApplication(hInstance);
  861.       if (iRun)
  862.       {
  863.         // Assume we'll set iRun to TRUE when initialization is done.
  864.         iRun = FALSE;
  865.  
  866.         // We are still go for running so we try to create a nifty new
  867.         // CMainWindow object for this app instance.
  868.         pWin = new CMainWindow;
  869.         if (NULL != pWin)
  870.         {
  871.           // Setup the server control object.
  872.           g_pServer = new CServer;
  873.           if (NULL != g_pServer)
  874.           {
  875.             // Assign the server's instance handle.
  876.             g_pServer->m_hInstServer = hInstance;
  877.  
  878.             // Check command line for switches to register or unregister
  879.             // this server's managed components.   iRun will be set to 2
  880.             // to signal an immediate and quiet exit of this application
  881.             // if such registration or unregistration is requested.
  882.             if (0 == lstrcmpiA(lpCmdLine, "-RegServer")
  883.                 || 0 == lstrcmpiA(lpCmdLine, "/RegServer"))
  884.             {
  885.               if (pWin->RegisterServer())
  886.                 iRun = 2;
  887.             }
  888.             else if (0 == lstrcmpiA(lpCmdLine, "-UnregServer")
  889.                      || 0 == lstrcmpiA(lpCmdLine, "/UnregServer"))
  890.             {
  891.               if (pWin->UnregisterServer())
  892.                 iRun = 2;
  893.             }
  894.  
  895.             if (FALSE == iRun)
  896.             {
  897.               // If we did not process a command line switch that
  898.               // requires immediate exit, then initialize an instance of
  899.               // the new CMainWindow. This entails creating the main window.
  900.               if (pWin->InitInstance(hInstance, nCmdShow))
  901.               {
  902.                 LOGF1("L: CmdLine Switches= %s", lpCmdLine);
  903.                 // Create and register the Class Factories.  But only do so
  904.                 // if this application has been launched by COM as indicated
  905.                 // by the -Embedding command line switch.
  906.                 if (0 == lstrcmpiA(lpCmdLine, "-Embedding")
  907.                     || 0 == lstrcmpiA(lpCmdLine, "/Embedding"))
  908.                   iRun = g_pServer->OpenFactories();
  909.               }
  910.             }
  911.           }
  912.         }
  913.       }
  914.  
  915.       switch (iRun)
  916.       {
  917.         case TRUE:
  918.           {
  919.             // If we initialized the app instance properly then we are still
  920.             // go for running.  We then start up the main message pump for
  921.             // the application.  The application will live hidden as a local
  922.             // server.
  923.             while (GetMessage(&msg, NULL, 0, 0))
  924.             {
  925.               TranslateMessage(&msg);
  926.               DispatchMessage(&msg);
  927.             }
  928.  
  929.             // We'll pass to the OS the reason why we exited the message
  930.             // loop.
  931.             iRun = msg.wParam;
  932.           }
  933.           break;
  934.  
  935.         case FALSE:
  936.           // We failed to initialize the application--issue an error
  937.           // messagebox.  Can't initialize or can't run stand-alone.
  938.           ErrorBox(hInstance, NULL, IDS_APPINITFAILED);
  939.  
  940.           // Delete the CMainWindow object.
  941.           DELETE_POINTER(pWin);
  942.  
  943.           // Pass the OS an error code of 1.
  944.           iRun = 1;
  945.           break;
  946.  
  947.         default:
  948.           // Pass the OS an error code of 0.
  949.           iRun = 0;
  950.           break;
  951.       }
  952.  
  953.       // We're exiting this app (either normally or by init failure) so
  954.       // shut down the COM Library.
  955.       CoUninitialize();
  956.     }
  957.     else
  958.     {
  959.       // We failed to Initialize the COM Library. Put up error message box
  960.       // saying that COM Library couldn't be initialized.  Parent window
  961.       // is desktop (ie, NULL). Exit the failed application (ie, by
  962.       // returning FALSE to WinMain).
  963.       ErrorBox(hInstance, NULL, IDS_COMINITFAILED);
  964.     }
  965.   }
  966.   else
  967.   {
  968.     // If we were compiled for UNICODE but the platform has problems with
  969.     // this then indicate an error and exit the app immediately.
  970.     CHAR szMsg[MAX_STRING_LENGTH];
  971.  
  972.     if (LoadStringA(
  973.           hInstance,
  974.           IDS_NOUNICODE,
  975.           szMsg,
  976.           MAX_STRING_LENGTH))
  977.     {
  978.       MessageBoxA(
  979.         NULL,
  980.         szMsg,
  981.         ERROR_TITLE_STR,
  982.         MB_OK | MB_ICONEXCLAMATION);
  983.     }
  984.   }
  985.  
  986.   return iRun;
  987. }
  988.