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