Jump to content

Proposed UDF: _ProcessGetName()


erifash
 Share

Recommended Posts

I have submitted this function to the Standard UDF Library and Jos.

I have created a function that will get a process name out of a PID. It is called _ProcessGetName(). This function is useful when you use WinGetProcess() since it only returns a PID. I am thinking about submitting this to the Standard UDF Library but I need someone to review it. Here is the function documentation:

;===============================================================================
;
; Description -   Returns a string containing the process name that belongs to a given PID.
;
; Syntax -        _ProcessGetName( $iPID )
;
; Parameters -    $iPID - The PID of a currently running process
;
; Requirements -  None.
;
; Return Values - Success - The name of the process
;                 Failure - Blank string and sets @error
;                       1 - Process doesn't exist
;                       2 - Error getting process list
;                       3 - No processes found
;
; Author(s) -     Erifash <erifash [at] gmail [dot] com>, Wouter van Kesteren.
;
; Notes -         Supplementary to ProcessExists().
;
;===============================================================================

The UDF is attached. If anyone is interested please tell me what you think of it. Thanks! B)

process.au3

Edited by erifash
Link to comment
Share on other sites

Okay, I fixed a small variable error. Does anyone think it could be useful? Here is an example:

Run("notepad.exe")
WinWaitActive("Untitled - Notepad", "")
$pid = WinGetProcess("Untitled - Notepad", "")
$name = _ProcessGetName($pid)

MsgBox(0, "Notepad - " & $pid, $name)
Link to comment
Share on other sites

looks good, modded it if you dont mind B)

Func _ProcessGetNameE($iPID)
    If Not ProcessExists($iPID) Then
        SetError(1)
        Return ""
    EndIf
    Local $aProcesses = ProcessList(), $sProcess = ""
    If @error Or Not IsArray($aProcesses) Then
        SetError(2)
        Return ""
    EndIf
    For $i = 1 To $aProcesses[0][0]
        If $aProcesses[$i][1] = $iPID Then
            $sProcess = $aProcesses[$i][0]
            ExitLoop
        EndIf
    Next
    If $sProcess = "" Then SetError(3)
    Return $sProcess
EndFunc  ;==>_ProcessGetNameE

Func _ProcessGetNameW($i_PID)
    If Not ProcessExists($i_PID) Then
        SetError(1)
        Return ''
    EndIf
    Local $a_Processes = ProcessList()
    If Not @error Then
        For $i = 1 To $a_Processes[0][0]
            If $a_Processes[$i][1] = $i_PID Then Return $a_Processes[$i][0]
        Next
    EndIf
    SetError(1)
    Return ''
EndFunc  ;==>_ProcessGetNameW

ProcessSetPriority(@autoitpid, 5)
$i_PID = Run("notepad.exe")
ProcessWait($i_PID)

Bench($i_PID);benchmarks a real pid
Bench(-1);benchmarks a fake pid

ProcessClose($i_PID)

Func Bench($pid)
    $ea = 0
    $wa = 0
    $wouter = 0
    For $i = 1 To 1000
        $e = TimerInit()
        $name = _ProcessGetNameE($pid)
        $e = TimerDiff($e)
        $w = TimerInit()
        $name = _ProcessGetNameW($pid)
        $w = TimerDiff($w)
        If $e > $w Then $wouter += 1
        $ea += $e
        $wa += $w
    Next
    
    ConsoleWrite('Wouter:  ' & $wouter & ' - ' & $wa & @LF)
    ConsoleWrite('Erifash: ' & 1000 - $wouter & ' - ' & $ea & @CRLF)
EndFunc  ;==>Bench
Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 6 years later...
  • Moderators

RickyMartin,

Welcome to the AutoIt forum. :oops:

Did you notice that this thread has been dormant for over 7 years? We do not encourage necroing threads as old as that - the language has developed so much since then that any code is virtually useless. For example, _ProcessGetName is now part of the standard AutoIt UDF library - look under "Process Management". :doh:

Please do not resurrect any more old threads like this in the future. :bye:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...