Qt开源小项目–查看dll,exe的版本信息
先上图看看效果:
在工作可能经常遇到的对文件的签名,dll和exe中所带的信息进行提取和验证。所以我就
封装成一个类,以后直接调用了,非常的方便。大家拿到这个类就可以直接使用在项目中。
这里我就贴出主要的代码,我会把完整的项目放到github上面。
/****************************************************************************
**
** Copyright (C) 2018 liushixiong (liushixiongcpp@163.com)
** All rights reserved.
**
****************************************************************************/
#include "dolproductinfo.h"
#include <wincrypt.h>
#include <wintrust.h>
#include "..\common\common.h"
#pragma comment(lib, "crypt32.lib")
#define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
const int LINE_BUFFER_SIZE = 2048;
dolProductInfo::dolProductInfo(const string &pFilePath, bool pIsInit)
: mFilePath(pFilePath)
{
if (pIsInit) { Init(); }
}
dolProductInfo::~dolProductInfo()
{
}
bool dolProductInfo::Init()
{
DWORD temp;
UINT size;
char tempbuf[LINE_BUFFER_SIZE] = { 0 };
HANDLE hFile = CreateFileA(mFilePath.c_str(), GENERIC_READ,
FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) { return false; }
char szMZHader[0x40];
char szMewHader[0x1000];
DWORD read;
bool result = ReadFile(hFile, szMZHader, 0x40, &read, NULL);
if (! result || read != 0x40) {
return false;
}
DWORD filesize = GetFileSize(hFile, NULL);
FormatDWord(filesize, tempbuf);
mSize = tempbuf;
mSize += " Bytes";
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
FILETIME ftCreation, ftModified;
GetFileTime(hFile, &ftCreation, NULL, &ftModified);
FormatFileTime(ftCreation, tempbuf);
mCreationTime = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
FormatFileTime(ftModified, tempbuf);
mModifiedTime = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
CloseHandle(hFile);
if ((size = GetFileVersionInfoSizeA(mFilePath.c_str(), &temp)) < 0) {
return false;
}
char *buf = new char[size];
char *str;
char szLang1[20];
if (! GetFileVersionInfoA(mFilePath.c_str(), 0, size, buf)) {
delete[] buf;
return false;
}
VS_FIXEDFILEINFO *info;
if (VerQueryValueA((LPVOID)buf, ("\\"), (LPVOID *)&info, &size)) {
string retvalue;
mType = GetFileType(info->dwFileType, retvalue);
sprintf_s(tempbuf, LINE_BUFFER_SIZE, "%d.%d.%d.%d",
(info->dwFileVersionMS >> 16),
(info->dwFileVersionMS & 65535),
(info->dwFileVersionLS >> 16),
info->dwFileVersionLS & 65535);
mFileVersion = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
sprintf_s(tempbuf, LINE_BUFFER_SIZE, "%d.%d.%d.%d",
(info->dwProductVersionMS >> 16),
(info->dwProductVersionMS & 65535),
(info->dwProductVersionLS >> 16),
info->dwProductVersionLS & 65535);
mProductVersion = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
if (! VerQueryValueA((LPCVOID)buf,
("\\VarFileInfo\\Translation"),
(LPVOID *)&str, &size)) {
// return false;
}
sprintf_s(szLang1, sizeof(szLang1)/sizeof(char),
"%4.4X%4.4X",
*(unsigned short *)str,
*(unsigned short *)(str+2));
// 如果采用unicode读取失败,就在用mult char在读取一次
result = GetInfoStr(buf, szLang1, "ProductName", tempbuf);
if (! result) {
ZeroMemory(szLang1, 20);
sprintf_s(szLang1, sizeof(szLang1)/sizeof(char),
"%4.4X%4.4X",
*(unsigned short *)str,
0x4E4);
if (GetInfoStr(buf, szLang1, "ProductName", tempbuf)) {
mProductName = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
} else {
mProductName = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
if (GetInfoStr(buf, szLang1, "FileDescription", tempbuf)) {
mFileDescription = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
if (GetInfoStr(buf, szLang1, "CompanyName", tempbuf)) {
mCompanyName = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
if (GetInfoStr(buf, szLang1, "LegalCopyRight", tempbuf)) {
mCopyRight = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
if (GetInfoStr(buf, szLang1, "OriginalFileName", tempbuf)) {
mOriginName = tempbuf;
ZeroMemory(tempbuf, LINE_BUFFER_SIZE);
}
string signstr = QueryDigSignature();
delete[] buf;
return true;
}
string dolProductInfo::GetFileType(DWORD pdwFileType, string &pszFileType)
{
const int count = 7;
KEY_VALUE FilesTypes[count] = {
{ VFT_UNKNOWN, "Unkown" },
{ VFT_APP, "Application" },
{ VFT_DLL, "Dynamic-Link Library" },
{ VFT_DRV, "Device Driver" },
{ VFT_FONT,"Font" },
{ VFT_VXD, "Virtual Device" },
{ VFT_STATIC_LIB, "Static-Link Library" }
};
return KeyValueToStr(FilesTypes, count, pdwFileType, pszFileType);
}
string dolProductInfo::GetFilePath() const
{
return mFilePath;
}
void dolProductInfo::SetFilePath(const string &pFilePath)
{
mFilePath = pFilePath;
}
string dolProductInfo::GetFileDescription() const
{
return mFileDescription;
}
void dolProductInfo::SetFileDescription(const string &pFileDescription)
{
mFileDescription = pFileDescription;
}
string dolProductInfo::GetDigSignature() const
{
return mDigSignature;
}
void dolProductInfo::SetDigSignature(const string &pDigSignature)
{
mDigSignature = pDigSignature;
}
string dolProductInfo::GetType() const
{
return mType;
}
看了代码,觉得有用的。github可以start下。关注微信公众号获取更多源码。