1. 程式人生 > >利用WinAPI(GetFileVersionInfo)獲取檔案版本資訊

利用WinAPI(GetFileVersionInfo)獲取檔案版本資訊

1)檔案版本資訊在Windows內部,按照以下格式儲存:
例如:
2.10.0.256
->
dwFileVersionMS = 0x0002000A 高4位元組 = 2 = 0x0002; 低4位元組 = 10 = 0x000A
dwFileVersionLS = 0x00000100 高4位元組 = 0 = 0x0000; 低4位元組 = 256 = 0x0100

2)請注意要把Version.lib加到Link中。

bool GetFileVersion
(
	const std::string& filePath,
	unsigned int &nFileVersionMS,
	unsigned int &nFileVersionLS
)
{
	DWORD dwHandle;
	DWORD dwVersionInfoSize;
	dwVersionInfoSize = ::GetFileVersionInfoSize((char*)filePath.c_str(), &dwHandle);

	if (0 == dwVersionInfoSize)
	{
		return false;
	}

	char* pVersionInfo = new char[dwVersionInfoSize];
	if (FALSE == ::GetFileVersionInfo((char*)filePath.c_str(), 0, dwVersionInfoSize, pVersionInfo))
	{
		delete[] pVersionInfo;
		return false;
	}

	VS_FIXEDFILEINFO* pVersion = NULL;
	unsigned int nLen;
	if(FALSE == ::VerQueryValue(pVersionInfo, "//", (void**)&pVersion, &nLen))
	{
		delete[] pVersionInfo;
		return false;
	}

	nFileVersionMS = pVersion->dwFileVersionMS;
	nFileVersionLS = pVersion->dwFileVersionLS;

	delete[] pVersionInfo;
	return true;
}