Показать полную графическую версию : [решено] Как узнать запущены ли два процесса с одним именем
CyberStyLe
15-10-2009, 13:12
Необходимо знать запущены ли два процесса с одним именем или нет. Процессы из одного приложения. У кого какие мысли на этот счет?
ProcessList('processname')
CyberStyLe
15-10-2009, 13:50
kaster, Нет, ты меня немного недопонял. Есть приложение создающие из себя два процесса. Необходимо реализовать следующую проверку: если запущен один процесс или ни одного, то гууд. Если два то выход.
По сути я это уже реализовал, но только деревянным методом:
$list = ProcessList("приложение.exe")
for $i = 1 to $list[0][0]
FileWriteLine("test.txt", $list[$i][1])
next
Sleep(50)
$datacheckline = _FileCountLines("test.txt")
if $datacheckline >= 2 Then
exit
Elseif $datacheckline = 1 Or NOT FileExists("test.txt") Then
Run(1234.exe)
и дальше код...
Он слишком прост, и тем более создается левый файл. Нужно как-то попрофессиональней чтоли ;)
Да нет, я все правильно понял :)
Но твой способ действительно "деревянный" :teeth:
И потом, обрамляй код тэгами
some code
Необязательно создавать файл, писать туда данные а потом снова считывать. Достаточно просто проверить кол-во элементов $list[0][0]
Если больше двух, выход.
$list = ProcessList("приложение.exe")
If $list[0][0] >= 2 Then
Exit; или ProcessClose('приложение') если прога запускается извне
Else
Run('1234.exe')
EndIf
...
Entire code
...
Если нужно узнать является ли второй процесс дочерним первому, то сделать это можно так:
$aChilds_IDs = _ProcessGetChildren("приложение.exe")
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) @error (http://www.autoitscript.com/autoit3/docs/macros.htm#@error) And (http://www.autoitscript.com/autoit3/docs/keywords.htm#And) $aChilds_IDs[0][0] >= 1 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit) 2
Else (http://www.autoitscript.com/autoit3/docs/keywords.htm#Else)
Run (http://www.autoitscript.com/autoit3/docs/functions/Run.htm)("1234.exe")
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
;===================================================================================================
; Function Name: _ProcessGetChildren()
;
; Description: Retrieve an array of all top level child processes
;
; Parameter(s): $i_pid: The process identifier of the process you want to list the child
; processes from
;
; Return Value(s):
; On Success:
; 2 dimensional array:
; [0][0] number of child processes found
; [n][0] is the process id of the child
; [n][1] is the process name of the child
;
; On Failure:
; Non array
;
; @Error:
; (1): CreateToolhelp32Snapshot failed
; (2): Process32First failed
; (3): No children processes found
;
; Remark(s): Tested on Windows XP SP2
;
; Author(s): SmOke_N (Ron Nielsen)
;
;===================================================================================================
Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _ProcessGetChildren($i_Pid) ; First level children processes only
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) IsString (http://www.autoitscript.com/autoit3/docs/functions/IsString.htm)($i_Pid) Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) $i_Pid = ProcessExists (http://www.autoitscript.com/autoit3/docs/functions/ProcessExists.htm)($i_Pid)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $i_Pid Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) SetError (http://www.autoitscript.com/autoit3/docs/functions/SetError.htm)(-1, 0, $i_Pid)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) Const (http://www.autoitscript.com/autoit3/docs/keywords.htm#Const) $TH32CS_SNAPPROCESS = 0x00000002
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $a_tool_help = DllCall (http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm)("Kernel32.dll", "long", "CreateToolhelp32Snapshot", "int", $TH32CS_SNAPPROCESS, "int", 0)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) IsArray (http://www.autoitscript.com/autoit3/docs/functions/IsArray.htm)($a_tool_help) = 0 Or (http://www.autoitscript.com/autoit3/docs/keywords.htm#Or) $a_tool_help[0] = -1 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) SetError (http://www.autoitscript.com/autoit3/docs/functions/SetError.htm)(1, 0, $i_Pid)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $tagPROCESSENTRY32 = _
DllStructCreate (http://www.autoitscript.com/autoit3/docs/functions/DllStructCreate.htm)( _
"dword dwsize;" & _
"dword cntUsage;" & _
"dword th32ProcessID;" & _
"uint th32DefaultHeapID;" & _
"dword th32ModuleID;" & _
"dword cntThreads;" & _
"dword th32ParentProcessID;" & _
"long pcPriClassBase;" & _
"dword dwFlags;" & _
"char szExeFile[260]")
DllStructSetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructSetData.htm)($tagPROCESSENTRY32, 1, DllStructGetSize (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetSize.htm)($tagPROCESSENTRY32))
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $p_PROCESSENTRY32 = DllStructGetPtr (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetPtr.htm)($tagPROCESSENTRY32)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $a_pfirst = DllCall (http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm)("Kernel32.dll", "int", "Process32First", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) IsArray (http://www.autoitscript.com/autoit3/docs/functions/IsArray.htm)($a_pfirst) = 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) SetError (http://www.autoitscript.com/autoit3/docs/functions/SetError.htm)(2, 0, $i_Pid)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $a_pnext, $a_children[11][2] = [[10]], $i_child_pid, $i_parent_pid, $i_add = 0
$i_child_pid = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($tagPROCESSENTRY32, "th32ProcessID")
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_child_pid <> $i_Pid Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
$i_parent_pid = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($tagPROCESSENTRY32, "th32ParentProcessID")
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_parent_pid = $i_Pid Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
$i_add += 1
$a_children[$i_add][0] = $i_child_pid
$a_children[$i_add][1] = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($tagPROCESSENTRY32, "szExeFile")
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
$a_pnext = DllCall (http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm)("Kernel32.dll", "int", "Process32Next", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) IsArray (http://www.autoitscript.com/autoit3/docs/functions/IsArray.htm)($a_pnext) And (http://www.autoitscript.com/autoit3/docs/keywords.htm#And) $a_pnext[0] = 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) ExitLoop (http://www.autoitscript.com/autoit3/docs/keywords.htm#ExitLoop)
$i_child_pid = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($tagPROCESSENTRY32, "th32ProcessID")
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_child_pid <> $i_Pid Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
$i_parent_pid = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($tagPROCESSENTRY32, "th32ParentProcessID")
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_parent_pid = $i_Pid Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_add = $a_children[0][0] Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
ReDim (http://www.autoitscript.com/autoit3/docs/keywords.htm#ReDim) $a_children[$a_children[0][0] + 11][2]
$a_children[0][0] = $a_children[0][0] + 10
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
$i_add += 1
$a_children[$i_add][0] = $i_child_pid
$a_children[$i_add][1] = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($tagPROCESSENTRY32, "szExeFile")
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_add <> 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
ReDim (http://www.autoitscript.com/autoit3/docs/keywords.htm#ReDim) $a_children[$i_add + 1][2]
$a_children[0][0] = $i_add
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
DllCall (http://www.autoitscript.com/autoit3/docs/functions/DllCall.htm)("Kernel32.dll", "int", "CloseHandle", "long", $a_tool_help[0])
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $i_add Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) $a_children
Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) SetError (http://www.autoitscript.com/autoit3/docs/functions/SetError.htm)(3, 0, 0)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc) ;==>_ProcessGetChildren
CyberStyLe
16-10-2009, 06:31
kaster, Спасибо подошло ;)
Creat0R, хотя узнавать является ли процесс дочерним было не нужно, но все равно большое спасибо. Думаю в будущем это пригодиться :)
CyberStyLe, если тема решена, то её следует помечать как таковую: Как сообщить о том, что моя проблема решена? (http://forum.oszone.net/faq.php?faq=vb_board_usage#faq_solved_thread_faq)
CyberStyLe
18-10-2009, 10:14
Creat0R, Упс.. забыл сразу сделать. Отметил как [решено] :)
© OSzone.net 2001-2012
vBulletin v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.
Available in ZeroNet 1osznRoVratMCN3bFoFpR2pSV5c9z6sTC