#include "stdafx.h"
#include
#include
#include
#include
#pragma comment(lib,"version.lib")
using namespace std;
string GetFileVersion(std::string &strFilePath)
{
DWORD dwSize;
DWORD dwRtn;
std::string szVersion;
//获取版本信息大小
dwSize = GetFileVersionInfoSizeA(strFilePath.c_str(),NULL);
if (dwSize == 0)
{
return "";
}
char *pBuf;
pBuf = new char[dwSize + 1];
if (pBuf == NULL)
return "";
memset(pBuf,0,dwSize + 1);
dwRtn = GetFileVersionInfoA(strFilePath.c_str(),NULL,dwSize,pBuf);
if (dwRtn == 0)
return "";
LPVOID lpBuffer = NULL;
UINT uLen = 0;
//版本资源中获取信息
dwRtn = VerQueryValueA(pBuf,"\\StringFileInfo\\040904E4\\ProductVersion",&lpBuffer,&uLen);
if (dwRtn == 0)
return "";
szVersion = (char*)lpBuffer;
delete pBuf;
return szVersion;
}
int _tmain(int argc, _TCHAR* argv[])
{
string strFilePath = "C:\Users\jhon am\Desktop\Backupper.exe";
GetFileVersion(strFilePath);
getchar();
return 0;
}
这上面是我写的测试代码,始终获取不到文件的产品版本号,不知道错在哪里了,哪位知道的大神指点下,谢谢了