如何使用API函数GetFileVersionInfo,获得版本信息

本文介绍了如何利用GetFileVersionInfoSize(), GetFileVersionInfo()和VerQueryValue()三个API函数获取.exe和.dll文件的版本信息。通过示例代码展示如何获取指定文件路径的版本字符串,包括自身和其他文件的版本信息。" 108230070,9289833,使用websocket快速实现多人协同编辑,"['前端开发', '后端开发', 'websocket', 'Python', 'Vue.js']

使用GetFileVersionInfoSize(),GetFileVersionInfo()和VerQueryValue()三个API可以获得.exe和.dll文件的版本信息


1.获得自身的版本信息

 
//////////////////////////////////////////////////////////////
//
// File: version.cpp
// Description: Sample code for getting version info
// Created: 2008-1-4
// Author: Ken Zhang
// E-Mail: cpp.china@hotmail.com
//
//////////////////////////////////////////////////////////////

/*
The following code shows how to get FILEINFO value from resource file.

These WIN32 functions will be used:

* GetFileVersionInfo
* GetFileVersionInfoSize
* VerQueryValue
* GetModuleFileName
*/


#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>

#pragma comment(lib, "version.lib")
using namespace std ;

bool GetFileVersion ( HMODULE hModule , WORD * pBuffer )
{
TCHAR fname
[ MAX_PATH ];
VS_FIXEDFILEINFO
* pVi ;
DWORD dwHandle
;
string str
;

if (:: GetModuleFileName ( hModule , fname , MAX_PATH ))
{
int size = GetFileVersionInfoSize ( fname , & dwHandle );

if ( size > 0 ) {
BYTE
* buffer = new BYTE [ size ];

if ( GetFileVersionInfo ( fname , dwHandle , size , buffer )) {
if ( VerQueryValue ( buffer , _T ( "\\"), ( LPVOID *)& pVi , ( PUINT )& size )) {
pBuffer
[ 0 ] = HIWORD ( pVi -> dwFileVersionMS );
pBuffer
[ 1 ] = LOWORD ( pVi -> dwFileVersionMS );
pBuffer
[ 2 ] = HIWORD ( pVi -> dwFileVersionLS );
pBuffer
[ 3 ] = LOWORD ( pVi -> dwFileVersionLS );

delete buffer ;
return true ;
}
}

delete buffer ;
}
}

return false ;
}

string
GetFileVersion ( HMODULE hModule )
{
string str
;
WORD buffer
[ 4 ];

if ( GetFileVersion ( hModule , buffer ))
{
char str2 [ 32 ];

for ( int i = 0 ; i < sizeof ( buffer )/ sizeof ( WORD ); i ++)
{
itoa
( buffer [ i ], str2 , 10 );
str
+= str2 ;

if ( i != sizeof ( buffer )/ sizeof ( WORD ) - 1 )
{
str
+= "." ;
}
}
}

return str ;
}

void main ()
{
cout
<< "Current version is: " << GetFileVersion (:: GetModuleHandle ( NULL )) << endl ;
}

2.获得其他exe或dll的版本信息

std::string GetFileVersion(const std::string &strFilePath)
{
DWORD dwSize;
DWORD dwRtn;
std::string szVersion;

//获取版本信息大小
dwSize = GetFileVersionInfoSize(_T(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 = GetFileVersionInfo(_T(strFilePath.c_str()),NULL, dwSize, pBuf);
if(dwRtn == 0)
{
return "";
}

LPVOID lpBuffer = NULL;
UINT uLen = 0;
//版本资源中获取信息
dwRtn = VerQueryValue(pBuf,
TEXT("\\StringFileInfo\\080404b0\\FileVersion"), //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()
{
std::string strFilePath = "abc.exe";
cout << strFilePath << " version is: " << GetFileVersion(strFilePath) << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值