From 10ea1d3e52217ccd93dfa6830776a6be308ca1f6 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Mon, 12 Apr 2021 20:40:20 -0500 Subject: Use verutil for handling versions in WiuEnumRelatedProductCodes. --- src/dutil/wiutil.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/dutil/wiutil.cpp b/src/dutil/wiutil.cpp index f1984266..7414ac42 100644 --- a/src/dutil/wiutil.cpp +++ b/src/dutil/wiutil.cpp @@ -674,8 +674,9 @@ extern "C" HRESULT DAPI WiuEnumRelatedProductCodes( HRESULT hr = S_OK; WCHAR wzCurrentProductCode[MAX_GUID_CHARS + 1] = { }; LPWSTR sczInstalledVersion = NULL; - DWORD64 qwCurrentVersion = 0; - DWORD64 qwHighestVersion = 0; + VERUTIL_VERSION* pCurrentVersion = NULL; + VERUTIL_VERSION* pHighestVersion = NULL; + int nCompare = 0; // make sure we start at zero *pcRelatedProducts = 0; @@ -702,31 +703,40 @@ extern "C" HRESULT DAPI WiuEnumRelatedProductCodes( continue; } - // try to parse the product version but if it is corrupt (for whatever - // reason), skip it - hr = FileVersionFromStringEx(sczInstalledVersion, 0, &qwCurrentVersion); - if (FAILED(hr)) + hr = VerParseVersion(sczInstalledVersion, 0, FALSE, &pCurrentVersion); + WiuExitOnFailure(hr, "Failed to parse version: %ls for product code: %ls", sczInstalledVersion, wzCurrentProductCode); + + if (pCurrentVersion->fInvalid) { - WiuExitTrace(hr, "Could not convert version: %ls to DWORD64 for product code: %ls, skipping...", sczInstalledVersion, wzCurrentProductCode); - continue; + WiuExitTrace(E_INVALIDDATA, "Enumerated msi package with invalid version, product code: '%1!ls!', version: '%2!ls!'"); } // if this is the first product found then it is the highest version (for now) - if (0 == *pcRelatedProducts) + if (!pHighestVersion) { - qwHighestVersion = qwCurrentVersion; + pHighestVersion = pCurrentVersion; + pCurrentVersion = NULL; } else { + hr = VerCompareParsedVersions(pCurrentVersion, pHighestVersion, &nCompare); + WiuExitOnFailure(hr, "Failed to compare version '%ls' to highest version: '%ls'", pCurrentVersion->sczVersion, pHighestVersion->sczVersion); + // if this is the highest version encountered so far then overwrite // the first item in the array (there will never be more than one item) - if (qwCurrentVersion > qwHighestVersion) + if (nCompare > 0) { - qwHighestVersion = qwCurrentVersion; + ReleaseVerutilVersion(pHighestVersion); + pHighestVersion = pCurrentVersion; + pCurrentVersion = NULL; hr = StrAllocString(prgsczProductCodes[0], wzCurrentProductCode, 0); WiuExitOnFailure(hr, "Failed to update array with higher versioned product code."); } + else + { + ReleaseVerutilVersion(pCurrentVersion); + } // continue here as we don't want anything else added to the list continue; @@ -738,6 +748,8 @@ extern "C" HRESULT DAPI WiuEnumRelatedProductCodes( } LExit: + ReleaseVerutilVersion(pCurrentVersion); + ReleaseVerutilVersion(pHighestVersion); ReleaseStr(sczInstalledVersion); return hr; } -- cgit v1.2.3-55-g6feb