C++获取文件版本等信息
#include "stdio.h"
#include <iostream>
#include <string>
#include <tchar.h>
#include <windows.h>
#pragma comment(lib, "version.lib")
using namespace std;
std::string GetFileVersion(char *strFilePath)
{
DWORD dwSize;
DWORD dwRtn;
std::string szVersion;
//获取版本信息大小
dwSize = GetFileVersionInfoSize(strFilePath, NULL);
if (dwSize == 0)
{
return "读取文件失败!";
}
char *pBuf;
pBuf = new char[dwSize + 1];
if (pBuf == NULL)
return "";
memset(pBuf, 0, dwSize + 1);
//获取版本信息
dwRtn = GetFileVersionInfo(strFilePath, NULL, dwSize, pBuf);
if (dwRtn == 0)
{
return "";
}
LPVOID lpBuffer = NULL;
UINT uLen = 0;
//版本资源中获取信息
dwRtn = VerQueryValue(pBuf,
TEXT("\\StringFileInfo\\080404b0\\FileDescription"), // 0804中文
// 04b0即1252,ANSI
//可以从ResourceView中的Version中BlockHeader中看到
//可以测试的属性
/*
CompanyName
FileDescription
FileVersion
InternalName
LegalCopyright
OriginalFilename
ProductName
ProductVersion
Comments
LegalTrademarks
PrivateBuild
SpecialBuild
*/
&lpBuffer,
&uLen);
if (dwRtn == 0)
{
return "";
}
szVersion = (char *)lpBuffer;
delete pBuf;
return szVersion;
}
void main()
{
char *strFilePath = "C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQ.exe";
cout << strFilePath << " FileDescription is: " << GetFileVersion(strFilePath) << endl;
getchar();
}