RunOnly(4)
MsgBox(0, '', 'Ok to exit')
Func RunOnly($iMax = 1)
Local $aProcessList = ProcessList(@ScriptName)
If Not @error Then
If Int($aProcessList[0][0]) > $iMax Then
Local $hPsAPI = DllOpen('Psapi.dll')
Local $hKernel = DllOpen('Kernel32.dll')
Local $Counter = 0
For $i = 1 To $aProcessList[0][0] Step +1
If _ProcessGetPath($aProcessList[$i][1], $hKernel, $hPsAPI) = @ScriptFullPath Then $Counter += 1
Next
DllClose($hKernel)
DllClose($hPsAPI)
If $Counter >= $iMax+1 Then Exit
EndIf
EndIf
EndFunc
Func _ProcessGetPath($hPID, $hKernel, $hPsAPI)
Local $sPath = DllStructCreate('char[1000]')
Local $hProcess = DllCall($hKernel, 'int', 'OpenProcess', 'dword', 0x0400 + 0x0010, 'int', 0, 'dword', $hPID)
DllCall($hPsAPI, 'long', 'GetModuleFileNameEx', 'long', $hProcess[0], 'int', 0, 'ptr', DllStructGetPtr($sPath), 'long', DllStructGetSize($sPath))
DllCall($hKernel, 'int', 'CloseHandle', 'hwnd', $hProcess[0])
Local $ret = DllStructGetData($sPath, 1)
Return $ret
EndFunc
HotKeySet("{ESC}", "_Exit")
While 1
Local $aProcessList = ProcessList(@ScriptName)
If $aProcessList[0][0] > 4 Then
ProcessClose($aProcessList[5][0])
EndIf
Sleep(1000)
WEnd
Func _Exit()
Exit
EndFunc ;==>_Exit
На компьютере может быть несколько exe-файлов с одинаковыми именами => у разных программ процессы могут называтся одинаково. Тогда возможно, что не запустится ни одна копия скрипта.madmasles сказал(а):А можно так:Работает тоже только в откомпилированом виде.Код:HotKeySet("{ESC}", "_Exit") While 1 Local $aProcessList = ProcessList(@ScriptName) If $aProcessList[0][0] > 4 Then ProcessClose($aProcessList[5][0]) EndIf Sleep(1000) WEnd Func _Exit() Exit EndFunc ;==>_Exit
Нет проблем:На компьютере может быть несколько exe-файлов с одинаковыми именами => у разных программ процессы могут называтся одинаково. Тогда возможно, что не запустится ни одна копия скрипта.
ProcessClose($aProcessList[5][1])
#include <WinAPIEx.au3>
HotKeySet("{ESC}", "_Exit")
While 1
$u = 0
Local $aProcessList = ProcessList(@ScriptName)
For $i = 1 To UBound($aProcessList) - 1
If _WinAPI_GetModuleFileNameEx($aProcessList[$i][1]) = @ScriptFullPath Then
$u += 1
If $u > 4 Then
ProcessClose($aProcessList[$i][0])
EndIf
EndIf
Next
Sleep(1000)
WEnd
Func _Exit()
Exit
EndFunc ;==>_Exit
Garrett сказал(а):Нет проблем:
Код:ProcessClose($aProcessList[5][1])
Local $aProcessList = ProcessList(@ScriptName)
If $aProcessList[0][0] > 4 Then
Согласен. Проверил. Мой первый скрипт закрывает любые скрипты с одинаковыми именами. Второй вроде закрывает только собственные копии.Если другие программы с таким же именем уже работают
Как то так:Vlad_007 сказал(а):Предполагаю что-то вроде лоудера вида Проверялка-запускалка.
$sPath = 'C:\WinXP\system32\notepad.exe'
$iMax = 4
If Not FileExists($sPath) Then
MsgBox(16, 'Ошибка', 'Файл не найден' & @CRLF & $sPath)
Exit
EndIf
Local $sProcessName = StringTrimLeft($sPath, StringInStr($sPath, '\', 0, -1))
Local $aProcessList = ProcessList($sProcessName)
If Not @error Then
If Int($aProcessList[0][0]) >= $iMax Then
Local $hPsAPI = DllOpen('Psapi.dll')
Local $hKernel = DllOpen('Kernel32.dll')
Local $Counter = 0
For $i = 1 To $aProcessList[0][0] Step +1
If _ProcessGetPath($aProcessList[$i][1], $hKernel, $hPsAPI) = $sPath Then $Counter += 1
Next
DllClose($hKernel)
DllClose($hPsAPI)
If $Counter >= $iMax Then Exit
EndIf
EndIf
Run($sPath & ' ' & $CmdLineRaw)
Func _ProcessGetPath($hPID, $hKernel, $hPsAPI)
Local $sPath = DllStructCreate('char[1000]')
Local $hProcess = DllCall($hKernel, 'int', 'OpenProcess', 'dword', 0x0400 + 0x0010, 'int', 0, 'dword', $hPID)
DllCall($hPsAPI, 'long', 'GetModuleFileNameEx', 'long', $hProcess[0], 'int', 0, 'ptr', DllStructGetPtr($sPath), 'long', DllStructGetSize($sPath))
DllCall($hKernel, 'int', 'CloseHandle', 'hwnd', $hProcess[0])
Local $ret = DllStructGetData($sPath, 1)
Return $ret
EndFunc