Win32 获取EXE/DLL文件版本信息

CFileVersion.h

#pragma once#include <windows.h>
#include <string>
#include <tchar.h>#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif// 版本号辅助类
class CVersionNumber
{
public:CVersionNumber() :m_nVer{ 0 } {};// 从版本字符串构造CVersionNumber(LPCTSTR lpStrVer) :m_nVer{ 0 }{_stscanf_s(lpStrVer, _T("%hd.%hd.%hd.%hd"), &m_nVer[0], &m_nVer[1], &m_nVer[2], &m_nVer[3]);}// 从指定版本号构造CVersionNumber(WORD v1, WORD v2, WORD v3, WORD v4) :m_nVer{ v1, v2, v3, v4 }{}// 从版本字符串构造CVersionNumber& operator = (LPCTSTR lpStrVer){_stscanf_s(lpStrVer, _T("%hd.%hd.%hd.%hd"), &m_nVer[0], &m_nVer[1], &m_nVer[2], &m_nVer[3]);return *this;}int CompareVersion(const CVersionNumber& ref){for (int i = 0; i < _countof(m_nVer); i++){if (m_nVer[i] != ref.m_nVer[i]){return (m_nVer[i] > ref.m_nVer[i] ? 1 : -1);}}return 0;}bool operator < (const CVersionNumber& ref){return CompareVersion(ref) < 0;}bool operator == (const CVersionNumber& ref){return CompareVersion(ref) == 0;}bool operator > (const CVersionNumber& ref){return CompareVersion(ref) > 0;}private:WORD m_nVer[4];         //版本号
};typedef struct _FILE_VERSION_INFO
{_tstring strComments;                       //文件注释_tstring strInternalName;                   //内部名称_tstring strProductName;                    //产品名称_tstring strCompanyName;                    //公司名称_tstring strLegalCopyright;                 //法律版权_tstring strProductVersion;                 //产品版本_tstring strFileDescription;                //文件描述_tstring strLegalTrademarks;                //合法商标_tstring strPrivateBuild;                   //私有构建_tstring strFileVersion;                    //文件版本_tstring strOriginalFilename;               //原始文件名_tstring strSpecialBuild;                   //特殊构建_tstring strFileVersionEx;                  //文件版本(从 VS_FIXEDFILEINFO中 获取)_tstring strProductVersionEx;               //产品版本(从 VS_FIXEDFILEINFO中 获取)CVersionNumber FileVerNumber;               //文件版本号CVersionNumber ProductVerNumber;            //产品版本号
}FILE_VERSION_INFO;class CFileVersion
{struct LANGANDCODEPAGE {WORD wLanguage;WORD wCodePage;LANGANDCODEPAGE():wLanguage(0),wCodePage(0){}};public:CFileVersion();~CFileVersion();CFileVersion operator = (const CFileVersion& r) = delete;static FILE_VERSION_INFO GetVersionInfo(const _tstring& strFile);static FILE_VERSION_INFO GetCurrentModuleVersionInfo();public:bool LoadFile(const _tstring& strFile);             //加载文件信息void Close();FILE_VERSION_INFO GetVersionInfo() const;           //整个版本信息_tstring GetComments() const;                       //文件注释_tstring GetInternalName() const;                   //内部名称_tstring GetProductName() const;                    //产品名称_tstring GetCompanyName() const;                    //公司名称_tstring GetLegalCopyright() const;                 //法律版权_tstring GetProductVersion() const;                 //产品版本_tstring GetFileDescription() const;                //文件描述_tstring GetLegalTrademarks() const;                //合法商标_tstring GetPrivateBuild() const;                   //私有版本_tstring GetFileVersion() const;                    //文件版本_tstring GetOriginalFilename() const;               //原始文件名_tstring GetSpecialBuild() const;                   //特别版本_tstring GetFileVersionEx() const;                  //文件版本(从 VS_FIXEDFILEINFO中 获取)_tstring GetProductVersionEx() const;               //产品版本(从 VS_FIXEDFILEINFO中 获取)VS_FIXEDFILEINFO GetFixedFileInfo() const;          //文件的版本信息private:_tstring _QueryInfo(const _tstring& strName) const;void _LoadAllInfo();LPVOID m_lpVerData;                                 //文件信息数据指针LANGANDCODEPAGE m_Translate;                        //语言代码页VS_FIXEDFILEINFO m_VsFixedFileInfo;                 //文件的版本信息FILE_VERSION_INFO m_VersionInfo;                    //版本信息
};

CFileVersion.cpp

#include "CFileVersion.h"
#include <tchar.h>
#include <strsafe.h>#pragma comment(lib, "Version.lib")typedef struct _FILE_VERSION_NUMBER
{WORD m_nVer[4];_FILE_VERSION_NUMBER() :m_nVer{0} {};_FILE_VERSION_NUMBER(LPCTSTR lpStrVer) :m_nVer{0}{_stscanf_s(lpStrVer, _T("%hd.%hd.%hd.%hd"),&m_nVer[0],&m_nVer[1],&m_nVer[2],&m_nVer[3]);}_FILE_VERSION_NUMBER& operator = (LPCTSTR lpStrVer){_stscanf_s(lpStrVer, _T("%hd.%hd.%hd.%hd"),&m_nVer[0],&m_nVer[1],&m_nVer[2],&m_nVer[3]);return *this;}int CompareVersion(const _FILE_VERSION_NUMBER& ref){for (int i = 0; i < _countof(m_nVer); i++){if (m_nVer[i] != ref.m_nVer[i]){return (m_nVer[i] > ref.m_nVer[i] ? 1 : -1);}}return 0;}bool operator < (const _FILE_VERSION_NUMBER& ref){return CompareVersion(ref) < 0;}bool operator == (const _FILE_VERSION_NUMBER& ref){return CompareVersion(ref) == 0;}bool operator > (const _FILE_VERSION_NUMBER& ref){return CompareVersion(ref) > 0;}}FILE_VERSION_NUMBER;#pragma pack(push)
#pragma pack(1)
// 包含文件的版本信息。 此信息与语言和代码页无关
// https://learn.microsoft.com/zh-cn/windows/win32/menurc/vs-versioninfo
typedef struct {WORD             wLength;       // VS_VERSIONINFO 结构的长度(以字节为单位),此长度不包括在 32 位边界上对齐任何后续版本资源数据的填充WORD             wValueLength;  // Value 成员的长度(以字节为单位)WORD             wType;         // 版本资源中的数据类型, 1: 资源包含文本数据 0: 版本资源包含二进制数据WCHAR            szKey[15];     // Unicode 字符串 L“VS_VERSION_INFO”WORD             Padding1;      // 在 32 位边界上对齐 Children 成员所需的任意或零个 WORD//VS_FIXEDFILEINFO Value//WORD             Padding2//WORD             Children
} VS_VERSIONINFO, * PVS_VERSIONINFO;#pragma pack(pop)CFileVersion::CFileVersion():m_lpVerData(NULL)
{memset(&m_VsFixedFileInfo, 0, sizeof(m_VsFixedFileInfo));
}CFileVersion::~CFileVersion()
{this->Close();
}FILE_VERSION_INFO CFileVersion::GetVersionInfo(const _tstring& strFile)
{CFileVersion verInfo;verInfo.LoadFile(strFile);return verInfo.GetVersionInfo();
}FILE_VERSION_INFO CFileVersion::GetCurrentModuleVersionInfo()
{TCHAR szBuf[MAX_PATH] = { 0 };::GetModuleFileName(NULL, szBuf, _countof(szBuf));return GetVersionInfo(szBuf);
}bool CFileVersion::LoadFile(const _tstring& strFile)
{PVOID pFsRedirectionOldValue = NULL;bool isDisableWow64Fs = false;bool isSuccess = false;UINT cbTranslate = 0;DWORD dwVerSize;if (strFile.empty()){return false;}this->Close();m_VersionInfo = FILE_VERSION_INFO();// 禁用文件重定向isDisableWow64Fs = ::Wow64DisableWow64FsRedirection(&pFsRedirectionOldValue);do{// 获取版本信息数据大小dwVerSize = ::GetFileVersionInfoSize(strFile.c_str(), 0);if (0 == dwVerSize){break;}// 分配版本信息缓冲m_lpVerData = ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, dwVerSize);if (!m_lpVerData){break;}// 获取版本信息if (!::GetFileVersionInfo(strFile.c_str(), 0, dwVerSize, m_lpVerData)){break;}// 获取语言代码LANGANDCODEPAGE* lpTranslate = NULL;if (!::VerQueryValue(m_lpVerData, _T("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate)){break;}m_Translate = *lpTranslate;// 获取 VS_FIXEDFILEINFO 信息PVS_VERSIONINFO lpVersion = (PVS_VERSIONINFO)m_lpVerData;if (0 != lpVersion->wValueLength){VS_FIXEDFILEINFO* pFixedFileInfo = (VS_FIXEDFILEINFO*)((LPBYTE)lpVersion + sizeof(VS_VERSIONINFO));DWORD dwAlign = 4;DWORD_PTR dwPadding = ((DWORD_PTR)pFixedFileInfo % dwAlign);if (0 != dwPadding){pFixedFileInfo = (VS_FIXEDFILEINFO*)((LPBYTE)pFixedFileInfo + (dwAlign - dwPadding));}m_VsFixedFileInfo = *pFixedFileInfo;}// 加载所有信息_LoadAllInfo();isSuccess = true;} while (false);// 不成功则关闭if (!isSuccess){this->Close();}// 恢复文件重定向if (isDisableWow64Fs){::Wow64RevertWow64FsRedirection(pFsRedirectionOldValue);}return isSuccess;
}void CFileVersion::Close()
{if (m_lpVerData){HeapFree(::GetProcessHeap(), 0, m_lpVerData);m_lpVerData = NULL;}
}void CFileVersion::_LoadAllInfo()
{m_VersionInfo.strComments = _QueryInfo(_T("Comments"));m_VersionInfo.strInternalName = _QueryInfo(_T("InternalName"));m_VersionInfo.strProductName = _QueryInfo(_T("ProductName"));m_VersionInfo.strCompanyName = _QueryInfo(_T("CompanyName"));m_VersionInfo.strLegalCopyright = _QueryInfo(_T("LegalCopyright"));m_VersionInfo.strProductVersion = _QueryInfo(_T("ProductVersion"));m_VersionInfo.strFileDescription = _QueryInfo(_T("FileDescription"));m_VersionInfo.strLegalTrademarks = _QueryInfo(_T("LegalTrademarks"));m_VersionInfo.strPrivateBuild = _QueryInfo(_T("PrivateBuild"));m_VersionInfo.strFileVersion = _QueryInfo(_T("FileVersion"));m_VersionInfo.strOriginalFilename = _QueryInfo(_T("OriginalFilename"));m_VersionInfo.strSpecialBuild = _QueryInfo(_T("SpecialBuild"));m_VersionInfo.strFileVersionEx = GetFileVersionEx();m_VersionInfo.strProductVersionEx = GetProductVersionEx();m_VersionInfo.FileVerNumber = m_VersionInfo.strFileVersionEx.c_str();m_VersionInfo.ProductVerNumber = m_VersionInfo.strProductVersionEx.c_str();}_tstring CFileVersion::_QueryInfo(const _tstring& strName) const
{_tstring strRes;TCHAR strQuery[MAX_PATH] = { 0 };LPCTSTR lpQueryRes = NULL;UINT uQueryCchSize;do{if (!m_lpVerData){break;}(void)::StringCchPrintf(strQuery, _countof(strQuery),_T("\\StringFileInfo\\%04x%04x\\%s"),m_Translate.wLanguage,m_Translate.wCodePage,strName.c_str());if (!::VerQueryValue(m_lpVerData, strQuery, (LPVOID*)&lpQueryRes, &uQueryCchSize)){break;}strRes = lpQueryRes;} while (false);return strRes;
}_tstring CFileVersion::GetComments() const
{return m_VersionInfo.strComments;
}_tstring CFileVersion::GetInternalName() const
{return m_VersionInfo.strInternalName;
}_tstring CFileVersion::GetProductName() const
{return m_VersionInfo.strProductName;
}_tstring CFileVersion::GetCompanyName() const
{return m_VersionInfo.strCompanyName;
}_tstring CFileVersion::GetLegalCopyright() const
{return m_VersionInfo.strLegalCopyright;
}_tstring CFileVersion::GetProductVersion() const
{return m_VersionInfo.strProductVersion;
}_tstring CFileVersion::GetFileDescription() const
{return m_VersionInfo.strFileDescription;
}_tstring CFileVersion::GetLegalTrademarks() const
{return m_VersionInfo.strLegalTrademarks;
}_tstring CFileVersion::GetPrivateBuild() const
{return m_VersionInfo.strPrivateBuild;
}_tstring CFileVersion::GetFileVersion() const
{return m_VersionInfo.strFileVersion;
}_tstring CFileVersion::GetOriginalFilename() const
{return m_VersionInfo.strOriginalFilename;
}_tstring CFileVersion::GetSpecialBuild() const
{return m_VersionInfo.strSpecialBuild;
}_tstring CFileVersion::GetFileVersionEx() const
{TCHAR szBuf[MAX_PATH] = { 0 };(void)::StringCchPrintf(szBuf, _countof(szBuf), _T("%hd.%hd.%hd.%hd"),HIWORD(m_VsFixedFileInfo.dwFileVersionMS),LOWORD(m_VsFixedFileInfo.dwFileVersionMS),HIWORD(m_VsFixedFileInfo.dwFileVersionLS),LOWORD(m_VsFixedFileInfo.dwFileVersionLS));return szBuf;
}_tstring CFileVersion::GetProductVersionEx() const
{TCHAR szBuf[MAX_PATH] = { 0 };(void)::StringCchPrintf(szBuf, _countof(szBuf), _T("%hd.%hd.%hd.%hd"),HIWORD(m_VsFixedFileInfo.dwProductVersionMS),LOWORD(m_VsFixedFileInfo.dwProductVersionMS),HIWORD(m_VsFixedFileInfo.dwProductVersionLS),LOWORD(m_VsFixedFileInfo.dwProductVersionLS));return szBuf;
}VS_FIXEDFILEINFO CFileVersion::GetFixedFileInfo() const
{return m_VsFixedFileInfo;
}FILE_VERSION_INFO CFileVersion::GetVersionInfo() const
{return m_VersionInfo;
}

main.cpp

#include <locale.h>
#include "Win32Utils/CFileVersion.h"int _tmain(int argc, LPCTSTR argv[])
{setlocale(LC_ALL, "");FILE_VERSION_INFO info = CFileVersion::GetCurrentModuleVersionInfo();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/689377.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【天衍系列 05】Flink集成KafkaSink组件:实现流式数据的可靠传输 高效协同

文章目录 01 KafkaSink 版本&导言02 KafkaSink 基本概念03 KafkaSink 工作原理1.初始化连接2.定义序列化模式3.创建KafkaSink算子4.创建数据源5.将数据流添加到KafkaSink6.内部工作机制 04 KafkaSink参数配置05 KafkaSink 应用依赖06 KafkaSink 快速入门6.1 包结构6.2 项目…

【深度学习:数据注释工具】构建与购买:从业者的经验教训

【深度学习&#xff1a;数据注释工具】构建与购买&#xff1a;从业者的经验教训 为什么数据注释团队需要标记工具&#xff1f;您的软件工程团队是否有时间/资源来构建数据注释解决方案&#xff1f;为您的项目构建数据注释工具需要多长时间&#xff1f;将开发外包给第三方会更有…

SSH密钥认证登陆流程(Vscode连接到远程)

目录 前言连接远程步骤1. 下载工具包wsCli到本地机器2. 本地机器上生成ssh密钥3. 在服务器上安装公钥4. vscode连接到远程 参考资料 前言 SSH&#xff08;Secure Shell&#xff09;是一种用于远程登录和安全传输数据的网络协议。它提供了两种主要的远程连接方式&#xff1a; 密…

有趣的CSS - 新拟态输入框

我是 Just&#xff0c;这里是「设计师工作日常」&#xff0c;《有趣的css》系列已更新 11 篇了&#xff0c;今天这篇是关于新拟态风格的一个输入框效果&#xff0c;希望你们喜欢。 目录 页面效果核心代码html代码css代码 完整代码html页面css样式 页面效果 此效果使用 css 中 b…

PDF控件Spire.PDF for .NET【安全】演示:如何在 PDF 中添加签名字段

Spire.PDF for .NET 是一款独立 PDF 控件&#xff0c;用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库&#xff0c;开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理&#xff0c;且无需安装 Adobe Acrobat。 E-iceblue 功能类库Spire 系列文档处…

PyCharm 新建目录 (directory or folder)

PyCharm 新建目录 [directory or folder] 1. 新建目录2. Enter new directory name -> OKReferences 1. 新建目录 right mouse click on the project -> New -> Directory 2. Enter new directory name -> OK ​​​ References [1] Yongqiang Cheng, https:/…

go redis

go redis 快速入门 安装&#xff1a; go get github.com/redis/go-redis/v9然后创建客户端&#xff1a; package mainimport "github.com/redis/go-redis/v9"func main() {rdb : redis.NewClient(&redis.Options{Addr: "47.109.87.142:6379",Pa…

C++_design_model_observer

/* 观察者模式是一种常用的设计模式&#xff0c;用于在对象之间建立一种一对多的依赖关系&#xff0c;当被观察的对象发生变化时&#xff0c; 所有依赖于它的对象都能够得到通知并自动更新。下面是一个使用C实现观察者模式的例子&#xff1a; */#include <iostream> #inc…

Redis篇----第六篇

系列文章目录 文章目录 系列文章目录前言一、Redis 的持久化机制是什么?各自的优缺点?二、Redis 常见性能问题和解决方案:三、redis 过期键的删除策略?前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章…

如何查看springboot依赖的JDK版本

通过maven构建Java项目或者使用源代码进行Java编译时&#xff0c;常常遇到JDK版本和Springboot版本不匹配的问题&#xff0c;导致编译失败&#xff0c;比如出现如下错误&#xff1a; org/springframework/beans/factory/InitializingBean.class [ERROR] 类文件具有错误的…

Unix I/O 模型及Java I/O 模型详解

在Unix Socket的输入操作中&#xff0c;可以将其分为以下几个阶段&#xff1a; 等待数据就绪(内核空间)&#xff1a; 在这个阶段&#xff0c;应用程序通过调用阻塞式的读取函数&#xff08;如recv&#xff09;或非阻塞式的读取函数&#xff08;如recv、recvfrom&#xff09;等待…

入门级10寸加固行业平板—EM-I10J

亿道信息以其坚固耐用的智能终端设备而闻名&#xff0c;近日发布了一款理想入门级 10 英寸加固平板电脑—I10J。 EM-I10J​​ 这是一款 10 英寸的平板电脑&#xff0c;主要运行 Windows 10操作系统&#xff0c;带有硬化塑料外壳&#xff0c;具有 IP65 防水防尘功能和 MIL-STD 8…

线性dp之石子合并

设有 N堆石子排成一排&#xff0c;其编号为 1,2,3,…,N。 每堆石子有一定的质量&#xff0c;可以用一个整数来描述&#xff0c;现在要将这 N 堆石子合并成为一堆。 每次只能合并相邻的两堆&#xff0c;合并的代价为这两堆石子的质量之和&#xff0c;合并后与这两堆石子相邻的…

注册及搭建小程序开发环境

一、注册小程序账号 打开微信公众平台&#xff0c;通过邮箱注册小程序账号&#xff08;每个邮箱账号&#xff0c;只能注册一个小程序&#xff09;。 注册完成后&#xff0c;登录邮箱&#xff0c;打开激活地址&#xff0c;按照邮件提示&#xff0c;填写信息&#xff0c;激活账号…

Eclipse - 查看工程或者文件的磁盘路径

Eclipse - 查看工程或者文件的磁盘路径 1. Help -> Eclipse Marketplace -> Find: Explorer -> Eclipse Explorer 4.1.0 -> Install2. right-click -> Open in ExplorerReferences 1. Help -> Eclipse Marketplace -> Find: Explorer -> Eclipse Explo…

门店数字化之旅:如何跨越那些难以逾越的鸿沟?

在数字化浪潮席卷全球的背景下&#xff0c;连锁门店运营正面临着一场深刻的变革。这场变革不仅关乎技术的升级&#xff0c;更涉及到商业模式、管理理念以及消费者体验的根本性转变。然而&#xff0c;在这场转型之路上&#xff0c;许多门店却常常遭遇各种痛点&#xff0c;让数字…

2023年总结与2024展望

今天是春节后上班第一天&#xff0c;你懂的&#xff0c;今天基本上是摸鱼状态&#xff0c;早上把我们负责的项目的ppt介绍完善了一下&#xff0c;然后写了一篇技术文章&#xff0c;《分布式系统一致性与共识算法》。接着就看了我近几年写的的年度总结&#xff0c;我一般不会在元…

一个服务器实现本机服务互联网化

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 一个服务器实现本机服务互联网化 前言痛点关于中微子代理实战演练搭建服务端搭建客户端服务端配置代理实现 前言 在数字世界的网络战场上&#xff0c;中微子代理就像是一支潜伏在黑暗中的数字特工队&…

自动化上位机开发C#100例:雷赛运动控制卡EtherCAT总线卡C#封装类

自动化上位机开发C#100例:雷赛运动控制卡EtherCAT总线卡C#封装类 文章目录 LTDMC.dll下载LTDMC.cs LTDMC.dll C#调用封装下载ICard.cs 运动控制卡接口Card.cs 运动控制卡抽象类CardLTDMC.cs 雷赛运动控制卡EtherCAT总线卡实现类CardList.cs 总线卡列表封装 LTDMC.dll下载 最新…

【笔记------STM32】MX_RTC_Init()初始化RTC时RTC_ISR_INITF位超时失败的解决方法

RTC和flash有点像&#xff0c;有些功能需要解锁才能配置&#xff0c;虽然cubeMX生成的RTC部分的解锁配置正确&#xff0c;但却没有配置好前提条件&#xff1a;关闭PWR模块的备份域写保护使能&#xff0c;有点奇怪&#xff0c;手动关掉就好了 现象&#xff1a;进入RTC_EnterInit…