VerQueryValue获取文件-属性-详细信息

经典封装:https://blog.csdn.net/liwen930723/article/details/49471459 

结构包含了文件的版本信息:

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


版权声明:本文为CSDN博主「西伯利亚的寒流」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liwen930723/article/details/49471459

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JNA(Java Native Access)是一个Java库,可以让Java应用程序调用本地的C/C++动态链接库(DLL/SO)。如果要获取文件作者,需要使用Windows API中的GetFileVersionInfo函数和VerQueryValue函数。可以通过JNA调用这些函数来实现。 以下是示例代码: ```java import com.sun.jna.Pointer; import com.sun.jna.platform.win32.Version; public class FileVersionInfo { public static void main(String[] args) { String fileName = "C:\\Windows\\System32\\notepad.exe"; Version.VS_FIXEDFILEINFO fileInfo = getFileVersionInfo(fileName); if (fileInfo != null) { System.out.println("File version: " + fileInfo.dwFileVersionMS + "." + fileInfo.dwFileVersionLS); System.out.println("Product version: " + fileInfo.dwProductVersionMS + "." + fileInfo.dwProductVersionLS); String companyName = getStringFileInfo(fileName, "CompanyName"); System.out.println("Company name: " + companyName); String fileDescription = getStringFileInfo(fileName, "FileDescription"); System.out.println("File description: " + fileDescription); String fileVersion = getStringFileInfo(fileName, "FileVersion"); System.out.println("File version string: " + fileVersion); String productName = getStringFileInfo(fileName, "ProductName"); System.out.println("Product name: " + productName); String originalFilename = getStringFileInfo(fileName, "OriginalFilename"); System.out.println("Original filename: " + originalFilename); String comments = getStringFileInfo(fileName, "Comments"); System.out.println("Comments: " + comments); } else { System.out.println("File version info not found."); } } private static Version.VS_FIXEDFILEINFO getFileVersionInfo(String fileName) { Version version = Version.INSTANCE; int handle = version.GetFileVersionInfoSize(fileName, null); if (handle <= 0) { return null; } byte[] buffer = new byte[handle]; if (!version.GetFileVersionInfo(fileName, 0, handle, buffer)) { return null; } Pointer pointer = new Pointer(0); int[] length = new int[1]; if (!version.VerQueryValue(buffer, "\\", pointer, length)) { return null; } return new Version.VS_FIXEDFILEINFO(pointer.getByteArray(0, length[0])); } private static String getStringFileInfo(String fileName, String key) { Version version = Version.INSTANCE; int handle = version.GetFileVersionInfoSize(fileName, null); if (handle <= 0) { return null; } byte[] buffer = new byte[handle]; if (!version.GetFileVersionInfo(fileName, 0, handle, buffer)) { return null; } Pointer pointer = new Pointer(0); int[] length = new int[1]; if (!version.VerQueryValue(buffer, "\\StringFileInfo\\040904b0\\" + key, pointer, length)) { return null; } return pointer.getString(0); } } ``` 其中,`getFileVersionInfo`函数获取文件的版本信息,`getStringFileInfo`函数获取指定键的字符串信息。在`getStringFileInfo`函数中,`040904b0`是代表英语(美国)的语言和地区代码,可以根据需要修改。 注意,该代码只适用于Windows操作系统。如果要在其他操作系统上运行,需要使用不同的API函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值