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

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

程序员文章站 2024-02-20 19:18:58
...
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;
}


相关标签: Version