GetVersion.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FileName    :   GetVersion.cpp
  4. //  Version     :   1.0
  5. //  Creater     :   Cheng Bitao
  6. //  Date        :   2001-11-20 4:36:29
  7. //  Comment     :   
  8. //
  9. //////////////////////////////////////////////////////////////////////////////////////
  10. #include "Stdafx.h"
  11. #include "GetVersion.h"
  12. #include "KAVStrTranslate.h"
  13. int GetVersionStringFromInt(
  14.     int nMajorVersion, int nMinorVersion, 
  15.     char szVersion[], int nVersionLen
  16. )
  17. {
  18.     int Result  = false;
  19.     int nLen    = 0;
  20.     KAV_ASSERT_EXIT(szVersion);
  21.     
  22.     _itoa(nMajorVersion >> 16, szVersion, 10);
  23.     if (nMajorVersion & 0x0FFFF)
  24.     {
  25.         strcat(szVersion, ".");
  26.         nLen = strlen(szVersion);
  27.         _itoa(nMajorVersion & 0x0FFFF, szVersion + nLen, 10);
  28.     }
  29.     if (nMinorVersion >> 16)
  30.     {
  31.         strcat(szVersion, ".");
  32.         nLen = strlen(szVersion);
  33.         _itoa(nMinorVersion >> 16, szVersion + nLen, 10);
  34.     }
  35.     if (nMinorVersion & 0x0FFFF)
  36.     {
  37.         strcat(szVersion, ".");
  38.         nLen = strlen(szVersion);
  39.         _itoa(nMinorVersion & 0x0FFFF, szVersion + nLen, 10);
  40.     }
  41.     Result = true;
  42. Exit0:    
  43.     return Result;
  44. }
  45. int GetVersionFromString(const char cszVersion[], unsigned *puMajorVersion, unsigned *puMinorVersion)
  46. {
  47.     char *pszFileVersionDetail[4];
  48.     char *pszFileVersion    = NULL;
  49.     char *pszVersionPos     = NULL;
  50.     char *pszPointerPos     = NULL;
  51.     int  i                  = 0;
  52.     int  nLen               = 0;   
  53.     int  nValue             = 0;
  54.     int  nResult            = false;
  55.     _int64  nFileVersion    = 0;
  56.     ASSERT(puMajorVersion);
  57.     ASSERT(puMinorVersion);
  58.     *puMajorVersion = 0;
  59.     *puMinorVersion = 0;
  60.     nLen = strlen(cszVersion);
  61.     pszFileVersion = new char[nLen + 1];
  62.     KAV_PROCESS_ERROR(pszFileVersion);
  63.     strcpy(pszFileVersion, cszVersion);
  64.     pszVersionPos = pszFileVersion;
  65.     for (i = 0; i < 4; i++)
  66.     {
  67.         if (!pszVersionPos)
  68.         {
  69.             pszFileVersionDetail[i] = new char[2];
  70.             KAV_PROCESS_ERROR(pszFileVersionDetail[i]);
  71.             strcpy(pszFileVersionDetail[i], "0");
  72.             continue;
  73.         }
  74.         pszPointerPos = strchr(pszVersionPos, '.');
  75.         if ((!pszPointerPos) && pszVersionPos)
  76.         {
  77.             nLen = strlen(pszVersionPos);
  78.             pszFileVersionDetail[i] = new char[nLen + 1];
  79.             KAV_PROCESS_ERROR(pszFileVersionDetail[i]);
  80.             strcpy(pszFileVersionDetail[i], pszVersionPos);
  81.             
  82.             pszVersionPos = NULL;
  83.             continue;
  84.         }
  85.         
  86.         nLen = pszPointerPos - pszVersionPos;
  87.         pszFileVersionDetail[i] = new char[nLen + 1];
  88.         KAV_PROCESS_ERROR(pszFileVersionDetail[i]);
  89.         
  90.         strncpy(pszFileVersionDetail[i], pszVersionPos, nLen);
  91.         *(pszFileVersionDetail[i] + nLen) = '';
  92.         pszVersionPos = pszPointerPos + 1;
  93.     }
  94.         
  95.     for (i = 0; i < 4; i++)
  96.     {
  97.         nValue = _StrToInt(pszFileVersionDetail[i]);
  98.         nValue = abs(nValue);
  99.         nFileVersion = (nFileVersion << 16) + (unsigned)nValue;
  100.     }
  101.     *puMajorVersion = (unsigned)(nFileVersion >> 32);
  102.     *puMinorVersion = (unsigned)nFileVersion;
  103.     nResult = true;
  104. Exit0:
  105.     KAV_DELETE_ARRAY(pszFileVersion);
  106.     for (i = 0; i < 4; i++)
  107.         KAV_DELETE_ARRAY(pszFileVersionDetail[i]);
  108.     return nResult;
  109. }
  110. int GetFileVersion(
  111.     const char cszFileName[], 
  112.     DWORD *pdwProductVersionMS, 
  113.     DWORD *pdwProductVersionLS
  114. )
  115. {
  116.     int nResult             = false;
  117.     int nRetCode            = false;
  118.     DWORD dwHandle          = 0;
  119.     DWORD dwFileInfoSize    = 0;
  120.     VS_FIXEDFILEINFO *pFixFileInfo = NULL;
  121.     char *pszFileInfo       = NULL;
  122.     UINT uLen               = 0;
  123.     
  124.     KAV_ASSERT_EXIT(cszFileName);
  125.     KAV_ASSERT_EXIT(pdwProductVersionMS);
  126.     KAV_ASSERT_EXIT(pdwProductVersionLS);
  127.     *pdwProductVersionMS = 0;
  128.     *pdwProductVersionLS = 0;
  129.     
  130.     dwFileInfoSize = ::GetFileVersionInfoSize((char *)cszFileName, &dwHandle);
  131.     KAV_PROCESS_ERROR(dwFileInfoSize);
  132.     
  133.     pszFileInfo = new char[dwFileInfoSize];
  134.     KAV_PROCESS_ERROR(pszFileInfo);
  135.     
  136.     nRetCode = GetFileVersionInfo((char *)cszFileName, dwHandle, dwFileInfoSize, pszFileInfo);
  137.     KAV_PROCESS_ERROR(nRetCode);
  138.     
  139.     //pFixFileInfo = new VS_FIXEDFILEINFO;
  140.     //if (pFixFileInfo == NULL)
  141.     //    goto Exit0;
  142.     
  143.     nRetCode = VerQueryValue(pszFileInfo, "\", (LPVOID *)&pFixFileInfo, &uLen);
  144.     KAV_PROCESS_ERROR(nRetCode);
  145.     
  146.     if (uLen > 0)
  147.     {
  148.         *pdwProductVersionMS = pFixFileInfo->dwProductVersionMS;
  149.         *pdwProductVersionLS = pFixFileInfo->dwProductVersionLS;
  150.     }    
  151.     nResult = true;    
  152. Exit0:
  153.     KAV_DELETE_ARRAY(pszFileInfo);
  154.    
  155.     return nResult;
  156. }
  157. int GetVersion(const char cszFileName[], char *pszVersion, int nVersionSize)
  158. {
  159.     int nRetCode            = false;
  160.     int nResult             = false;
  161.     DWORD dwMajorVersion    = 0;
  162.     DWORD dwMinorVersion    = 0;
  163.     
  164.     KAV_ASSERT_EXIT(cszFileName);
  165.     KAV_ASSERT_EXIT(pszVersion);
  166.     
  167.     *pszVersion = '';
  168.     nRetCode = GetFileVersion(cszFileName, &dwMajorVersion, &dwMinorVersion);
  169.     KAV_ASSERT_EXIT(nRetCode);
  170.     nRetCode = GetVersionStringFromInt(dwMajorVersion, dwMinorVersion, pszVersion, nVersionSize);
  171.     KAV_ASSERT_EXIT(nRetCode);
  172.     nResult = true;
  173. Exit0:    
  174.     return nResult;
  175. }
  176. int GetProgramVersion(CString &sVersion)
  177. {
  178.     HINSTANCE hModule = NULL;
  179.     int nResult                 = false;
  180.     int nRetCode                = false;
  181.     char szFileName[MAX_PATH]   = {0};
  182.     char szVersion[20]          = {0};
  183.     
  184.     KAV_ASSERT_EXIT(sVersion);
  185.     sVersion.Empty();
  186.     hModule = AfxGetInstanceHandle();
  187.     KAV_ASSERT_EXIT(hModule);
  188.     nRetCode = GetModuleFileName((HINSTANCE)hModule, szFileName, MAX_PATH);
  189.     KAV_ASSERT_EXIT(hModule);
  190.     nRetCode = GetVersion(szFileName, szVersion, 20);
  191.     KAV_ASSERT_EXIT(nRetCode);
  192.     sVersion = (CString)szVersion;    
  193.     nResult = true;
  194. Exit0:
  195.     return nResult;
  196. }