应用程序版本号比对

BOOL CompareVersion(LPCTSTR lpszVer1, LPCTSTR lpszVer2, int& nResult)
{
	BOOL bRet = FALSE;

	do
	{
		if ((NULL == lpszVer1) || (NULL == lpszVer2)) { break; }

		// Version: a.b.c.d
		#define DEFAULT_MAX_VERSION_COUNT	(4)

		int nVer1[DEFAULT_MAX_VERSION_COUNT] = { 0 };
		int nVer2[DEFAULT_MAX_VERSION_COUNT] = { 0 };

		ASSERT(_countof(nVer1) == _countof(nVer2));

		_stscanf_s(lpszVer1, TEXT("%d.%d.%d.%d"), &nVer1[0], &nVer1[1], &nVer1[2], &nVer1[3]);
		_stscanf_s(lpszVer2, TEXT("%d.%d.%d.%d"), &nVer2[0], &nVer2[1], &nVer2[2], &nVer2[3]);

		for (size_t i = 0; i != min(_countof(nVer1), _countof(nVer2)); ++i)
		{
			if (nVer1[i] == nVer2[i]) { nResult = 0; continue; }
			if (nVer1[i] > nVer2[i]) { nResult = 1; break; }
			if (nVer1[i] < nVer2[i]) { nResult = -1; break; }
		}

		// Completed
		bRet = TRUE;
	} while (0);

	return bRet;
}

// For Example:
int nResult = 0;
CompareVersion(TEXT("1.0.6.09"), TEXT("1.0.5.102"), nResult);
CString strText;
strText.Format(TEXT("Result: %d"), nResult);
AfxMessageBox(strText);


猜你喜欢

转载自blog.csdn.net/visualeleven/article/details/80188286