Qt開源小項目--查看dll,exe的版本信息

Qt開源小項目–查看dll,exe的版本信息

先上圖看看效果:
image

在工作可能經常遇到的對文件的簽名,dll和exe中所帶的信息進行提取和驗證。所以我就
封裝成一個類,以後直接調用了,非常的方便。大家拿到這個類就可以直接使用在項目中。

這裏我就貼出主要的代碼,我會把完整的項目放到github上面。

/****************************************************************************
**
** Copyright (C) 2018 liushixiong ([email protected])
** 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下。關注微信公衆號獲取更多源碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章