NunoBorges Posted August 4, 2011 Share Posted August 4, 2011 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?NunoProcess_Close_1.au3 Link to comment Share on other sites More sharing options...
wakillon Posted August 4, 2011 Share Posted August 4, 2011 in the second case @ComSpec is cmd.exeSo you get his pid. AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Blue_Drache Posted August 4, 2011 Share Posted August 4, 2011 ;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 Link to comment Share on other sites More sharing options...
wakillon Posted August 4, 2011 Share Posted August 4, 2011 Change this part $pid = Run(@ComSpec & ' /c notepad.exe');, @TempDir, @SW_SHOW) ProcessWait ( "notepad.exe") $pidTest = ProcessExists("notepad.exe") AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Blue_Drache Posted August 4, 2011 Share Posted August 4, 2011 Yep. That worked. Still need to nab the PID with ProcessExsists though. Odd. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
UEZ Posted August 4, 2011 Share Posted August 4, 2011 (edited) 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: expandcollapse popup#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 August 4, 2011 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
NunoBorges Posted August 4, 2011 Author Share Posted August 4, 2011 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: expandcollapse popup#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 Link to comment Share on other sites More sharing options...
NunoBorges Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks, When you know, it's really simple! Once there, use "ProcessClose ($ aPid_Notepad [1] [0])" wonderfulThanks to all Nuno Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now