var sa: SECURITY_ATTRIBUTES; hRead, hWrite: THandle; si: STARTUPINFO; pi: PROCESS_INFORMATION; bytesRead: DWORD; buffer: PChar; begin sa.nLength := sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor := nil; sa.bInheritHandle := TRUE; si.cb := sizeof(STARTUPINFO); GetStartupInfo(si); si.hStdError := hWrite; si.hStdOutput := hWrite; si.wShowWindow := SW_SHOW; si.dwFlags := STARTF_USESHOWWINDOW; if ( not CreateProcess(nil,PChar('d:\\winnt\\system32\\cmd.exe') ,nil,nil,true,0,nil,nil,si,pi)) then begin ShowMessage('Error on CreateProcess()'); exit; end; TerminateProcess(pi.hProcess,0);end;
//杀死指定进程ID的进程 function KillProcess(var ID:DWORD):boolean;stdcall; var h,a:THandle; //ID:是要杀死进程的ID begin h:=openprocess(PROCESS_ALL_ACCESS,True,Id); getExitCodeProcess(h,a); if terminateProcess(h,a) then begin //进程已关闭 result:=true; end else //进程关闭失败 result:=false; end;
在超级猛料中发现的: unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,Tlhelp32;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function KillTask(ExeFileName: string): Integer; const PROCESS_TERMINATE = $0001; var ContinueLoop: BOOL; FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32; begin Result := 0; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := SizeOf(FProcessEntry32); ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); while Integer(ContinueLoop) <> 0 do begin if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then Result := Integer(TerminateProcess( OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0)); ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); end; CloseHandle(FSnapshotHandle); end;procedure TForm1.Button1Click(Sender: TObject); begin KillTask('game.exe'); end;end.
sa: SECURITY_ATTRIBUTES;
hRead, hWrite: THandle;
si: STARTUPINFO;
pi: PROCESS_INFORMATION;
bytesRead: DWORD;
buffer: PChar;
begin
sa.nLength := sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor := nil;
sa.bInheritHandle := TRUE; si.cb := sizeof(STARTUPINFO);
GetStartupInfo(si);
si.hStdError := hWrite;
si.hStdOutput := hWrite;
si.wShowWindow := SW_SHOW;
si.dwFlags := STARTF_USESHOWWINDOW;
if ( not CreateProcess(nil,PChar('d:\\winnt\\system32\\cmd.exe')
,nil,nil,true,0,nil,nil,si,pi)) then
begin
ShowMessage('Error on CreateProcess()');
exit;
end; TerminateProcess(pi.hProcess,0);end;
function KillProcess(var ID:DWORD):boolean;stdcall;
var
h,a:THandle; //ID:是要杀死进程的ID
begin
h:=openprocess(PROCESS_ALL_ACCESS,True,Id);
getExitCodeProcess(h,a);
if terminateProcess(h,a) then
begin
//进程已关闭
result:=true;
end
else
//进程关闭失败
result:=false;
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Tlhelp32;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public { Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
KillTask('game.exe');
end;end.