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

  1. /*+==========================================================================
  2.   File:      PERDRAW.CPP
  3.  
  4.   Summary:   Main implementation file for a DLL COM Component server
  5.              providing the DrawPage COM Component. Access to Class
  6.              Factories is provided in this module.  This module also
  7.              supports self registration and unregistration for the
  8.              components housed in the server.
  9.  
  10.              For a comprehensive tutorial code tour of PERDRAW's contents
  11.              and offerings see the tutorial PERDRAW.HTM file. For more
  12.              specific technical details on the internal workings see the
  13.              comments dispersed throughout the PERDRAW source code. For
  14.              more details on how the PERDRAW server works with the
  15.              PERCLIEN client see PERCLIEN.HTM in the main tutorial
  16.              directory.
  17.  
  18.   Classes:   none.
  19.  
  20.   Functions: DllMain, DllGetClassObject, DllCanUnloadNow, DllRegisterServer,
  21.              DllUnregisterServer.
  22.  
  23.   Origin:    5-20-97: atrent - Editor-inheritance from STOSERVE.CPP in
  24.                the STOSERVE Tutorial Code Sample.
  25.  
  26. ----------------------------------------------------------------------------
  27.   This file is part of the Microsoft COM 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 IPAGES.H and PAGEGUID.H for the common DrawPage-related
  50.     Interface class, GUID, and CLSID specifications.
  51.   We include PERDRAW.H because it has the _DLLEXPORT_ controlled import
  52.     and export specifications.
  53.   We include SERVER.H because it has internal class declarations for
  54.     the server's control object.
  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 <ipages.h>
  63. #include <pageguid.h>
  64. #define _DLLEXPORT_
  65. #include "perdraw.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 global 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:  Just as WinMain is for an EXE application, this DllMain
  105.             function is the main entry point for this DLL.  It is called
  106.             when the DLL is loaded by a process, and when new threads are
  107.             created by a process that has already loaded this DLL.
  108.             DllMain is also called when threads of a process that has
  109.             loaded the DLL exit cleanly and when the process itself
  110.             unloads the DLL.
  111.  
  112.             If you want to use C runtime libraries, keep this function
  113.             named "DllMain" and you won't have to do anything special to
  114.             initialize the runtime libraries.
  115.  
  116.             When fdwReason == DLL_PROCESS_ATTACH, the return value is used
  117.             to determine if the DLL should remain loaded, or should be
  118.             immediately unloaded depending upon whether the DLL could be
  119.             initialized properly.  For all other values of fdwReason, the
  120.             return value is ignored.
  121.  
  122.   Args:     HINSTANCE hDLLInst,
  123.               Instance handle of the DLL.
  124.             DWORD fdwReason,
  125.               Process attach/detach or thread attach/detach.
  126.               Reason for calling.
  127.             LPVOID lpvReserved)
  128.               Reserved and not used.
  129.  
  130.   Returns:  BOOL,
  131.               Return value is used only when fdwReason == DLL_PROCESS_ATTACH.
  132.               TRUE  -  Used to signify that the DLL should remain loaded.
  133.               FALSE -  Used to signify that the DLL should be
  134.                 immediately unloaded.
  135. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  136. BOOL WINAPI DllMain(
  137.               HINSTANCE hDllInst,
  138.               DWORD fdwReason,
  139.               LPVOID lpvReserved)
  140. {
  141.   BOOL bResult = TRUE;
  142.  
  143.   // Dispatch this main call based on the reason it was called.
  144.   switch (fdwReason)
  145.   {
  146.     case DLL_PROCESS_ATTACH:
  147.       // The DLL is being loaded for the first time by a given process.
  148.       // Perform per-process initialization here.  If the initialization
  149.       // is successful, return TRUE; if unsuccessful, return FALSE.
  150.       bResult = FALSE;
  151.       if (UnicodeOk())
  152.       {
  153.         // Instantiate the CServer utility class.
  154.         g_pServer = new CServer;
  155.         if (NULL != g_pServer)
  156.         {
  157.           // Remember the DLL Instance handle.
  158.           g_pServer->m_hDllInst = hDllInst;
  159.           bResult = TRUE;
  160.         }
  161.       }
  162.       break;
  163.  
  164.     case DLL_PROCESS_DETACH:
  165.       // The DLL is being unloaded by a given process.  Do any
  166.       // per-process clean up here, such as undoing what was done in
  167.       // DLL_PROCESS_ATTACH.  The return value is ignored.
  168.       DELETE_POINTER(g_pServer);
  169.       break;
  170.  
  171.     case DLL_THREAD_ATTACH:
  172.       // A thread is being created in a process that has already loaded
  173.       // this DLL.  Perform any per-thread initialization here.  The
  174.       // return value is ignored.
  175.       break;
  176.  
  177.     case DLL_THREAD_DETACH:
  178.       // A thread is exiting cleanly in a process that has already
  179.       // loaded this DLL.  Perform any per-thread clean up here.  The
  180.       // return value is ignored.
  181.       break;
  182.  
  183.     default:
  184.       break;
  185.   }
  186.  
  187.   return (bResult);
  188. }
  189.  
  190.  
  191. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  192.   Function: DllGetClassObject
  193.  
  194.   Summary:  The standard exported function that the COM service library
  195.             uses to obtain an object class of the class factory for a
  196.             specified component provided by this server DLL.
  197.  
  198.   Args:     REFCLSID rclsid,
  199.               [in] The CLSID of the requested Component.
  200.             REFIID riid,
  201.               [in] GUID of the requested interface on the Class Factory.
  202.             PPVOID ppv)
  203.               [out] Address of the caller's pointer variable that will
  204.               receive the requested interface pointer.
  205.  
  206.   Returns:  HRESULT
  207.               E_FAIL if requested component isn't supported.
  208.               E_OUTOFMEMORY if out of memory.
  209.               Error code out of the QueryInterface.
  210. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  211. STDAPI DllGetClassObject(
  212.          REFCLSID rclsid,
  213.          REFIID riid,
  214.          PPVOID ppv)
  215. {
  216.   HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
  217.   IUnknown* pCO = NULL;
  218.  
  219.   if (CLSID_DrawPage == rclsid)
  220.   {
  221.     hr = E_OUTOFMEMORY;
  222.     pCO = new CFDrawPage(NULL, g_pServer);
  223.   }
  224.  
  225.   if (NULL != pCO)
  226.   {
  227.     g_pServer->ObjectsUp();
  228.     hr = pCO->QueryInterface(riid, ppv);
  229.     if (FAILED(hr))
  230.     {
  231.       g_pServer->ObjectsDown();
  232.       DELETE_POINTER(pCO);
  233.     }
  234.   }
  235.  
  236.   return hr;
  237. }
  238.  
  239.  
  240. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  241.   Function: DllCanUnloadNow
  242.  
  243.   Summary:  The standard exported function that the COM service library
  244.             uses to determine if this server DLL can be unloaded.
  245.  
  246.   Args:     void.
  247.  
  248.   Returns:  HRESULT
  249.               S_OK if this DLL server can be unloaded.
  250.               S_FALSE if this DLL can not be unloaded.
  251. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  252. STDAPI DllCanUnloadNow(void)
  253. {
  254.   HRESULT hr;
  255.  
  256.   // We return S_OK if there are no longer any living objects AND
  257.   // there are no outstanding client locks on this server.
  258.   hr = g_pServer->CanUnloadNow();
  259.  
  260.   return hr;
  261. }
  262.  
  263.  
  264. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  265.   Function: SetRegKeyValue
  266.  
  267.   Summary:  Internal utility function to set a Key, Subkey, and value
  268.             in the system Registry under HKEY_CLASSES_ROOT.
  269.  
  270.   Args:     LPTSTR pszKey,
  271.             LPTSTR pszSubkey,
  272.             LPTSTR pszValue)
  273.  
  274.   Returns:  BOOL
  275.               TRUE if success; FALSE if not.
  276. ------------------------------------------------------------------------F-F*/
  277. BOOL SetRegKeyValue(
  278.        LPTSTR pszKey,
  279.        LPTSTR pszSubkey,
  280.        LPTSTR pszValue)
  281. {
  282.   BOOL bOk = FALSE;
  283.   LONG ec;
  284.   HKEY hKey;
  285.   TCHAR szKey[MAX_STRING_LENGTH];
  286.  
  287.   lstrcpy(szKey, pszKey);
  288.  
  289.   if (NULL != pszSubkey)
  290.   {
  291.     lstrcat(szKey, TEXT("\\"));
  292.     lstrcat(szKey, pszSubkey);
  293.   }
  294.  
  295.   ec = RegCreateKeyEx(
  296.          HKEY_CLASSES_ROOT,
  297.          szKey,
  298.          0,
  299.          NULL,
  300.          REG_OPTION_NON_VOLATILE,
  301.          KEY_ALL_ACCESS,
  302.          NULL,
  303.          &hKey,
  304.          NULL);
  305.  
  306.   if (ERROR_SUCCESS == ec)
  307.   {
  308.     if (NULL != pszValue)
  309.     {
  310.       ec = RegSetValueEx(
  311.              hKey,
  312.              NULL,
  313.              0,
  314.              REG_SZ,
  315.              (BYTE *)pszValue,
  316.              (lstrlen(pszValue)+1)*sizeof(TCHAR));
  317.     }
  318.     if (ERROR_SUCCESS == ec)
  319.       bOk = TRUE;
  320.     RegCloseKey(hKey);
  321.   }
  322.  
  323.   return bOk;
  324. }
  325.  
  326.  
  327. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  328.   Function: AddRegNamedValue
  329.  
  330.   Summary:  Internal utility function to add a named data value to an
  331.             existing Key (with optional Subkey) in the system Registry
  332.             under HKEY_CLASSES_ROOT.
  333.  
  334.   Args:     LPTSTR pszKey,
  335.             LPTSTR pszSubkey,
  336.             LPTSTR pszValueName,
  337.             LPTSTR pszValue)
  338.  
  339.   Returns:  BOOL
  340.               TRUE if success; FALSE if not.
  341. ------------------------------------------------------------------------F-F*/
  342. BOOL AddRegNamedValue(
  343.        LPTSTR pszKey,
  344.        LPTSTR pszSubkey,
  345.        LPTSTR pszValueName,
  346.        LPTSTR pszValue)
  347. {
  348.   BOOL bOk = FALSE;
  349.   LONG ec;
  350.   HKEY hKey;
  351.   TCHAR szKey[MAX_STRING_LENGTH];
  352.  
  353.   lstrcpy(szKey, pszKey);
  354.  
  355.   if (NULL != pszSubkey)
  356.   {
  357.     lstrcat(szKey, TEXT("\\"));
  358.     lstrcat(szKey, pszSubkey);
  359.   }
  360.  
  361.   ec = RegOpenKeyEx(
  362.          HKEY_CLASSES_ROOT,
  363.          szKey,
  364.          0,
  365.          KEY_ALL_ACCESS,
  366.          &hKey);
  367.  
  368.   if (NULL != pszValue && ERROR_SUCCESS == ec)
  369.   {
  370.     ec = RegSetValueEx(
  371.            hKey,
  372.            pszValueName,
  373.            0,
  374.            REG_SZ,
  375.            (BYTE *)pszValue,
  376.            (lstrlen(pszValue)+1)*sizeof(TCHAR));
  377.     if (ERROR_SUCCESS == ec)
  378.       bOk = TRUE;
  379.     RegCloseKey(hKey);
  380.   }
  381.  
  382.   return bOk;
  383. }
  384.  
  385.  
  386. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  387.   Function: DllRegisterServer
  388.  
  389.   Summary:  The standard exported function that can be called to command
  390.             this DLL server to register itself in the system registry.
  391.  
  392.   Args:     void.
  393.  
  394.   Returns:  HRESULT
  395.               NOERROR
  396. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  397. STDAPI DllRegisterServer(void)
  398. {
  399.   HRESULT  hr = NOERROR;
  400.   TCHAR    szID[GUID_SIZE+1];
  401.   TCHAR    szCLSID[GUID_SIZE+32];
  402.   TCHAR    szModulePath[MAX_PATH];
  403.  
  404.   // Obtain the path to this module's executable file for later use.
  405.   GetModuleFileName(
  406.     g_pServer->m_hDllInst,
  407.     szModulePath,
  408.     sizeof(szModulePath)/sizeof(TCHAR));
  409.  
  410.   /*-------------------------------------------------------------------------
  411.     Create registry entries for the DrawPage Component.
  412.   -------------------------------------------------------------------------*/
  413.   // Create some base key strings.
  414.   StringFromGUID2(CLSID_DrawPage, szID, GUID_SIZE);
  415.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  416.   lstrcat(szCLSID, szID);
  417.  
  418.   // Create ProgID keys.
  419.   SetRegKeyValue(
  420.     TEXT("CTS.DrawPage.1"),
  421.     NULL,
  422.     TEXT("DrawPage Component - PERDRAW Code Sample"));
  423.   SetRegKeyValue(
  424.     TEXT("CTS.DrawPage.1"),
  425.     TEXT("CLSID"),
  426.     szID);
  427.  
  428.   // Create VersionIndependentProgID keys.
  429.   SetRegKeyValue(
  430.     TEXT("CTS.DrawPage"),
  431.     NULL,
  432.     TEXT("DrawPage Component - PERDRAW Code Sample"));
  433.   SetRegKeyValue(
  434.     TEXT("CTS.DrawPage"),
  435.     TEXT("CurVer"),
  436.     TEXT("CTS.DrawPage.1"));
  437.   SetRegKeyValue(
  438.     TEXT("CTS.DrawPage"),
  439.     TEXT("CLSID"),
  440.     szID);
  441.  
  442.   // Create entries under CLSID.
  443.   SetRegKeyValue(
  444.     szCLSID,
  445.     NULL,
  446.     TEXT("DrawPage Component - PERDRAW Code Sample"));
  447.   SetRegKeyValue(
  448.     szCLSID,
  449.     TEXT("ProgID"),
  450.     TEXT("CTS.DrawPage.1"));
  451.   SetRegKeyValue(
  452.     szCLSID,
  453.     TEXT("VersionIndependentProgID"),
  454.     TEXT("CTS.DrawPage"));
  455.   SetRegKeyValue(
  456.     szCLSID,
  457.     TEXT("NotInsertable"),
  458.     NULL);
  459.   SetRegKeyValue(
  460.     szCLSID,
  461.     TEXT("InprocServer32"),
  462.     szModulePath);
  463.   AddRegNamedValue(
  464.     szCLSID,
  465.     TEXT("InprocServer32"),
  466.     TEXT("ThreadingModel"),
  467.     TEXT("Apartment"));
  468.  
  469.   return hr;
  470. }
  471.  
  472.  
  473. /*F+F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F+++F
  474.   Function: DllUnregisterServer
  475.  
  476.   Summary:  The standard exported function that can be called to command
  477.             this DLL server to unregister itself from the system Registry.
  478.  
  479.   Args:     void.
  480.  
  481.   Returns:  HRESULT
  482.               NOERROR
  483. F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F---F-F*/
  484. STDAPI DllUnregisterServer(void)
  485. {
  486.   HRESULT  hr = NOERROR;
  487.   TCHAR    szID[GUID_SIZE+1];
  488.   TCHAR    szCLSID[GUID_SIZE+32];
  489.   TCHAR    szTemp[MAX_PATH+GUID_SIZE];
  490.  
  491.   /*-------------------------------------------------------------------------
  492.     Delete registry entries for the DrawPage Component.
  493.   -------------------------------------------------------------------------*/
  494.   //Create some base key strings.
  495.   StringFromGUID2(CLSID_DrawPage, szID, GUID_SIZE);
  496.   lstrcpy(szCLSID, TEXT("CLSID\\"));
  497.   lstrcat(szCLSID, szID);
  498.  
  499.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.DrawPage\\CurVer"));
  500.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.DrawPage\\CLSID"));
  501.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.DrawPage"));
  502.  
  503.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.DrawPage.1\\CLSID"));
  504.   RegDeleteKey(HKEY_CLASSES_ROOT, TEXT("CTS.DrawPage.1"));
  505.  
  506.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("ProgID"));
  507.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  508.  
  509.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("VersionIndependentProgID"));
  510.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  511.  
  512.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("NotInsertable"));
  513.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  514.  
  515.   wsprintf(szTemp, TEXT("%s\\%s"), szCLSID, TEXT("InprocServer32"));
  516.   RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  517.  
  518.   RegDeleteKey(HKEY_CLASSES_ROOT, szCLSID);
  519.  
  520.   return hr;
  521. }
  522.