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 / licserve / server.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-05  |  7.0 KB  |  248 lines

  1. /*+==========================================================================
  2.   File:      SERVER.CPP
  3.  
  4.   Summary:   Implementation file for the CServer server-related utility
  5.              C++ object.  This object encapsulates the server's internal
  6.              control of global server object and lock counts and checks
  7.              for the global machine license for this server.
  8.  
  9.              For a comprehensive tutorial code tour of this module's
  10.              contents and offerings see the tutorial LICSERVE.HTM
  11.              file. For more specific technical details on the internal
  12.              workings see the comments dispersed throughout the module's
  13.              source code.
  14.  
  15.   Classes:   CServer.
  16.  
  17.   Functions:
  18.  
  19.   Origin:    10-5-95: atrent - Editor-inheritance from SERVER.CPP in
  20.                the DLLSERVE Tutorial Code Sample.
  21.  
  22. ----------------------------------------------------------------------------
  23.   This file is part of the Microsoft COM Tutorial Code Samples.
  24.  
  25.   Copyright (C) Microsoft Corporation, 1997.  All rights reserved.
  26.  
  27.   This source code is intended only as a supplement to Microsoft
  28.   Development Tools and/or on-line documentation.  See these other
  29.   materials for detailed information regarding Microsoft code samples.
  30.  
  31.   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  32.   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  33.   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  34.   PARTICULAR PURPOSE.
  35. ==========================================================================+*/
  36.  
  37. /*---------------------------------------------------------------------------
  38.   We include WINDOWS.H for all Win32 applications.
  39.   We include OLE2.H because we will be calling the COM/OLE Libraries.
  40.   We include APPUTIL.H because we will be building this DLL using
  41.     the convenient Virtual Window and Dialog classes and other
  42.     utility functions in the APPUTIL Library (ie, APPUTIL.LIB).
  43.   We include SERVER.H for the object class declarations for the
  44.     C++ CServer server control object.
  45. ---------------------------------------------------------------------------*/
  46. #include <windows.h>
  47. #include <ole2.h>
  48. #include <apputil.h>
  49. #include "server.h"
  50.  
  51.  
  52. /*---------------------------------------------------------------------------
  53.   Implementation the internal CServer C++ object.  Used to encapsulate
  54.   some server data and the methods for Lock and Object count incrementing
  55.   and decrementing.
  56. ---------------------------------------------------------------------------*/
  57.  
  58. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  59.   Method:   CServer::CServer
  60.  
  61.   Summary:  CServer Constructor.
  62.  
  63.   Args:     void
  64.  
  65.   Modifies: .
  66.  
  67.   Returns:  void
  68. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  69. CServer::CServer(void)
  70. {
  71.   // Init the Server License Key string.
  72.   lstrcpyA(
  73.     m_szLicenseKey,
  74.     "LICSERVE 1.0 - Component Server - Copyright (c) 1997 Microsoft Corp.");
  75.  
  76.   m_cLicenseLen = 0;
  77.   m_bLicensed = FALSE;
  78.  
  79.   m_hDllInst = NULL;
  80.   m_hWndParent = NULL;
  81.   m_pMsgBox = NULL;
  82.  
  83.   // Zero the Object and Lock counts for this attached process.
  84.   m_cObjects = 0;
  85.   m_cLocks = 0;
  86.  
  87.   return;
  88. }
  89.  
  90.  
  91. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  92.   Method:   CServer::~CServer
  93.  
  94.   Summary:  CServer Destructor.
  95.  
  96.   Args:     void
  97.  
  98.   Modifies: .
  99.  
  100.   Returns:  void
  101. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  102. CServer::~CServer(void)
  103. {
  104.   return;
  105. }
  106.  
  107.  
  108. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  109.   Method:   CServer::Lock
  110.  
  111.   Summary:  Increment the Server's Lock count.
  112.  
  113.   Args:     void
  114.  
  115.   Modifies: .
  116.  
  117.   Returns:  void
  118. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  119. void CServer::Lock(void)
  120. {
  121.   InterlockedIncrement((PLONG) &m_cLocks);
  122.  
  123.   LOGF1("P: CServer::Lock. New cLocks=%i.", m_cLocks);
  124.  
  125.   return;
  126. }
  127.  
  128.  
  129. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  130.   Method:   CServer::Unlock
  131.  
  132.   Summary:  Decrement the Server's Lock count.
  133.  
  134.   Args:     void
  135.  
  136.   Modifies: .
  137.  
  138.   Returns:  void
  139. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  140. void CServer::Unlock(void)
  141. {
  142.   InterlockedDecrement((PLONG) &m_cLocks);
  143.  
  144.   LOGF1("P: CServer::Unlock. New cLocks=%i.", m_cLocks);
  145.  
  146.   return;
  147. }
  148.  
  149.  
  150. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  151.   Method:   CServer::ObjectsUp
  152.  
  153.   Summary:  Increment the Server's living Object count.
  154.  
  155.   Args:     void
  156.  
  157.   Modifies: .
  158.  
  159.   Returns:  void
  160. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  161. void CServer::ObjectsUp(void)
  162. {
  163.   InterlockedIncrement((PLONG) &m_cObjects);
  164.  
  165.   LOGF1("P: CServer::ObjectsUp. New cObjects=%i.", m_cObjects);
  166.  
  167.   return;
  168. }
  169.  
  170.  
  171. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  172.   Method:   CServer::ObjectsDown
  173.  
  174.   Summary:  Decrement the Server's living object count.
  175.  
  176.   Args:     void
  177.  
  178.   Modifies: .
  179.  
  180.   Returns:  void
  181. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  182. void CServer::ObjectsDown(void)
  183. {
  184.   InterlockedDecrement((PLONG) &m_cObjects);
  185.  
  186.   LOGF1("P: CServer::ObjectsDown. New cObjects=%i.", m_cObjects);
  187.  
  188.   return;
  189. }
  190.  
  191.  
  192. /*M+M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M+++M
  193.   Method:   CServer::CheckLicense
  194.  
  195.   Summary:  Check for global machine license for this server.  Looks for a
  196.             LICSERVE.LIC file and compares its first text line with the
  197.             expected license key string stored in this server module.
  198.  
  199.   Args:     void
  200.  
  201.   Modifies: .
  202.  
  203.   Returns:  BOOL
  204.               TRUE if license verified; FALSE if not.
  205. M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M---M-M*/
  206. BOOL CServer::CheckLicense(void)
  207. {
  208.   TCHAR  szPath[MAX_PATH];
  209.   CHAR   szLicKeyRead[MAX_LICENSEKEY];
  210.   UINT   cbRead;
  211.   ULONG  cbWasRead;
  212.   UINT   cb;
  213.   HANDLE hFile;
  214.  
  215.   // Assume license not verified to start.
  216.   m_bLicensed = FALSE;
  217.  
  218.   // Assume the license key string is ASCII and count its length.
  219.   m_cLicenseLen = cb = lstrlenA(m_szLicenseKey);
  220.  
  221.   // Get the module path. Then parse and replace DLL extension with LIC.
  222.   MakeFamilyPath(m_hDllInst, szPath, TEXT(LICENSE_FILE_EXT));
  223.  
  224.   // Open the xx.LIC file and read in and check the license key for a match.
  225.   hFile = CreateFile(
  226.             szPath,
  227.             GENERIC_READ,
  228.             FILE_SHARE_READ,
  229.             NULL,
  230.             OPEN_EXISTING,
  231.             FILE_ATTRIBUTE_NORMAL,
  232.             NULL);
  233.  
  234.   if (INVALID_HANDLE_VALUE != hFile)
  235.   {
  236.     cbRead = cb * sizeof(CHAR);
  237.     ReadFile(hFile, szLicKeyRead, cbRead, &cbWasRead, NULL);
  238.     CloseHandle(hFile);
  239.     if (cbRead == cbWasRead)
  240.       if (0 == memcmp(m_szLicenseKey, szLicKeyRead, cb))
  241.         m_bLicensed = TRUE;
  242.   }
  243.  
  244.   LOGF1("P: CServer::CheckLicense. bLicensed=%i.", m_bLicensed);
  245.  
  246.   return m_bLicensed;
  247. }
  248.