命令行工具转储Windows DLL版本?
-
03-07-2019 - |
题
我需要一个命令行工具来转储标准的Windows DLL版本信息,以便我可以通过bash脚本(Cygwin)处理它。
作为一名Java开发人员,我不太熟悉Microsoft开发工具(尽管我对Microsoft Visual Embedded C ++ 4.0和Microsoft Visual Basic 6.0有一点经验。)
适当的工具似乎是 mt.exe ,因为<在SO 上说明了一个href =“https://stackoverflow.com/questions/420852/reading-an-applications-manifest-file”>。然而,我发现获得这个小应用程序的唯一机会是下载1.29 GB ISO 适用于Windows Server 2008和.NET Framework的Windows SDK 。我无法相信这是唯一的方法。
我还在互联网上找到了一个名为 PEView 的小应用程序,但它显示太多(在我的情况下没用)信息,它不是命令行应用程序。
Cygwin中捆绑的标准 objdump 也可以转储有关DLL文件的一些信息,但我可以没有看到转储DLL版本的选项。请注意,此工具转储的MajorImageVersion,MinorImageVersion和其他字段(使用-p选项)与自己的DLL版本无关。
关于该怎么做的任何替代方案?也许我错过了一些重要的objdump选项? mt.exe是我唯一的选择吗?如果是这种情况,是否可以单独从Windows SDK获取它?
解决方案
您还可以查看filever.exe,它可以作为 Windows XP SP2支持工具包 - 仅下载4.7MB。
其他提示
您可以使用PowerShell获取所需的信息。
(Get-Item C:\Path\To\MyFile.dll).VersionInfo
默认情况下,这将显示ProductVersion和FileVersion 但完整的 VERSIONINFO 可用。即返回评论
(Get-Item C:\Path\To\MyFile.dll).VersionInfo.Comments
您可以编写VBScript脚本来获取文件版本信息:
<强> VersionInfo.vbs 强>
set args = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.GetFileVersion(args(0))
Wscript.Quit
您可以从命令行中调用此方法:
cscript //nologo VersionInfo.vbs C:\Path\To\MyFile.dll
或者你可以自己建造一个。打开VS,创建一个新的控制台应用程序。创建一个没有ATL或MFC支持的简单项目,保持选中stdafx选项但不检查'empty project'并将其命名为VersionInfo。
您将获得一个包含2个文件的简单项目:VersionInfo.cpp和VersionInfo.h
打开cpp文件并将以下内容粘贴到其中,然后进行编译。您将能够运行它,第一个参数是完整文件名,它将根据版本资源块打印出&quot; Product:5.6.7.8 File:1.2.3.4&quot; 。如果没有版本资源,它将返回-1,否则返回0。
使用dll CRT编译为8k二进制文件,使用静态链接的所有内容编译为60k(在C ++选项中设置,更改“代码生成页面,运行时选项”到“/ MT”)
HTH。
PS。如果您不想使用Visual Studio,它仍然可以使用任何c ++编译器进行编译(手指交叉),但您几乎肯定必须更改#pragma - 只需在链接器设置中指定lib,pragma的只是一个自动链接到该库的简写。
// VersionInfo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#pragma comment(lib, "version.lib")
int _tmain(int argc, _TCHAR* argv[])
{
DWORD handle = 0;
DWORD size = GetFileVersionInfoSize(argv[1], &handle);
BYTE* versionInfo = new BYTE[size];
if (!GetFileVersionInfo(argv[1], handle, size, versionInfo))
{
delete[] versionInfo;
return -1;
}
// we have version information
UINT len = 0;
VS_FIXEDFILEINFO* vsfi = NULL;
VerQueryValue(versionInfo, L"\\", (void**)&vsfi, &len);
WORD fVersion[4], pVersion[4];
fVersion[0] = HIWORD(vsfi->dwFileVersionMS);
fVersion[1] = LOWORD(vsfi->dwFileVersionMS);
fVersion[2] = HIWORD(vsfi->dwFileVersionLS);
fVersion[3] = LOWORD(vsfi->dwFileVersionLS);
pVersion[0] = HIWORD(vsfi->dwProductVersionMS);
pVersion[1] = LOWORD(vsfi->dwProductVersionMS);
pVersion[2] = HIWORD(vsfi->dwProductVersionLS);
pVersion[3] = LOWORD(vsfi->dwProductVersionLS);
printf("Product: %d.%d.%d.%d File: %d.%d.%d.%d\n",
pVersion[0], pVersion[1],
pVersion[2], pVersion[3],
fVersion[0], fVersion[1],
fVersion[2], fVersion[3]);
delete[] versionInfo;
return 0;
}
C:\>wmic datafile where name="C:\\Windows\\System32\\kernel32.dll" get version
Version
6.1.7601.18229
Systernals的listdlls工具可以完成这项工作: http:// technet。 microsoft.com/en-us/sysinternals/bb896656.aspx
listdlls -v -d mylib.dll
此函数返回使用Cygwin bash(实际r-click-properties-info)到术语
的任何文件的ntfs Windows文件详细信息将文件路径传递给finfo(),可以是unix路径,dos路径,相对或绝对路径。该文件将转换为绝对nix路径,然后检查它是否实际上是常规/现有文件。然后转换为绝对窗口路径并发送到“wmic”。然后魔法,你在终端有Windows文件的详细信息。用途:cygwin,cygpath,sed和awk。需要Windows WMI“wmic.exe”运作。输出已经过修正,很容易......
$ finfo notepad.exe
$ finfo "C:\windows\system32\notepad.exe"
$ finfo /cygdrive/c/Windows/System32/notepad.exe
$ finfo "/cygdrive/c/Program Files/notepad.exe"
$ finfo ../notepad.exe
finfo() {
[[ -e "$(cygpath -wa "$@")" ]] || { echo "bad-file"; return 1; }
echo "$(wmic datafile where name=\""$(echo "$(cygpath -wa "$@")" | sed 's/\\/\\\\/g')"\" get /value)" |\
sed 's/\r//g;s/^M$//;/^$/d' | awk -F"=" '{print $1"=""\033[1m"$2"\033[0m" }'
}
使用Powershell可以使用以下命令从任何dll或exe获取Version字符串,即2.3.4
(Get-Item "C:\program files\OpenVPN\bin\openvpn.exe").VersionInfo.ProductVersion
在Windows 10上测试
有一个名为“ShowVer”的命令行应用程序。在CodeProject:
像往常一样,应用程序附带了exe和源代码(VisualC ++ 6)。
Out输出所有可用的元数据:
在德国Win7系统上,user32.dll的输出如下:
VERSIONINFO for file "C:\Windows\system32\user32.dll": (type:0)
Signature: feef04bd
StrucVersion: 1.0
FileVersion: 6.1.7601.17514
ProductVersion: 6.1.7601.17514
FileFlagsMask: 0x3f
FileFlags: 0
FileOS: VOS_NT_WINDOWS32
FileType: VFT_DLL
FileDate: 0.0
LangID: 040704B0
CompanyName : Microsoft Corporation
FileDescription : Multi-User Windows USER API Client DLL
FileVersion : 6.1.7601.17514 (win7sp1_rtm.101119-1850)
InternalName : user32
LegalCopyright : ® Microsoft Corporation. Alle Rechte vorbehalten.
OriginalFilename : user32
ProductName : Betriebssystem Microsoft« Windows«
ProductVersion : 6.1.7601.17514
Translation: 040704b0
和 makecab
的一种方式:
; @echo off
;;goto :end_help
;;setlocal DsiableDelayedExpansion
;;;
;;;
;;; fileinf /l list of full file paths separated with ;
;;; fileinf /f text file with a list of files to be processed ( one on each line )
;;; fileinf /? prints the help
;;;
;;:end_help
; REM Creating a Newline variable (the two blank lines are required!)
; set NLM=^
; set NL=^^^%NLM%%NLM%^%NLM%%NLM%
; if "%~1" equ "/?" type "%~f0" | find ";;;" | find /v "find" && exit /b 0
; if "%~2" equ "" type "%~f0" | find ";;;" | find /v "find" && exit /b 0
; setlocal enableDelayedExpansion
; if "%~1" equ "/l" (
; set "_files=%~2"
; echo !_files:;=%NL%!>"%TEMP%\file.paths"
; set _process_file="%TEMP%\file.paths"
; goto :get_info
; )
; if "%~1" equ "/f" if exist "%~2" (
; set _process_file="%~2"
; goto :get_info
; )
; echo incorect parameters & exit /b 1
; :get_info
; set "file_info="
; makecab /d InfFileName=%TEMP%\file.inf /d "DiskDirectory1=%TEMP%" /f "%~f0" /f %_process_file% /v0>nul
; for /f "usebackq skip=4 delims=" %%f in ("%TEMP%\file.inf") do (
; set "file_info=%%f"
; echo !file_info:,=%nl%!
; )
; endlocal
;endlocal
; del /q /f %TEMP%\file.inf 2>nul
; del /q /f %TEMP%\file.path 2>nul
; exit /b 0
.set DoNotCopyFiles=on
.set DestinationDir=;
.set RptFileName=nul
.set InfFooter=;
.set InfHeader=;
.Set ChecksumWidth=8
.Set InfDiskLineFormat=;
.Set Cabinet=off
.Set Compress=off
.Set GenerateInf=ON
.Set InfDiskHeader=;
.Set InfFileHeader=;
.set InfCabinetHeader=;
.Set InfFileLineFormat=",file:*file*,date:*date*,size:*size*,csum:*csum*,time:*time*,vern:*ver*,vers:*vers*,lang:*lang*"
示例输出(它有一个字符串版本,这是对wmic方法的一个小补充:)):
c:> fileinfo.bat /l C:\install.exe
file:install.exe
date:11/07/07
size:562688
csum:380ef239
time:07:03:18a
vern:9.0.21022.8
vers:9.0.21022.8 built by: RTM
lang:1033
还有一个使用 shell.application 和混合批处理\ jscript.Here的 tooptipInfo.bat :
@if (@X)==(@Y) @end /* JScript comment
@echo off
rem :: the first argument is the script name as it will be used for proper help message
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
//////
FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var ARGS = WScript.Arguments;
if (ARGS.Length < 1 ) {
WScript.Echo("No file passed");
WScript.Quit(1);
}
var filename=ARGS.Item(0);
var objShell=new ActiveXObject("Shell.Application");
/////
//fso
ExistsItem = function (path) {
return FSOObj.FolderExists(path)||FSOObj.FileExists(path);
}
getFullPath = function (path) {
return FSOObj.GetAbsolutePathName(path);
}
//
//paths
getParent = function(path){
var splitted=path.split("\\");
var result="";
for (var s=0;s<splitted.length-1;s++){
if (s==0) {
result=splitted[s];
} else {
result=result+"\\"+splitted[s];
}
}
return result;
}
getName = function(path){
var splitted=path.split("\\");
return splitted[splitted.length-1];
}
//
function main(){
if (!ExistsItem(filename)) {
WScript.Echo(filename + " does not exist");
WScript.Quit(2);
}
var fullFilename=getFullPath(filename);
var namespace=getParent(fullFilename);
var name=getName(fullFilename);
var objFolder=objShell.NameSpace(namespace);
var objItem=objFolder.ParseName(name);
//https://msdn.microsoft.com/en-us/library/windows/desktop/bb787870(v=vs.85).aspx
WScript.Echo(fullFilename + " : ");
WScript.Echo(objFolder.GetDetailsOf(objItem,-1));
}
main();
用于cmd.exe:
C:\Windows\System32\cmd.exe :
File description: Windows Command Processor
Company: Microsoft Corporation
File version: 6.3.9600.16384
Date created: ?22-?Aug-?13 ??13:03
Size: 347 KB