欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

应用程序版本号比对

程序员文章站 2024-02-20 19:32:40
...
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);


相关标签: Compare Version