提取内部EXE信息 [英] Extract internal EXE info
问题描述
Windows EXE文件具有一些元数据,例如CompanyName
,FileVersion
,InternalName
,ProductName
,OriginalFileName
,ProductVersion
等.
Windows EXE files have some metadata like CompanyName
, FileVersion
, InternalName
, ProductName
, OriginalFileName
, ProductVersion
, etc.
如何使用PHP提取此类元数据?
How can I extract such metadata from using PHP?
推荐答案
对此我感到好奇,所以我决定编写此函数:
I got curious about this, so I decided to write this function:
function getFileVersionInfo($filename,$encoding='UTF-8'){
$dat = file_get_contents($filename);
if($pos=strpos($dat,mb_convert_encoding('VS_VERSION_INFO','UTF-16LE'))){
$pos-= 6;
$six = unpack('v*',substr($dat,$pos,6));
$dat = substr($dat,$pos,$six[1]);
if($pos=strpos($dat,mb_convert_encoding('StringFileInfo','UTF-16LE'))){
$pos+= 54;
$res = [];
$six = unpack('v*',substr($dat,$pos,6));
while($six[2]){
$nul = strpos($dat,"\0\0\0",$pos+6)+1;
$key = mb_convert_encoding(substr($dat,$pos+6,$nul-$pos-6),$encoding,'UTF-16LE');
$val = mb_convert_encoding(substr($dat,ceil(($nul+2)/4)*4,$six[2]*2-2),$encoding,'UTF-16LE');
$res[$key] = $val;
$pos+= ceil($six[1]/4)*4;
$six = unpack('v*',substr($dat,$pos,6));
}
return $res;
}
}
}
它适用于32位和64位exe.用法示例:
It works with 32-bit and 64-bit exe. Usage example:
echo "<pre>".print_r(getFileVersionInfo('notepad.exe'),1)."</pre>";
echo "<pre>".print_r(getFileVersionInfo('php.exe'),1)."</pre>";
echo "<pre>".print_r(getFileVersionInfo('jre-7u9-windows-x64.exe'),1)."</pre>";
notepad.exe(32位):
notepad.exe (32-bit):
Array
(
[CompanyName] => Microsoft Corporation
[FileDescription] => Notepad
[FileVersion] => 6.1.7600.16385 (win7_rtm.090713-1255)
[InternalName] => Notepad
[LegalCopyright] => © Microsoft Corporation. All rights reserved.
[OriginalFilename] => NOTEPAD.EXE
[ProductName] => Microsoft® Windows® Operating System
[ProductVersion] => 6.1.7600.16385
)
php.exe(32位):
php.exe (32-bit):
Array
(
[Comments] => Thanks to Edin Kadribasic, Marcus Boerger, Johannes Schlueter, Moriyoshi Koizumi, Xinchen Hui
[CompanyName] => The PHP Group
[FileDescription] => CLI
[FileVersion] => 7.0.12
[InternalName] => CLI SAPI
[LegalCopyright] => Copyright © 1997-2016 The PHP Group
[LegalTrademarks] => PHP
[OriginalFilename] => php.exe
[ProductName] => PHP
[ProductVersion] => 7.0.12
[URL] => http://www.php.net
)
jre-7u9-windows-x64.exe(64位):
jre-7u9-windows-x64.exe (64-bit):
Array
(
[CompanyName] => Oracle Corporation
[FileDescription] => Java(TM) Platform SE binary
[FileVersion] => 7.0.90.5
[Full Version] => 1.7.0_09-b05
[InternalName] => Setup Launcher
[LegalCopyright] => Copyright © 2012
[OriginalFilename] => jinstall.exe
[ProductName] => Java(TM) Platform SE 7 U9
[ProductVersion] => 7.0.90.5
)
关于php.exe
的一些有趣信息:Comments
和URL
不会显示在详细信息"选项卡中.
至少在我的电脑上.
Something interesting about php.exe
: the Comments
and URL
don't show up in the Details tab.
At least in my computer.
享受.
更新1:我忘记了错误检查.现在,如果版本信息不存在,它将返回null.
Update 1: I forgot error checking. Now it returns null if the version info doesn't exist.
更新2:非常感谢 @Abela 引起了我的编码问题
Update 2: Many thanks to @Abela for bringing an encoding issue to my attention.
我添加了一个可选的第二个参数,默认为UTF-8,该参数应可用于大多数用途. 如果需要单字节字符输出,请改用ISO-8859-1,如下所示:
I added an optional 2nd parameter that defaults to UTF-8 which should work for most purposes. If you need single-byte-character output, use ISO-8859-1 instead, like this:
getFileVersionInfo('php.exe','ISO-8859-1');
这篇关于提取内部EXE信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!