Delphi通过GetFileVersionInfo和VerQueryValue等API函数取得详细EXE信息

时间:2022-11-06 17:56:55

This has been described at About:

http://delphi.about.com/cs/adptips2001/a/bltip0701_4.htm
Basically, you just use the GetFileVersionInfo function to obtain the data and then VerQueryValue function to read it.

Because these API functions are a bit 'hard' to use, I have written a simple example:

type
TEXEVersionData = record
CompanyName,
FileDescription,
FileVersion,
InternalName,
LegalCopyright,
LegalTrademarks,
OriginalFileName,
ProductName,
ProductVersion,
Comments,
PrivateBuild,
SpecialBuild: string;
end;

function GetEXEVersionData(const FileName: string): TEXEVersionData;
type
PLandCodepage = ^TLandCodepage;
TLandCodepage = record
wLanguage,
wCodePage: word;
end;
var
dummy,
len: cardinal;
buf, pntr: pointer;
lang: string;
begin
len := GetFileVersionInfoSize(PChar(FileName), dummy);
if len = 0 then
RaiseLastOSError;
GetMem(buf, len);
try
if not GetFileVersionInfo(PChar(FileName), 0, len, buf) then
RaiseLastOSError;

if not VerQueryValue(buf, '\VarFileInfo\Translation\', pntr, len) then
RaiseLastOSError;

lang := Format('%.4x%.4x', [PLandCodepage(pntr)^.wLanguage, PLandCodepage(pntr)^.wCodePage]);

if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\CompanyName'), pntr, len){ and (@len <> nil)} then
result.CompanyName := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\FileDescription'), pntr, len){ and (@len <> nil)} then
result.FileDescription := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\FileVersion'), pntr, len){ and (@len <> nil)} then
result.FileVersion := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\InternalName'), pntr, len){ and (@len <> nil)} then
result.InternalName := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\LegalCopyright'), pntr, len){ and (@len <> nil)} then
result.LegalCopyright := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\LegalTrademarks'), pntr, len){ and (@len <> nil)} then
result.LegalTrademarks := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\OriginalFileName'), pntr, len){ and (@len <> nil)} then
result.OriginalFileName := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\ProductName'), pntr, len){ and (@len <> nil)} then
result.ProductName := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\ProductVersion'), pntr, len){ and (@len <> nil)} then
result.ProductVersion := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\Comments'), pntr, len){ and (@len <> nil)} then
result.Comments := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\PrivateBuild'), pntr, len){ and (@len <> nil)} then
result.PrivateBuild := PChar(pntr);
if VerQueryValue(buf, PChar('\StringFileInfo\' + lang + '\SpecialBuild'), pntr, len){ and (@len <> nil)} then
result.SpecialBuild := PChar(pntr);
finally
FreeMem(buf);
end;
end;
Try it. But beware -- currently, this only works for en-us EXEs! It doesn't work for most of the EXEs on my Swedish machine, for instance. It is late now; tomorrow I will extend this to work with any EXE language, if only I get some time left. [The About.com code has the same problem, but they don't even pretend it is a problem!]

Update: The code now works with any EXE language.

http://*.com/questions/5539316/how-can-i-read-details-of-file

http://*.com/questions/17340794/verqueryvalue-does-not-work-with-single-character-values

http://*.com/questions/6557778/get-fileversion-with-build

Delphi通过GetFileVersionInfo和VerQueryValue等API函数取得详细EXE信息的更多相关文章

  1. Delphi调用API函数获取Windows目录信息、获取System目录信息、获取Temp临时文件目录信息

    var Str1, Str2: Array[..Max_Path]of Char;//开辟缓冲区 Str3: Array[..]of Char; begin GetWindowsDirectory(@ ...

  2. 队列顺序存储 - 设计与实现 - API函数

    队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! queue常用操作 销毁队列 清空队 ...

  3. Delphi常用API,API函数

    auxGetDevCaps API 获取附属设备容量 auxGetNumDevs API 返回附属设备数量 auxGetVolume API 获取当前卷设置 auxOutMessage API 向输出 ...

  4. Delphi获取当前系统时间(使用API函数GetSystemTime)

    在开发应用程序时往往需要获取当前系统时间.尽管Y2K似乎已经平安过去,但在我们新开发的应用程序中还是要谨慎处理“时间”问题. 在<融会贯通--Delphi4.0实战技巧>(以下简称“该书” ...

  5. Delphi内存操作API函数(备查,并一一学习)

    Delphi内存操作API函数System.IsMemoryManagerSet;System.Move;System.New;System.ReallocMem;System.ReallocMemo ...

  6. Delphi 常用API 函数

    Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeIconic ...

  7. Delphi - Windows系统下,Delphi调用API函数和7z&period;dll动态库,自动把文件压缩成&period;tar&period;gz格式的文件

    项目背景 应欧美客户需求,需要将文件压缩成.tar.gz格式的文件,并上传给客户端SFTP服务器. 你懂的,7-Zip软件的显著特点是文件越大压缩比越高,在Linux系统上相当于我们Windows系统 ...

  8. &lbrack;原创&rsqb; Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍

    Delphi Win API函数 操作帮助文件 HtmlHelpA函数介绍 函数原型:HWND HtmlHelpA( HWND hwndCaller, LPCSTR pszFile, UINT uCo ...

  9. Delphi Win API 函数 MulDiv

    Delphi Win API 函数 MulDiv 原型:function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer; st ...

随机推荐

  1. &period;Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...

  2. One Time Auth

    One Time Auth One-time authentication (shortened as OTA) is a new experimental feature designed to i ...

  3. JAVA实现Excel导出数据(以写好的Excel模版导出)

    工作中经常会有将后台数据以Excel导出的功能. 简单的方法有将response的contentType设置为application/vnd.ms-excel: 或在JSP页面直接设置成: <% ...

  4. 整理的sql sever一些数据库查询面试题

    当然,我整理的只是一些常见的面试题,具体数据库就不给了,相信大家能看懂!!! --2列出EMPLOYEES表中各部门的部门号,最高工资,最低工资 select Max(salary) as '最高工资 ...

  5. 源码讲解 node&plus;mongodb 建站攻略(一期)第二节

    源码讲解 node+mongodb 建站攻略(一期)第二节 上一节,我们完成了模拟数据,这次我们来玩儿真正的数据库,mongodb. 代码http://www.imlwj.com/download/n ...

  6. mysql5&period;7在windows下面的主从复制配置

    目标:自动同步Master 服务器上面的Demo数据库到Slave 服务器的Demo数据库中. 对于一些操作系统比较强而使用频率又不高的东西,往往好久不去弄就忘记了,所以要经常记录起来,方便日后查阅. ...

  7. sql serve 数据库游标的使用

      什么是游标? 通俗来讲,个人理解,游标是对一个查询结果集,每次取出一条数据进行处理操作.   使用场景: 例如,我们要修改一个表300条数据,且每条数据修改的内容不一样,那么平时用的update ...

  8. 移动rem自适应

    /** * rem计算方式:设计图尺寸px / 100 = 实际rem 例: 100px = 1rem */!function (window) { /* 设计图文档宽度 */ var docWidt ...

  9. &lbrack;福大软工&rsqb; Z班 评测作业对应表

    学号 测试组号 011500908 8 031501102 3 031501118 8 031502106 6 031502109 9 031502113 3 031502142 2 03150220 ...

  10. Dom4j与sax 简单对比

    Dom4j与sax之间的对比 dom4j不适合大文件的解析,因为它是一下子将文件加载到内存中,所以有可能出现内存溢出,sax是基于事件来对xml进行解析的,所以他可以解析大文件的xml,也正是因为如此 ...