windows - 读取正在运行的进程的文件版本

标签 windows process version autoit

我正在收集有关正在运行的进程的信息(例如 CPU 和 RAM 使用情况)。现在我想通过 PID 或进程名称获取进程的版本号。我怎样才能做到这一点?

最佳答案

版本号假设您确定 PID 后面的 exe 及其路径(以便用它调用 FileGetVersion() )。

This thread讨论如何做到这一点,并提出 following script :

#include <Array.au3>

$aProcListEx = _ProcessListEx()
$yourExe = "jqs.exe"

If @error Then
    MsgBox(48, "_ProcessListEx - Error", StringFormat("There was an error to get ProcessList (@error = %i)", @error))
Else
 For $i = 1 to $aProcListEx[0][0]
 If $aProcListEx[$i][0] = $yourExe Then
     $Version = FileGetVersion($aProcListEx[$i][4],"FileVersion")
     MsgBox(0,"","Path to '" & $YourExe & "' is '" & $aProcListEx[$i][5] & "'" & @CRLF & "File Version is: " & $Version)
 EndIf
 Next
EndIf

;===============================================================================
;
; Function Name:    _ProcessListEx()
;
; Function Description: Gets Process List with extended info, plus can retrieve only a processes with specific resources strings.
;
; Parameter(s):     $sResourceName [Optional] - Resource name of the process filename, i.e. "CompiledScript".
;   $sInResString [Optional] - String to check in the resource name.
;   $iWholeWord [Optional] - Defines if the $sInResString will be compared as whole string (default is 1).
;
; Requirement(s):   None.
;
; Return Value(s):  On Success - Return 2-dimentional array, where:
;   $aRet_List[0][0] = Total processes (array elements).
;   $aRet_List[N][0] = Process Name.
;   $aRet_List[N][6] = PID (Process ID).
;   $aRet_List[N][7] = Process File Path.
;   On Failure - Return '' (empty string) and set @error to:
;   1 - Unable to Open Kernel32.dll.
;   2 - Unable to Open Psapi.dll.
;   3 - No Processes Found.
;
; Author(s):    G.Sandler (a.k.a MrCreatoR) - CreatoR's Lab (http://creator-lab.ucoz.ru)
;
;=====================================================================
Func _ProcessListEx($sResourceName="", $sInResString="", $iWholeWord=1)
    Local $aProcList = ProcessList()
    Local $hKernel32_Dll = DllOpen('Kernel32.dll'), $hPsapi_Dll = DllOpen('Psapi.dll')
    Local $aOpenProc, $aProcPath, $sFileVersion, $aRet_List[1][8]

    If $hKernel32_Dll = -1 Then Return SetError(1, 0, '')

    If $hPsapi_Dll = -1 Then $hPsapi_Dll = DllOpen(@SystemDir & '\Psapi.dll')
    If $hPsapi_Dll = -1 Then $hPsapi_Dll = DllOpen(@WindowsDir & '\Psapi.dll')
    If $hPsapi_Dll = -1 Then Return SetError(2, 0, '')

    Local $vStruct  = DllStructCreate('int[1024]')
    Local $pStructPtr = DllStructGetPtr($vStruct)
    Local $iStructSize = DllStructGetSize($vStruct)

    For $i = 1 To UBound($aProcList)-1
    $aOpenProc = DllCall($hKernel32_Dll, 'hwnd', 'OpenProcess', _
    'int', BitOR(0x0400, 0x0010), 'int', 0, 'int', $aProcList[$i][9])

    If Not IsArray($aOpenProc) Or Not $aOpenProc[0] Then ContinueLoop

    DllCall($hPsapi_Dll, 'int', 'EnumProcessModules', _
    'hwnd', $aOpenProc[0], _
    'ptr', $pStructPtr, _
    'int', $iStructSize, _
    'int_ptr', 0)

    $aProcPath = DllCall($hPsapi_Dll, 'int', 'GetModuleFileNameEx', _
    'hwnd', $aOpenProc[0], _
    'int', DllStructGetData($vStruct, 1), _
    'str', '', _
    'int', 2048)

    If Not IsArray($aProcPath) Or StringLen($aProcPath[3]) = 0 Then ContinueLoop

    $sFileVersion = FileGetVersion($aProcPath[3], $sResourceName)

    If $sResourceName = "" Or $sFileVersion = $sInResString Or _
    ($iWholeWord = 0 And StringInStr($sFileVersion, $sInResString)) Then

    $aRet_List[0][0] += 1
    ReDim $aRet_List[$aRet_List[0][0]+1][3]
    $aRet_List[$aRet_List[0][0]][0] = $aProcList[$i][0]     ;Process Name
    $aRet_List[$aRet_List[0][0]][10] = $aProcList[$i][11]     ;PID (Process ID)
    $aRet_List[$aRet_List[0][0]][12] = $aProcPath[3]     ;Process File Path
    EndIf
    Next

    DllClose($hKernel32_Dll)
    DllClose($hPsapi_Dll)

    If $aRet_List[0][0] < 1 Then Return SetError(3, 0, '')
    Return $aRet_List
EndFunc

关于windows - 读取正在运行的进程的文件版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4690264/

相关文章:

c - 如何正确使用 fork() 和 exec()

visual-studio - 当特定版本为 False 时特定版本的程序集引用

c++ - 获取USB序列号c++

基于 Windows 的文本编辑器

c++ - 在 Linux 上用 C++ 从文件打开进程

javascript - process.on ('uncaughtException' 的最佳用途是什么,...)?

c++ - 如何在构造函数中正确初始化 XMVECTOR?

regex - JScript 正则表达式 : start of line doesn't work?

java - 强制使用 "java -version:<value>"运行 Java 最低版本在 Windows 上不起作用

gcc - 强制使用 cmake 提供的最新 gcc