Sign in to follow this  
Followers 0
erifash

Proposed UDF: _ProcessGetName()

9 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Why not use the Windows API functions OpenProcess(), GetModuleBaseName() and CloseHandle()?

Edit: Or GetModuleFileName() to get the full path.

Edited by Valik

Share this post


Link to post
Share on other sites

Why not use the Windows API functions OpenProcess(), GetModuleBaseName() and CloseHandle()?

Edit: Or GetModuleFileName() to get the full path.

YES please!

Can I use this as an API call ?!?

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0