利用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;
}



 

你可能感兴趣的:(Windows)