获取EXE文件版本信息(版本格式:xx.xx.xx.xx)

BOOL GetVersion(LPTSTR lpszVersion, size_t nLength, LPCTSTR lpszExecute)
{
	BOOL bRet = FALSE;

	TCHAR* pVersion = NULL;

	do
	{
		if ((NULL == lpszVersion) || (nLength <= 0) || (NULL == lpszExecute)) { break; }
		
		DWORD dwSize = GetFileVersionInfoSize(lpszExecute, NULL);
		if (0 == dwSize) { break; }

		pVersion = new TCHAR[dwSize + 1];
		if (NULL == pVersion) { break; }

		_ASSERT(NULL != pVersion);
		memset(pVersion, 0, sizeof(TCHAR) * (dwSize + 1));

		if (!GetFileVersionInfo(lpszExecute, 0, dwSize, pVersion)) { break; }

		VS_FIXEDFILEINFO * pInfo = NULL;
		unsigned int nInfoLen = 0;

		if (!VerQueryValue(pVersion, TEXT("\\"), (void**)&pInfo, &nInfoLen)) { break; }

		StringCchPrintf(lpszVersion, nLength, TEXT("%d.%d.%d.%d"), HIWORD(pInfo->dwFileVersionMS), LOWORD(pInfo->dwFileVersionMS), HIWORD(pInfo->dwFileVersionLS), LOWORD(pInfo->dwFileVersionLS));

		// Completed
		bRet = TRUE;
	} while (0);
	
	if (NULL != pVersion) { delete[] pVersion; pVersion = NULL; }

	return bRet;
}


你可能感兴趣的:(VC/MFC,Development)