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 / conserve / conserve.cpp < prev    next >
C/C++ Source or Header  |  1997-08-30  |  16KB  |  520 lines

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