home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / ctllic.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  2KB  |  86 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFXCTL_FACT_SEG
  14. #pragma code_seg(AFXCTL_FACT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // AfxVerifyLicFile - Checks that a license file exists and contains a
  26. //    specific byte pattern.
  27.  
  28. BOOL AFXAPI AfxVerifyLicFile(HINSTANCE hInstance, LPCTSTR pszLicFileName,
  29.     LPCOLESTR pszLicFileContents, UINT cch)
  30. {
  31.     // Assume the worst...
  32.     BOOL bVerified = FALSE;
  33.  
  34.     // Look for license file in same directory as this DLL.
  35.     TCHAR szPathName[_MAX_PATH];
  36.     ::GetModuleFileName(hInstance, szPathName, _MAX_PATH);
  37.     LPTSTR pszFileName = _tcsrchr(szPathName, '\\') + 1;
  38.     lstrcpy(pszFileName, pszLicFileName);
  39.  
  40. #ifndef OLE2ANSI
  41.     LPSTR pszKey = NULL;
  42. #endif
  43.     LPBYTE pbContent = NULL;
  44.  
  45.     TRY
  46.     {
  47.         // Open file, read content and compare.
  48.  
  49.         CFile file(szPathName, CFile::modeRead);
  50.  
  51.         if (cch == -1)
  52. #ifdef OLE2ANSI
  53.             cch = lstrlen(pszLicFileContents);
  54. #else
  55.             cch = wcslen(pszLicFileContents);
  56.  
  57.         pszKey = (char*)_alloca(cch*2 + 1);
  58.         cch = _wcstombsz(pszKey, pszLicFileContents, cch*2 + 1);
  59. #endif
  60.  
  61.         if (cch != 0)
  62.         {
  63.             --cch;  // license file won't contain the terminating null char
  64.             pbContent = (BYTE*)_alloca(cch);
  65.             file.Read(pbContent, cch);
  66.  
  67. #ifndef OLE2ANSI
  68.             if (memcmp(pszKey, pbContent, (size_t)cch) == 0)
  69. #else
  70.             if (memcmp(pszLicFileContents, pbContent, (size_t)cch) == 0)
  71. #endif
  72.                 bVerified = TRUE;
  73.         }
  74.     }
  75.     END_TRY
  76.  
  77.     return bVerified;
  78. }
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // Force any extra compiler-generated code into AFX_INIT_SEG
  82.  
  83. #ifdef AFX_INIT_SEG
  84. #pragma code_seg(AFX_INIT_SEG)
  85. #endif
  86.