home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / freethrd / server / freserve.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  15.8 KB  |  511 lines

  1. /*+==========================================================================
  2.   File:      FRESERVE.CPP
  3.  
  4.   Summary:   Implementation file for a DLL COM Component server providing
  5.              a Ball-related COM Component: Ball. Access to Class Factories
  6.              is provided in this module.  This server also supports self
  7.              registration and unregistration. FRESERVE is a thread-safe
  8.              server for the COBall COM objects.
  9.  
  10.              For a comprehensive tutorial code tour of FRESERVE's contents
  11.              and offerings see the accompanying FRESERVE.TXT file. For
  12.              more specific technical details on the internal workings see
  13.              the comments dispersed throughout the FRESERVE source code.
  14.              FRESERVE is functioned by the multi-threaded server, FRECLIEN.
  15.              For more details see FRECLIEN.TXT in the sibling FRECLIEN
  16.              directory.
  17.  
  18.   Classes:   none.
  19.  
  20.   Functions: DllMain, DllGetClassObject, DllCanUnloadNow, DllRegisterServer,
  21.              DllUnregisterServer.
  22.  
  23.   Origin:    4-6-96: atrent - Editor-inheritance from DLLSERVE.CPP in
  24.                the DLLSERVE OLE Tutorial Code Sample.
  25.  
  26. ----------------------------------------------------------------------------
  27.   This file is part of the Microsoft OLE Tutorial Code Samples.
  28.  
  29.   Copyright (C) Microsoft Corporation, 1996.  All rights reserved.
  30.  
  31.   This source code is intended only as a supplement to Microsoft
  32.   Development Tools and/or on-line documentation.  See these other
  33.   materials for detailed information regarding Microsoft code samples.
  34.  
  35.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  36.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  37.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  38.   PARTICULAR PURPOSE.
  39. ==========================================================================+*/
  40.  
  41. /*---------------------------------------------------------------------------
  42.   We include WINDOWS.H for all Win32 applications.
  43.   We include OLE2.H because we will be making calls to the OLE Libraries
  44.     in future exploitation of this DLL skeleton.
  45.   We include INITGUID.H only once (here) in the entire DLL because we
  46.     will be defining GUIDs and want them as constants in the data segment.
  47.   We include APPUTIL.H because we will be building this DLL using
  48.     the convenient Virtual Window and Dialog classes and other
  49.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  50.   We include IBALL.H and BALLGUID.H for the common ball-related Interface
  51.     class, GUID, and CLSID specifications.
  52.   We include FRESERVE.H because it has the main declerations of all the
  53.     car related interfaces and their GUIDs.  It also has the _DLLEXPORT_
  54.     controlled import and export specifications.
  55.   We include SERVER.H because it has the necessary internal class and
  56.     resource definitions for this DLL.
  57.   We include FACTORY.H because it has the necessary internal class factory
  58.     declarations for this DLL component server.
  59. ---------------------------------------------------------------------------*/
  60.  
  61. #include "preserve.h"
  62. #include "factory.h"
  63.  
  64. // Global variable definitions. Some Initialized in DllMain() below.
  65.  
  66. // We encapsulate the control of this COM server (eg, lock and object
  67. // counting) in a server control C++ object.  Here is it's pointer.
  68. CServer* g_pServer = NULL;
  69.  
  70.  
  71. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  72.   Function: UnicodeOk
  73.  
  74.   Summary:  Checks if the platform will handle unicode versions of
  75.             Win32 string API calls.
  76.  
  77.   Args:     void
  78.  
  79.   Returns:  BOOL
  80.               TRUE if unicode support; FALSE if not.
  81. ------------------------------------------------------------------------F-F*/
  82. BOOL UnicodeOk(void)
  83. {
  84.   BOOL bOk = TRUE;
  85.   TCHAR szUserName[MAX_STRING_LENGTH];
  86.   DWORD dwSize = MAX_STRING_LENGTH;
  87.  
  88.   if (!GetUserName(szUserName, &dwSize))
  89.     bOk = ERROR_CALL_NOT_IMPLEMENTED == GetLastError() ? FALSE : TRUE;
  90.  
  91.   return bOk;
  92. }
  93.  
  94.  
  95. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  96.   Function: DllMain
  97.  
  98.   Summary:  Like WinMain is for an EXE application, this DllMain function
  99.             is the main entry point for this DLL.  It is called when the
  100.             DLL is loaded by a process, and when new threads are created
  101.             by a process that has already loaded this DLL.  DllMain is
  102.             also called when threads of a process that has loaded the DLL
  103.             exit cleanly and when the process itself unloads the DLL.
  104.  
  105.             If you want to use C runtime libraries, keep this function
  106.             named "DllMain" and you won't have to do anything special to
  107.             initialize the runtime libraries.
  108.  
  109.             When fdwReason == DLL_PROCESS_ATTACH, the return value is used
  110.             to determine if the DLL should remain loaded, or should be
  111.             immediately unloaded depending upon whether the DLL could be
  112.             initialized properly.  For all other values of fdwReason, the
  113.             return value is ignored.
  114.  
  115.   Args:     HINSTANCE hDLLInst,
  116.               Instance handle of the DLL.
  117.             DWORD fdwReason,
  118.               Process attach/detach or thread attach/detach.
  119.               Reason for calling.
  120.             LPVOID lpvReserved)
  121.               Reserved and not used.
  122.  
  123.   Returns:  BOOL,
  124.               Return value is used only when fdwReason == DLL_PROCESS_ATTACH.
  125.               TRUE  -  Used to signify that the DLL should remain loaded.
  126.               FALSE -  Used to signify that the DLL should be
  127.                 immediately unloaded.
  128. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  129. BOOL WINAPI DllMain(
  130.               HINSTANCE hDllInst,
  131.               DWORD fdwReason,
  132.               LPVOID lpvReserved)
  133. {
  134.   BOOL bResult = TRUE;
  135.  
  136.   // Dispatch this main call based on the reason it was called.
  137.   switch (fdwReason)
  138.   {
  139.     case DLL_PROCESS_ATTACH:
  140.       // The DLL is being loaded for the first time by a given process.
  141.       // Perform per-process initialization here.  If the initialization
  142.       // is successful, return TRUE; if unsuccessful, return FALSE.
  143.       bResult = FALSE;
  144.       if (UnicodeOk())
  145.       {
  146.         // Instantiate the CServer utility class.
  147.         g_pServer = new CServer;
  148.         if (NULL != g_pServer)
  149.         {
  150.           // Remember the DLL Instance handle.
  151.           g_pServer->m_hDllInst = hDllInst;
  152.           bResult = TRUE;
  153.         }
  154.       }
  155.       break;
  156.  
  157.     case DLL_PROCESS_DETACH:
  158.       // The DLL is being unloaded by a given process.  Do any
  159.       // per-process clean up here, such as undoing what was done in
  160.       // DLL_PROCESS_ATTACH.  The return value is ignored.
  161.       DELETE_POINTER(g_pServer);
  162.       break;
  163.  
  164.     case DLL_THREAD_ATTACH:
  165.       // A thread is being created in a process that has already loaded
  166.       // this DLL.  Perform any per-thread initialization here.  The
  167.       // return value is ignored.
  168.       break;
  169.  
  170.     case DLL_THREAD_DETACH:
  171.       // A thread is exiting cleanly in a process that has already
  172.       // loaded this DLL.  Perform any per-thread clean up here.  The
  173.       // return value is ignored.
  174.       break;
  175.  
  176.     default:
  177.       break;
  178.   }
  179.  
  180.   return (bResult);
  181. }
  182.  
  183.  
  184. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  185.   Function: DllGetClassObject
  186.  
  187.   Summary:  The standard exported function that the COM service library
  188.             uses to obtain an object class of the class factory for a
  189.             specified component provided by this server DLL.
  190.  
  191.   Args:     REFCLSID rclsid,
  192.               [in] The CLSID of the requested Component.
  193.             REFIID riid,
  194.               [in] The requested interface on the Class Factory.
  195.             PPVOID ppv)
  196.               [in/out] The output interface pointer to the Class Factory.
  197.  
  198.   Returns:  HRESULT
  199.               E_FAIL if requested component isn't supported.
  200.               E_OUTOFMEMORY if out of memory.
  201.               Error code out of the QueryInterface.
  202. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  203. STDAPI DllGetClassObject(
  204.          REFCLSID rclsid,
  205.          REFIID riid,
  206.          PPVOID ppv)
  207. {
  208.   HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
  209.   IUnknown* pCob = NULL;
  210.  
  211.   if (CLSID_Ball == rclsid)
  212.   {
  213.     hr = E_OUTOFMEMORY;
  214.     pCob = new CFBall(NULL, g_pServer);
  215.   }
  216.  
  217.   if (NULL != pCob)
  218.   {
  219.     g_pServer->ObjectsUp();
  220.     hr = pCob->QueryInterface(riid, ppv);
  221.     if (FAILED(hr))
  222.     {
  223.       g_pServer->ObjectsDown();
  224.       DELETE_POINTER(pCob);
  225.     }
  226.   }
  227.  
  228.   return hr;
  229. }
  230.  
  231.  
  232. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  233.   Function: DllCanUnloadNow
  234.  
  235.   Summary:  The standard exported function that the COM service library
  236.             uses to determine if this server DLL can be unloaded.
  237.  
  238.   Args:     void.
  239.  
  240.   Returns:  HRESULT
  241.               S_OK if this DLL server can be unloaded.
  242.               S_FALSE if this DLL can not be unloaded.
  243. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  244. STDAPI DllCanUnloadNow(void)
  245. {
  246.   HRESULT hr;
  247.  
  248.   // We return S_OK of there are no longer any living objects AND
  249.   // there are no outstanding client locks on this server.
  250.   hr = g_pServer->CanUnloadNow();
  251.  
  252.   return hr;
  253. }
  254.  
  255.  
  256. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  257.   Function: SetRegKeyValue
  258.  
  259.   Summary:  Internal utility function to set a Key, Subkey, and value
  260.             in the system Registry under HKEY_CLASSES_ROOT.
  261.  
  262.   Args:     LPTSTR pszKey,
  263.             LPTSTR pszSubkey,
  264.             LPTSTR pszValue)
  265.  
  266.   Returns:  BOOL
  267.               TRUE if success; FALSE if not.
  268. ------------------------------------------------------------------------F-F*/
  269. BOOL SetRegKeyValue(
  270.        LPTSTR pszKey,
  271.        LPTSTR pszSubkey,
  272.        LPTSTR pszValue)
  273. {
  274.   BOOL bOk = FALSE;
  275.   LONG ec;
  276.   HKEY hKey;
  277.   TCHAR szKey[MAX_STRING_LENGTH];
  278.  
  279.   lstrcpy(szKey, pszKey);
  280.  
  281.   if (NULL != pszSubkey)
  282.   {
  283.     lstrcat(szKey, TEXT("\\"));
  284.     lstrcat(szKey, pszSubkey);
  285.   }
  286.  
  287.   ec = RegCreateKeyEx(
  288.          HKEY_CLASSES_ROOT,
  289.          szKey,
  290.          0,
  291.          NULL,
  292.          REG_OPTION_NON_VOLATILE,
  293.          KEY_ALL_ACCESS,
  294.          NULL,
  295.          &hKey,
  296.          NULL);
  297.  
  298.   if (NULL != pszValue && ERROR_SUCCESS == ec)
  299.   {
  300.     ec = RegSetValueEx(
  301.            hKey,
  302.            NULL,
  303.            0,
  304.            REG_SZ,
  305.            (BYTE *)pszValue,
  306.            (lstrlen(pszValue)+1)*sizeof(TCHAR));
  307.     if (ERROR_SUCCESS == ec)
  308.       bOk = TRUE;
  309.     RegCloseKey(hKey);
  310.   }
  311.  
  312.   return bOk;
  313. }
  314.  
  315.  
  316. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  317.   Function: AddRegNamedValue
  318.  
  319.   Summary:  Internal utility function to add a named data value to an
  320.             existing Key (with optional Subkey) in the system Registry
  321.             under HKEY_CLASSES_ROOT.
  322.  
  323.   Args:     LPTSTR pszKey,
  324.             LPTSTR pszSubkey,
  325.             LPTSTR pszValueName,
  326.             LPTSTR pszValue)
  327.  
  328.   Returns:  BOOL
  329.               TRUE if success; FALSE if not.
  330. ------------------------------------------------------------------------F-F*/
  331. BOOL AddRegNamedValue(
  332.        LPTSTR pszKey,
  333.        LPTSTR pszSubkey,
  334.        LPTSTR pszValueName,
  335.        LPTSTR pszValue)
  336. {
  337.   BOOL bOk = FALSE;
  338.   LONG ec;
  339.   HKEY hKey;
  340.   TCHAR szKey[MAX_STRING_LENGTH];
  341.  
  342.   lstrcpy(szKey, pszKey);
  343.  
  344.   if (NULL != pszSubkey)
  345.   {
  346.     lstrcat(szKey, TEXT("\\"));
  347.     lstrcat(szKey, pszSubkey);
  348.   }
  349.  
  350.   ec = RegOpenKeyEx(
  351.          HKEY_CLASSES_ROOT,
  352.          szKey,
  353.          0,
  354.          KEY_ALL_ACCESS,
  355.          &hKey);
  356.  
  357.   if (NULL != pszValue && ERROR_SUCCESS == ec)
  358.   {
  359.     ec = RegSetValueEx(
  360.            hKey,
  361.            pszValueName,
  362.            0,
  363.            REG_SZ,
  364.            (BYTE *)pszValue,
  365.            (lstrlen(pszValue)+1)*sizeof(TCHAR));
  366.     if (ERROR_SUCCESS == ec)
  367.       bOk = TRUE;
  368.     RegCloseKey(hKey);
  369.   }
  370.  
  371.   return bOk;
  372. }
  373.  
  374.  
  375. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  376.   Function: DllRegisterServer
  377.  
  378.   Summary:  The standard exported function that can be called to command
  379.             this DLL server to register itself in the system registry.
  380.  
  381.   Args:     void.
  382.  
  383.   Returns:  HRESULT
  384.               NOERROR
  385. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  386. STDAPI DllRegisterServer(void)
  387. {
  388.   HRESULT  hr = NOERROR;
  389.   TCHAR    szID[GUID_SIZE+1];
  390.   TCHAR    szCLSID[GUID_SIZE+1];
  391.   TCHAR    szModulePath[MAX_PATH];
  392.  
  393.   // Obtain the path to this module's executable file for later use.
  394.   GetModuleFileName(
  395.     g_pServer->m_hDllInst,
  396.     szModulePath,
  397.     sizeof(szModulePath)/sizeof(TCHAR));
  398.  
  399.   /*-------------------------------------------------------------------------
  400.     Create registry entries for the DllBall Component.
  401.   -------------------------------------------------------------------------*/
  402.   // Create some base key strings.
  403.   StringFromGUID2(CLSID_Ball, szID, GUID_SIZE);
  404.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  405.   lstrcat(szCLSID, szID);
  406.  
  407.   // Create ProgID keys.
  408.   SetRegKeyValue(
  409.     TEXT("DllBall1.0"),
  410.     NULL,
  411.     TEXT("DllBall Component - FRESERVE Code Sample"));
  412.   SetRegKeyValue(
  413.     TEXT("DllBall1.0"),
  414.     TEXT("CLSID"),
  415.     szID);
  416.  
  417.   // Create VersionIndependentProgID keys.
  418.   SetRegKeyValue(
  419.     TEXT("DllBall"),
  420.     NULL,
  421.     TEXT("DllBall Component - FRESERVE Code Sample"));
  422.   SetRegKeyValue(
  423.     TEXT("DllBall"),
  424.     TEXT("CurVer"),
  425.     TEXT("DllBall1.0"));
  426.   SetRegKeyValue(
  427.     TEXT("DllBall"),
  428.     TEXT("CLSID"),
  429.     szID);
  430.  
  431.   // Create entries under CLSID.
  432.   SetRegKeyValue(
  433.     szCLSID,
  434.     NULL,
  435.     TEXT("DllBall Component - FRESERVE Code Sample"));
  436.   SetRegKeyValue(
  437.     szCLSID,
  438.     TEXT("ProgID"),
  439.     TEXT("DllBall1.0"));
  440.   SetRegKeyValue(
  441.     szCLSID,
  442.     TEXT("VersionIndependentProgID"),
  443.     TEXT("DllBall"));
  444.   SetRegKeyValue(
  445.     szCLSID,
  446.     TEXT("NotInsertable"),
  447.     NULL);
  448.   SetRegKeyValue(
  449.     szCLSID,
  450.     TEXT("InprocServer32"),
  451.     szModulePath);
  452.   AddRegNamedValue(
  453.     szCLSID,
  454.     TEXT("InprocServer32"),
  455.     TEXT("ThreadingModel"),
  456.     TEXT("Free"));
  457.  
  458.   return hr;
  459. }
  460.  
  461.  
  462. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  463.   Function: DllUnregisterServer
  464.  
  465.   Summary:  The standard exported function that can be called to command
  466.             this DLL server to unregister itself from the system Registry.
  467.  
  468.   Args:     void.
  469.  
  470.   Returns:  HRESULT
  471.               NOERROR
  472. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  473. STDAPI DllUnregisterServer(void)
  474. {
  475.   HRESULT  hr = NOERROR;
  476.   TCHAR    szID[GUID_SIZE+1];
  477.   TCHAR    szCLSID[GUID_SIZE+1];
  478.   TCHAR    szTemp[GUID_SIZE+1];
  479.  
  480.   /*-------------------------------------------------------------------------
  481.     Delete registry entries for the Ball Component.
  482.   -------------------------------------------------------------------------*/
  483.   //Create some base key strings.
  484.   StringFromGUID2(CLSID_Ball, szID, GUID_SIZE);
  485.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  486.   lstrcat(szCLSID, szID);
  487.  
  488.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("DllBall\\CurVer"));
  489.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("DllBall\\CLSID"));
  490.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("DllBall"));
  491.  
  492.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("DllBall1.0\\CLSID"));
  493.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("DllBall1.0"));
  494.  
  495.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("ProgID"));
  496.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  497.  
  498.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("VersionIndependentProgID"));
  499.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  500.  
  501.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("NotInsertable"));
  502.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  503.  
  504.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("InprocServer32"));
  505.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  506.  
  507.   RegDeleteKey(HKEY_CLASSES_ROOT, szCLSID);
  508.  
  509.   return hr;
  510. }
  511.