1. 程式人生 > >VerQueryValue獲取檔案-屬性-詳細資訊

VerQueryValue獲取檔案-屬性-詳細資訊

結構包含了檔案的版本資訊:

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 <Windows.h>
#include <string>

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); };    //獲取產品版本