Jump to content
Sign in to follow this  
NunoBorges

ProcessClose and @ ComSpec

Recommended Posts

NunoBorges

Viva,

I need to use $pid to close a process.

ProcessClose function has different behavior between Run('notepad.exe') and Run(@ ComSpec & '/ c notepad.exe')

Will someone help me?

Nuno

Process_Close_1.au3

Share this post


Link to post
Share on other sites
wakillon

in the second case @ComSpec is cmd.exe

So you get his pid.Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
Blue_Drache

;1test
$pid = Run('notepad.exe');, @TempDir, @SW_SHOW)
MsgBox(528448, "ProcessClose", "Run('notepad.exe')" & @CRLF _
         & @CRLF & "Press Ok, to close process with $PID:" & @CRLF & $pid);, 10)
ProcessClose($pid)

;2test
$pid = Run(@ComSpec & ' /c notepad.exe');, @TempDir, @SW_SHOW)
$pidTest = ProcessExists("notepad.exe")

MsgBox(528448, "ProcessClose", "Run(@ComSpec & ' /c notepad.exe')" & @CRLF _
         & @CRLF & "Press Ok, to close process with $PID:" & @CRLF & $pid);, 10)
ProcessClose($pid)
MsgBox(528448, "ProcessClose", "ProcessExists('notepad.exe')" & @CRLF _
         & @CRLF & "Press Ok, to close process with $PID:" & @CRLF & $pid);, 10)

ProcessClose($pidTest)

I tried nabbing the process via ProcessExists() and it still didn't do anything.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
Blue_Drache

Yep. That worked. Still need to nab the PID with ProcessExsists though. Odd.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
UEZ

When you start Notepad with Run(@ ComSpec & '/ c notepad.exe') you will get 2 PIDs, one for CMD and the other one for Notepad.exe! PID from Notepad.exe is a child process of CMD!

Example:

#Include <WinAPI.au3>
$pid_CMD = Run(@ComSpec & " /c " & "Notepad.exe")
Sleep(500)
$aPid_Notepad = _WinAPI_EnumChildProcess($pid_CMD)
MsgBox(0, "Test", "CMD PID: " & $pid_CMD & @CRLF & "Notepad PID: " & $aPid_Notepad[1][0])
Exit

Func _WinAPI_EnumChildProcess($PID = 0)
    If Not $PID Then
        $PID = _WinAPI_GetCurrentProcessID()
        If Not $PID Then Return SetError(1, 0, 0)
    EndIf

    Local $hSnapshot = DllCall('kernel32.dll', 'ptr', 'CreateToolhelp32Snapshot', 'dword', 0x00000002, 'dword', 0)
    If (@error) Or (Not $hSnapshot[0]) Then Return SetError(1, 0, 0)

    Local $tPROCESSENTRY32 = DllStructCreate('dword Size;dword Usage;dword ProcessID;ulong_ptr DefaultHeapID;dword ModuleID;dword Threads;dword ParentProcessID;long PriClassBase;dword Flags;wchar ExeFile[260]')
    Local $pPROCESSENTRY32 = DllStructGetPtr($tPROCESSENTRY32)
    Local $Ret, $Result[101][2] = [[0]]

    $hSnapshot = $hSnapshot[0]
    DllStructSetData($tPROCESSENTRY32, 'Size', DllStructGetSize($tPROCESSENTRY32))
    $Ret = DllCall('kernel32.dll', 'int', 'Process32FirstW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    While (Not @error) And ($Ret[0])
        If DllStructGetData($tPROCESSENTRY32, 'ParentProcessID') = $PID Then
            $Result[0][0] += 1
            If $Result[0][0] > UBound($Result) - 1 Then
                ReDim $Result[$Result[0][0] + 100][2]
            EndIf
            $Result[$Result[0][0]][0] = DllStructGetData($tPROCESSENTRY32, 'ProcessID')
            $Result[$Result[0][0]][1] = DllStructGetData($tPROCESSENTRY32, 'ExeFile')
        EndIf
        $Ret = DllCall('kernel32.dll', 'int', 'Process32NextW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    WEnd
    _WinAPI_CloseHandle($hSnapshot)
    If $Result[0][0] Then
        ReDim $Result[$Result[0][0] + 1][2]
    Else
        Return SetError(1, 0, 0)
    EndIf
    Return $Result
EndFunc   ;==>_WinAPI_EnumChildProcess

_WinAPI_EnumChildProcess() taken from WinAPIEx.au3!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
NunoBorges

When you start Notepad with Run(@ ComSpec & '/ c notepad.exe') you will get 2 PIDs, one for CMD and the other one for Notepad.exe! PID from Notepad.exe is a child process of CMD!

Example:

#Include <WinAPI.au3>
$pid_CMD = Run(@ComSpec & " /c " & "Notepad.exe")
Sleep(500)
$aPid_Notepad = _WinAPI_EnumChildProcess($pid_CMD)
MsgBox(0, "Test", "CMD PID: " & $pid_CMD & @CRLF & "Notepad PID: " & $aPid_Notepad[1][0])
Exit

Func _WinAPI_EnumChildProcess($PID = 0)
    If Not $PID Then
        $PID = _WinAPI_GetCurrentProcessID()
        If Not $PID Then Return SetError(1, 0, 0)
    EndIf

    Local $hSnapshot = DllCall('kernel32.dll', 'ptr', 'CreateToolhelp32Snapshot', 'dword', 0x00000002, 'dword', 0)
    If (@error) Or (Not $hSnapshot[0]) Then Return SetError(1, 0, 0)

    Local $tPROCESSENTRY32 = DllStructCreate('dword Size;dword Usage;dword ProcessID;ulong_ptr DefaultHeapID;dword ModuleID;dword Threads;dword ParentProcessID;long PriClassBase;dword Flags;wchar ExeFile[260]')
    Local $pPROCESSENTRY32 = DllStructGetPtr($tPROCESSENTRY32)
    Local $Ret, $Result[101][2] = [[0]]

    $hSnapshot = $hSnapshot[0]
    DllStructSetData($tPROCESSENTRY32, 'Size', DllStructGetSize($tPROCESSENTRY32))
    $Ret = DllCall('kernel32.dll', 'int', 'Process32FirstW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    While (Not @error) And ($Ret[0])
        If DllStructGetData($tPROCESSENTRY32, 'ParentProcessID') = $PID Then
            $Result[0][0] += 1
            If $Result[0][0] > UBound($Result) - 1 Then
                ReDim $Result[$Result[0][0] + 100][2]
            EndIf
            $Result[$Result[0][0]][0] = DllStructGetData($tPROCESSENTRY32, 'ProcessID')
            $Result[$Result[0][0]][1] = DllStructGetData($tPROCESSENTRY32, 'ExeFile')
        EndIf
        $Ret = DllCall('kernel32.dll', 'int', 'Process32NextW', 'ptr', $hSnapshot, 'ptr', $pPROCESSENTRY32)
    WEnd
    _WinAPI_CloseHandle($hSnapshot)
    If $Result[0][0] Then
        ReDim $Result[$Result[0][0] + 1][2]
    Else
        Return SetError(1, 0, 0)
    EndIf
    Return $Result
EndFunc   ;==>_WinAPI_EnumChildProcess

_WinAPI_EnumChildProcess() taken from WinAPIEx.au3!

Br,

UEZ

Yes, UEZ :)

Im going to try that, i think it's what i need.

Nuno :mellow:

Share this post


Link to post
Share on other sites
NunoBorges

Thanks,

When you know, it's really simple!

Once there, use "ProcessClose ($ aPid_Notepad [1] [0])"

wonderful

Thanks to all

Nuno

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×