结构包含了文件的版本信息:
GetFileVersionInfoSize函数用于判断系统能否检索到指定文件的版本信息,如果可以函数返回版本信息的字节大小:
DWORD WINAPI GetFileVersionInfoSize(
__in LPCTSTR lptstrFilename, //指定文件的名称
__out_opt LPDWORD lpdwHandle //一个变量的指针,该函数将该变量设为0
);
GetFileVersionInfo函数用来获得指定文件的版本信息:
BOOL WINAPI GetFileVersionInfo(
__in LPCTSTR lptstrFilename, //文件名
__reserved DWORD dwHandle, //保留值
__in DWORD dwLen, //lpData指向缓冲区的大小,使用函数GetFileVersionInfoSize得到
__out LPVOID lpData //指向存放文件版本信息的缓冲区的指针
);
VerQueryValue函数用于从指定的版本信息源获取版本信息,在调用该函数之前,需要先依次调用函数GetFileVersionInfoSize和GetFileVersionInfo:
BOOL WINAPI VerQueryValue(
__in LPCVOID pBlock, //由函数GetFileVersionInfo得到的版本信息源
__in LPCTSTR lpSubBlock, //“/”表示该函数获取VS_FIXEDFILEINFO结构信息
//为“/VarFileInfo/Translation”表示该函数获取文件的翻译表
//为“/StringFileInfo/lang-codepage/string-name”表示该函数获取文件的字符串信息
__out LPVOID *lplpBuffer, //返回指向pBlock指向的地址,当pBlock被释放时,该参数也被释放
__out PUINT puLen //lplpBuffer指向的数据的字节大小
);
上面参数lpSubBlock取值中的string-name必须是下面系统预定义的字符串之一:
头文件FileAttribute.h:
// FileAttribute.h: interface for the FileAttribute
// by xiboliya
//////////////////////////////////////////////////////////////////////
// #ifndef __FILEATTRIBUTE_H_
// #define __FILEATTRIBUTE_H_
#pragma once
#include
#include
namespace BaseFlow
{
namespace Attribute
{
bool GetFileDescription(const std::string& szModuleName, std::string& RetStr);
bool GetFileVersion(const std::string& szModuleName, std::string& RetStr);
bool GetInternalName(const std::string& szModuleName, std::string& RetStr);
bool GetCompanyName(const std::string& szModuleName, std::string& RetStr);
bool GetLegalCopyright(const std::string& szModuleName, std::string& RetStr);
bool GetOriginalFilename(const std::string& szModuleName, std::string& RetStr);
bool GetProductName(const std::string& szModuleName, std::string& RetStr);
bool GetProductVersion(const std::string& szModuleName, std::string& RetStr);
}
}
//#endif // __FILEATTRIBUTE_H_
源文件FileAttribute.cpp:
// FileAttribute.cpp: 获取文件-属性-详细信息
// by liwen
//////////////////////////////////////////////////////////////////////
#include "attribute.h"
#pragma comment(lib, "version")
bool QueryValue(const std::string& ValueName, const std::string& szModuleName, std::string& RetStr)
{
bool bSuccess = FALSE;
BYTE* m_lpVersionData = NULL;
DWORD m_dwLangCharset = 0;
CHAR *tmpstr = NULL;
do
{
if (!ValueName.size() || !szModuleName.size())
break;
DWORD dwHandle;
// 判断系统能否检索到指定文件的版本信息
DWORD dwDataSize = ::GetFileVersionInfoSizeA((LPCSTR)szModuleName.c_str(), &dwHandle);
if (dwDataSize == 0)
break;
m_lpVersionData = new (std::nothrow) BYTE[dwDataSize];// 分配缓冲区
if ( NULL == m_lpVersionData)
break;
// 检索信息
if (!::GetFileVersionInfoA((LPCSTR)szModuleName.c_str(), dwHandle, dwDataSize,
(void*)m_lpVersionData))
break;
UINT nQuerySize;
DWORD* pTransTable;
// 设置语言
if (!::VerQueryValueA(m_lpVersionData, "\\VarFileInfo\\Translation", (void **)&pTransTable, &nQuerySize))
break;
m_dwLangCharset = MAKELONG(HIWORD(pTransTable[0]), LOWORD(pTransTable[0]));
if (m_lpVersionData == NULL)
break;
tmpstr = new (std::nothrow) CHAR[128];// 分配缓冲区
if (NULL == tmpstr)
break;
sprintf_s(tmpstr, 128, "\\StringFileInfo\\%08lx\\%s", m_dwLangCharset, ValueName.c_str());
LPVOID lpData;
// 调用此函数查询前需要先依次调用函数GetFileVersionInfoSize和GetFileVersionInfo
if (::VerQueryValueA((void *)m_lpVersionData, tmpstr, &lpData, &nQuerySize))
RetStr = (char*)lpData;
bSuccess = TRUE;
} while (FALSE);
// 销毁缓冲区
if (m_lpVersionData)
{
delete[] m_lpVersionData;
m_lpVersionData = NULL;
}
if (tmpstr)
{
delete[] tmpstr;
tmpstr = NULL;
}
return bSuccess;
}
bool BaseFlow::Attribute::GetFileDescription(const std::string& szModuleName, std::string& RetStr) { return QueryValue("FileDescription", szModuleName, RetStr); }; //获取文件说明
bool BaseFlow::Attribute::GetFileVersion(const std::string& szModuleName, std::string& RetStr) { return QueryValue("FileVersion", szModuleName, RetStr); }; //获取文件版本
bool BaseFlow::Attribute::GetInternalName(const std::string& szModuleName, std::string& RetStr) { return QueryValue("InternalName", szModuleName, RetStr); }; //获取内部名称
bool BaseFlow::Attribute::GetCompanyName(const std::string& szModuleName, std::string& RetStr) { return QueryValue("CompanyName", szModuleName, RetStr); }; //获取公司名称
bool BaseFlow::Attribute::GetLegalCopyright(const std::string& szModuleName, std::string& RetStr) { return QueryValue("LegalCopyright", szModuleName, RetStr); }; //获取版权
bool BaseFlow::Attribute::GetOriginalFilename(const std::string& szModuleName, std::string& RetStr) { return QueryValue("OriginalFilename", szModuleName, RetStr); }; //获取原始文件名
bool BaseFlow::Attribute::GetProductName(const std::string& szModuleName, std::string& RetStr) { return QueryValue("ProductName", szModuleName, RetStr); }; //获取产品名称
bool BaseFlow::Attribute::GetProductVersion(const std::string& szModuleName, std::string& RetStr) { return QueryValue("ProductVersion", szModuleName, RetStr); }; //获取产品版本