关闭程序 如Foxmail Var I:Thandle; begin I:=FindWindow(nil,'Foxmail'); If I<>0 Then SendMessage(I,WM_close,0,0); end;关闭进程 try thread.Suspend; thread.Terminate; except ... end;
转贴 (注意uses TLHelp32) 然后 var lppe: TProcessEntry32; found : boolean; Hand : THandle; begin Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); found := Process32First(Hand,lppe); while found do begin ListBox.Items.Add(StrPas(lppe.szExeFile));//列出所有进程。 found := Process32Next(Hand,lppe); end; end; ///////////////////////////////////////////////////// uses ... TLHelp32, ... type TForm1 = class(TForm) ... end; var Form1: TForm1; l : Tlist; ////返回的东东在"L"这个TList中。 type TProcessInfo = Record ExeFile : String; ProcessID : DWORD; end; pProcessInfo = ^TProcessInfo; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var p : pProcessInfo; i : integer; ContinueLoop:BOOL; var FSnapshotHandle:THandle; FProcessEntry32:TProcessEntry32; begin l := TList.Create; l.Clear; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); while integer(ContinueLoop)<>0 do begin New(p); p.ExeFile := FProcessEntry32.szExeFile; p.ProcessID := FProcessEntry32.th32ProcessID; l.Add(p); ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32); end; end; procedure TForm1.FormDestroy(Sender: TObject); var p : pProcessInfo; i : integer; begin With l do for i := Count - 1 DownTo 0 do begin p := items[i]; Dispose(p); Delete(i); end; end; ... end.
继续转 uses tlhelp32; 假设要终止的程序的文件名为:project2.exe,那么例程如下: var lppe:tprocessentry32; sshandle:thandle; hh:hwnd; found:boolean; begin sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0); found:=process32first(sshandle,lppe); while found do begin //进行你的处理其中lppe.szExefile就是程序名。 if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then begin hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID); TerminateProcess(hh,0); end; found:=process32next(sshandle,lppe); end; end; ******************** HANDLE hProcess Windows NT/2000: The handle must have PROCESS_TERMINATE access. For more information, see Process Security and Access Rights. 所以要先使用 DWORD SetSecurityInfo( HANDLE handle, // handle to object SE_OBJECT_TYPE ObjectType, // object type SECURITY_INFORMATION SecurityInfo, // buffer PSID psidOwner, // new owner SID PSID psidGroup, // new primary group SID PACL pDacl, // new DACL PACL pSacl // new SACL );
Var
I:Thandle;
begin
I:=FindWindow(nil,'Foxmail');
If I<>0 Then
SendMessage(I,WM_close,0,0);
end;关闭进程
try
thread.Suspend;
thread.Terminate;
except
...
end;
(注意uses TLHelp32)
然后
var lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
begin
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
ListBox.Items.Add(StrPas(lppe.szExeFile));//列出所有进程。
found := Process32Next(Hand,lppe);
end;
end; /////////////////////////////////////////////////////
uses ... TLHelp32, ... type
TForm1 = class(TForm)
...
end; var
Form1: TForm1;
l : Tlist; ////返回的东东在"L"这个TList中。 type
TProcessInfo = Record
ExeFile : String;
ProcessID : DWORD;
end;
pProcessInfo = ^TProcessInfo; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject);
var p : pProcessInfo;
i : integer;
ContinueLoop:BOOL;
var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
l := TList.Create;
l.Clear;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
New(p);
p.ExeFile := FProcessEntry32.szExeFile;
p.ProcessID := FProcessEntry32.th32ProcessID;
l.Add(p);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end; procedure TForm1.FormDestroy(Sender: TObject);
var p : pProcessInfo;
i : integer;
begin
With l do
for i := Count - 1 DownTo 0 do
begin p := items[i]; Dispose(p); Delete(i); end;
end; ...
end.
uses tlhelp32;
假设要终止的程序的文件名为:project2.exe,那么例程如下:
var
lppe:tprocessentry32;
sshandle:thandle;
hh:hwnd;
found:boolean;
begin
sshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);
found:=process32first(sshandle,lppe);
while found do
begin
//进行你的处理其中lppe.szExefile就是程序名。
if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' then
begin
hh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);
TerminateProcess(hh,0);
end;
found:=process32next(sshandle,lppe);
end;
end;
********************
HANDLE hProcess
Windows NT/2000: The handle must have PROCESS_TERMINATE access.
For more information, see Process Security and Access Rights. 所以要先使用
DWORD SetSecurityInfo(
HANDLE handle, // handle to object
SE_OBJECT_TYPE ObjectType, // object type
SECURITY_INFORMATION SecurityInfo, // buffer
PSID psidOwner, // new owner SID
PSID psidGroup, // new primary group SID
PACL pDacl, // new DACL
PACL pSacl // new SACL
);
halt or terminate方法。