如何使用API函数GetFileVersionInfo,获得版本信息
来源:互联网 发布:小差事软件下载 编辑:程序博客网 时间:2024/05/18 18:43
使用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;
}
- 如何使用API函数GetFileVersionInfo,获得版本信息
- 如何使用API函数GetFileVersionInfo,获得版本信息
- 获取EXE版本信息 GetFileVersionInfo
- 利用WinAPI(GetFileVersionInfo)获取文件版本信息
- GetFileVersionInfo 获取文件版本信息错误原因分析
- 利用WinAPI(GetFileVersionInfo)获取文件版本信息
- 《Windows API巡礼》---GetFileVersionInfo函数和VerQueryValue函数
- 用API函数获得系统本地信息
- Android如何获得手机版本信息?
- QT用API获得文件的版本信息
- QT用API获得文件的版本信息
- QT用API获得文件的版本信息
- GetFileVersionInfo
- 获得软件版本信息
- 获得软件版本信息
- 获得版本信息
- 获得文件版本信息
- 如何获得WM设备信息,如型号、操作系统版本等
- 乔布斯传》最后一章精华节选
- 调用setsockopt()函数时的WSAEINVAL错误
- Android UI开发第八篇——ViewFlipper 左右滑动效果
- 快速排序Java源码(递归和非递归)
- Android UI开发第九篇——SlidingDrawer 抽屉效果
- 如何使用API函数GetFileVersionInfo,获得版本信息
- Literal与label的区别
- WCF客户端和服务端配置
- Myeclipse SSH框架配置流程
- inote记事工具-PreferenceScreen及PreferenceActivity使用
- sizeof
- mysql导入数据方法mysqlimport
- hdu 1016 Prime Ring Problem
- 给自己提一个问题,关于MFC的,改日再解决