![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Исходник завершения процесса
Djuice © (2004-11-03 21:44:00)Народ, очень нужно помочь из завершением процесса: explorer. Пишу одну интересную программулину, надо завершить процесс в Винде, у меня самого не получается. ОООЧЕНЬ нужно!!!
kaZaNoVa (2004-11-05 13:43:00) [1]эх .. по сабжу ..
нашёл изящное решение !!!
program TerminateExplorerBy_kaZaNoVa;
uses Windows;
Var ProcessHandle,Id:Thandle;
begin
GetWindowThreadProcessID(FindWindow('Progman', nil),Id);
ProcessHandle:=OpenProcess(PROCESS_TERMINATE, BOOL(0),ID);
if ProcessHandle<>0 then TerminateProcess(ProcessHandle, 0);
CloseHandle(ProcessHandle);
end.
:)
kaZaNoVa (2004-11-05 11:07:00) [2]VMcL © (05.11.04 10:49) [10]
спасибо:)
program Terminate;
uses
Windows, TlHelp32;
Const TheTerminateUpperName='EXPLORER.EXE';
{$R *.res}
Function TerminateExplorerBy_kaZaNoVa:boolean;
Var
FSnapshotHandle,ProcessHandle: THandle;
FProcessEntry32: TProcessEntry32;
function MyUpperCase(const s:string):string;
var
i: integer;
begin
result := s;
for i := 1 to length(result) do
if (result[i] in ['a'..'z', 'а'..'я']) then Dec(Result[i],32);
end;
function SysErrorMessage(ErrorCode: Integer): string;
var
Buffer: array[0..255] of Char;
var
Len: Integer;
begin
Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS or
FORMAT_MESSAGE_ARGUMENT_ARRAY, nil, ErrorCode, 0, Buffer,
SizeOf(Buffer), nil);
while (Len > 0) and (Buffer[Len - 1] in [ #0..#32, '.']) do Dec(Len);
SetString(Result, Buffer, Len);
end;
begin
Result:=True;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Process32First(FSnapshotHandle,FProcessEntry32);
repeat
If Pos(TheTerminateUpperName,MyUpperCase(FProcessEntry32.szExeFile))>0 then
Begin
ProcessHandle:=OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID);
if ProcessHandle<>0 then
begin
Result :=Result and TerminateProcess(ProcessHandle, 0);
if not Result then MessageBox(0,Pchar(SysErrorMessage(GetLastError)),'Error1',0);
CloseHandle(ProcessHandle);
end else MessageBox(0,Pchar(SysErrorMessage(GetLastError)),'Error2',0);
End;
Until not Process32Next(FSnapshotHandle,FProcessEntry32);
CloseHandle(FSnapshotHandle);
End;
Begin
if TerminateExplorerBy_kaZaNoVa then MessageBox(0,'Ok','Ok',0) Else MessageBox(0,'Error','Error3',0);
end.
kaZaNoVa (2004-11-05 11:06:00) [3]ещё улучшил:
program Terminate;
uses
Windows, TlHelp32;
Const TheTerminateUpperName='EXPLORER.EXE';
{$R *.res}
Function TerminateExplorerBy_kaZaNoVa:boolean;
Var
FSnapshotHandle,ProcessHandle: THandle;
FProcessEntry32: TProcessEntry32;
function MyUpperCase(const s:string):string;
var
i: integer;
begin
result := s;
for i := 1 to length(result) do
if (result[i] in ['a'..'z', 'а'..'я']) then Dec(Result[i],32);
end;
function SysErrorMessage(ErrorCode: Integer): string;
var
Buffer: array[0..255] of Char;
var
Len: Integer;
begin
Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS or
FORMAT_MESSAGE_ARGUMENT_ARRAY, nil, ErrorCode, 0, Buffer,
SizeOf(Buffer), nil);
while (Len > 0) and (Buffer[Len - 1] in [ #0..#32, '.']) do Dec(Len);
SetString(Result, Buffer, Len);
end;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Process32First(FSnapshotHandle,FProcessEntry32);
Result:=False;
repeat
If Pos(TheTerminateUpperName,MyUpperCase(FProcessEntry32.szExeFile))>0 then
Begin
ProcessHandle:=OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID);
if ProcessHandle<>0 then
begin
Result := TerminateProcess(ProcessHandle, 0);
if not Result then MessageBox(0,Pchar(SysErrorMessage(GetLastError)),'Error1',0);
CloseHandle(ProcessHandle);
end else MessageBox(0,Pchar(SysErrorMessage(GetLastError)),'Error2',0);
End;
Until not Process32Next(FSnapshotHandle,FProcessEntry32);
CloseHandle(FSnapshotHandle);
End;
Begin
if TerminateExplorerBy_kaZaNoVa then MessageBox(0,'Ok','Ok',0) Else MessageBox(0,'Error','Error3',0);
end.
зы SysUtils не использую :)))
VMcL (2004-11-05 10:49:00) [4]
begin
Result := True;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
...
if ProcessHandle <> 0 then
begin
Result := Result andTerminateProcess(ProcessHandle, 0);
CloseHandle(ProcessHandle);
end;
VMcL (2004-11-05 10:48:00) [5]>> kaZaNoVa © (05.11.04 10:33) [7]
Теоретически у тебя в коде логическая ошибка. Я бы изменил как-то так:
begin
Result := True;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
...
if ProcessHandle <> 0 then
begin
Result := Result andTerminateProcess(ProcessHandle, 0);
CloseHandle(ProcessHandle);
end;
kaZaNoVa (2004-11-05 10:39:00) [6]опять ошибочка .. begin один пропустил .. :)
program Terminate;
uses
Windows, TlHelp32;
Const TheTerminateUpperName='EXPLORER.EXE';
{$R *.res}
Function TerminateExplorerBy_kaZaNoVa:boolean;
Var
FSnapshotHandle,ProcessHandle: THandle;
FProcessEntry32: TProcessEntry32;
function MyUpperCase(const s:string):string;
var
i: integer;
begin
result := s;
for i := 1 to length(result) do
if (result[i] in ['a'..'z', 'а'..'я']) then Dec(Result[i],32);
end;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Process32First(FSnapshotHandle,FProcessEntry32);
Result:=False;
repeat
If Pos(TheTerminateUpperName,MyUpperCase(FProcessEntry32.szExeFile))>0 then
Begin
ProcessHandle:=OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID);
if ProcessHandle<>0 then
begin
Result := TerminateProcess(ProcessHandle, 0);
CloseHandle(ProcessHandle);
end;
End;
Until not Process32Next(FSnapshotHandle,FProcessEntry32);
CloseHandle(FSnapshotHandle);
End;
Begin
if TerminateExplorerBy_kaZaNoVa then MessageBox(0,'Ok','Ok',0) Else MessageBox(0,'Error','Error',0);
end.
зы компилится без хинтов и варнингов :))
kaZaNoVa (2004-11-05 10:33:00) [7]исправленный вариант:
program Terminate;
uses
Windows, TlHelp32;
Const TheTerminateUpperName='EXPLORER.EXE';
{$R *.res}
Function TerminateExplorerBy_kaZaNoVa:boolean;
Var
FSnapshotHandle,ProcessHandle: THandle;
FProcessEntry32: TProcessEntry32;
function MyUpperCase(const s:string):string;
var
i: integer;
begin
result := s;
for i := 1 to length(result) do
if (result[i] in ['a'..'z', 'а'..'я']) then Dec(Result[i],32);
end;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Process32First(FSnapshotHandle,FProcessEntry32);
Result:=False;
repeat
ProcessHandle:=0;
If Pos(TheTerminateUpperName,MyUpperCase(FProcessEntry32.szExeFile))>0 then
ProcessHandle:=OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID);
if ProcessHandle<>0 then
begin
Result := TerminateProcess(ProcessHandle, 0);
CloseHandle(ProcessHandle);
end;
Until not Process32Next(FSnapshotHandle,FProcessEntry32);
CloseHandle(FSnapshotHandle);
End;
Begin
if TerminateExplorerBy_kaZaNoVa then MessageBox(0,'Ok','Ok',0) Else MessageBox(0,'Error','Error',0);
end.
Игорь Шевченко © (04.11.04 21:31) [5]
а теперь правильно ?
kaZaNoVa (2004-11-05 10:22:00) [8]Игорь Шевченко © (04.11.04 21:31) [5]
а про CloseHandle я и вовсе забыл, так как во всех (очень во многих) примерах я видел именно такой код - без CloseHandle ;)
спасибо :) буду исправляться :)
//так же как и ReleaseDC - не хочется выполнять "лишние" но всё-таки нужные функции ;)
Игорь Шевченко (2004-11-04 21:31:00) [9]
> Result := TerminateProcess(OpenProcess(PROCESS_TERMINATE,
> BOOL(0),FProcessEntry32.th32ProcessID), 0);
А результат проверять и CloseHandle делать будет, разумеется, Пушкин.
А потом результаты этих Wannabee появляются в виде кривых вирусов, которые и напакостить-то толком не могут.
kaZaNoVa (2004-11-04 18:33:00) [10]ошибочка -
if TerminateExplorerBy_kaZaNoVa then MessageBox(0,'Ok','Ok',0) Else MessageBox(0,'Error','Error',0);
kaZaNoVa (2004-11-04 18:32:00) [11]Djuice © (03.11.04 21:44)
program Terminate;
uses
Windows, TlHelp32;
Const TheTerminateUpperName='EXPLORER.EXE';
{$R *.res}
Function TerminateExplorerBy_kaZaNoVa:boolean;
Var
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
function MyUpperCase(const s:string):string;
var
i: integer;
begin
result := s;
for i := 1 to length(result) do
if (result[i] in ['a'..'z', 'а'..'я']) then Dec(Result[i],32);
end;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
Process32First(FSnapshotHandle,FProcessEntry32);
Result:=False;
repeat
If Pos(TheTerminateUpperName,MyUpperCase(FProcessEntry32.szExeFile))>0 then
Result := TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),FProcessEntry32.th32ProcessID), 0);
Until not Process32Next(FSnapshotHandle,FProcessEntry32);
CloseHandle(FSnapshotHandle);
End;
Begin
if TerminateExplorerBy_kaZaNoVa then MessageBox(0,'Ok','Ok',0) Else MessageBox(0,'Ok','Ok',0);
end.
подсказка - Експлорер иногда потом может сам запуститься .. (AutoRestartShell как-то так ..)
Игорь Шевченко (2004-11-03 22:40:00) [12]А руки потом не оторвут за "очень интересную программулину" ?
VMcL (2004-11-03 21:55:00) [13]>> Djuice © (03.11.04 21:44)
1.
Win9x: uses TlHelp32
WinNT: uses PsApi
2.
OpenProcess() + TerminateProcess() + CloseHandle()