visual-c++mfcversioninfo

How can this code to compare version information be improved?


I have a problem with my logic to compare version information.

Take this method:

bool CPTSDatabase::IsInstalledPublicTalksVersionSupported()
{
    CSettingsStore store(TRUE, TRUE);
    CString strPath, strVersion;
    VS_FIXEDFILEINFO* pVerInfo = nullptr;
    bool bOK = false;

    if (store.Open(_T("Software\\Community Talks\\Public Talks")))
    {
        if (store.Read(_T("AppPath"), strPath))
        {
            //yes, but is the path still valid
            if (PathFileExists(strPath))
            {
                pVerInfo = theApp.ExtractVersionInformation(strPath, strVersion);
                if (pVerInfo != nullptr)
                {
                    if (HIWORD(pVerInfo->dwFileVersionMS) >= 20 &&
                        LOWORD(pVerInfo->dwFileVersionMS) >= 0 &&
                        HIWORD(pVerInfo->dwFileVersionLS) >= 3)
                    {
                        bOK = true;
                    }
                }
            }
        }
    }

    return bOK;
}

The issue is this bit:

if (HIWORD(pVerInfo->dwFileVersionMS) >= 20 &&
    LOWORD(pVerInfo->dwFileVersionMS) >= 0 &&
    HIWORD(pVerInfo->dwFileVersionLS) >= 3)
{
    bOK = true;
}

The executable in question is now 21.0.1 and ofcourse the pVerInfo->dwFileVersionLS test is failing. Is there a more robust wat to check that the version in question is greater than 20.0.3 without a lot of if clauses?


Solution

  • Just use a 64bit int.

    auto Version = [](WORD a, WORD b, WORD c)
    {
      return int64_t(a)<<32 | int64_t(b)<<16 | int64_t(c);
    };
    
    if (Version(HIWORD(pVerInfo->dwFileVersionMS),LOWORD(pVerInfo->dwFileVersionMS),HIWORD(pVerInfo->dwFileVersionLS))>=Version(20,0,3))
    {
      bOK = true;
    }