2017年11月26日 星期日

[ Windows | MFC ] get appliation version

CString ClassName::getAppVersion()
{
     int major, minor, build, revision;
     DWORD verBufferSize;
     char verBuffer[2048];
     CString version;

     TCHAR AppName[MAX_PATH];
     GetModuleFileName(NULL, AppName, MAX_PATH);

     //  Get the size of the version info block in the file
     verBufferSize = GetFileVersionInfoSize(AppName, NULL);
     if (verBufferSize > 0 && verBufferSize <= sizeof(verBuffer))
     {
         //  get the version block from the file
         if (TRUE == GetFileVersionInfo(AppName, NULL, verBufferSize, verBuffer))
         {
              UINT length;
              VS_FIXEDFILEINFO *verInfo = NULL;

              //  Query the version information for neutral language
              if (TRUE == VerQueryValue(
                  verBuffer,
                  _T("\\"),
                  reinterpret_cast<LPVOID*>(&verInfo),
                  &length))
              {
                  //  Pull the version values.
                  major = HIWORD(verInfo->dwProductVersionMS);
                  minor = LOWORD(verInfo->dwProductVersionMS);
                  build = HIWORD(verInfo->dwProductVersionLS);
                  revision = LOWORD(verInfo->dwProductVersionLS);
              }
         }
     }

     version.Format(_T("V %d.%d.%d.%d"), major, minor, build, revision);
     return version;
}


沒有留言: