Advertisement
Gov_777

Проверка запущен ли процесс и если запущен - прибить

May 30th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.65 KB | None | 0 0
  1. function IsRunning(sName: string): boolean; // проверяет, запущен ли процесс sName
  2. var
  3.   han: THandle;
  4.   ProcStruct: PROCESSENTRY32;
  5.   sID: string;
  6. begin
  7.   Result := false;
  8.   han := CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  9.   if han = 0 then
  10.     exit;
  11.   ProcStruct.dwSize := sizeof(PROCESSENTRY32);
  12.   if Process32First(han, ProcStruct) then
  13.   begin
  14.     repeat
  15.       sID := ExtractFileName(ProcStruct.szExeFile);
  16.       if uppercase(copy(sId, 1, length(sName))) = uppercase(sName) then
  17.       begin
  18.         Result := true;
  19.         Break;
  20.       end;
  21.     until not Process32Next(han, ProcStruct);
  22.   end;
  23.   CloseHandle(han);
  24. end;
  25.  
  26. function KillTask(ExeFileName: string): integer;//прибить процесс
  27. const
  28.   PROCESS_TERMINATE = $0001;
  29. var
  30.   ContinueLoop: BOOL;
  31.   FSnapshotHandle: THandle;
  32.   FProcessEntry32: TProcessEntry32;
  33. begin
  34.   result := 0;
  35.   FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  36.   FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  37.   ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  38.   while integer(ContinueLoop) <> 0 do
  39.   begin
  40.     if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
  41.       Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0));
  42.     ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  43.   end;
  44.   CloseHandle(FSnapshotHandle);
  45. end;
  46. //  применение
  47. //  while IsRunning('OkayFreedomService.exe') do begin
  48. //  Sleep(10);
  49. //  KillTask('OkayFreedomClient.exe') end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement