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 );
myHwnd:=FindWindow(nil,PChar(ProgramName)) ; new(lpID); GetWindowThreadProcessId(myHwnd,lPID); mID:=(lpID^); Result:=TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE, FALSE,mID),0); Dispose(lpID);
procedure TFrmOpenORClose.BtnRunAppClick(Sender: TObject); var str: string; //存储指定的应用程序文件名 begin if opendialog1.Execute then //选择要调用的外部可执行程序 begin str := opendialog1.FileName; //获取可执行文件名 winexec(PChar(str), SW_SHOWNORMAL); //启动指定的可执行程序 end; end;procedure TFrmOpenORClose.BtnClosrAppClick(Sender: TObject); var hWndClose: HWnd;//存储指定的外部应用程序窗口句柄 str: String; //存储指定的外部应用程序的窗口名 begin str := InputBox('提示','请输入应用程序名:','');//获取要关闭的应用程序窗口名 if str <> '' then begin//根据窗口名查找要关闭的窗口句柄 hWndClose := FindWindow(nil, PChar(str)); if hWndClose <> 0 then//如果查找成功,则发送消息,关闭指定的窗口 SendMessage(hWndClose,WM_CLOSE,0,0) else//否则,给出提示信息 ShowMessage('没找到指定的应用程序,所以无法关闭!'); end; end;
假设要终止的程序的文件名为: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
);
new(lpID);
GetWindowThreadProcessId(myHwnd,lPID);
mID:=(lpID^);
Result:=TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE,
FALSE,mID),0);
Dispose(lpID);
var
str: string; //存储指定的应用程序文件名
begin
if opendialog1.Execute then //选择要调用的外部可执行程序
begin
str := opendialog1.FileName; //获取可执行文件名
winexec(PChar(str), SW_SHOWNORMAL); //启动指定的可执行程序
end;
end;procedure TFrmOpenORClose.BtnClosrAppClick(Sender: TObject);
var
hWndClose: HWnd;//存储指定的外部应用程序窗口句柄
str: String; //存储指定的外部应用程序的窗口名
begin
str := InputBox('提示','请输入应用程序名:','');//获取要关闭的应用程序窗口名
if str <> '' then
begin//根据窗口名查找要关闭的窗口句柄
hWndClose := FindWindow(nil, PChar(str));
if hWndClose <> 0
then//如果查找成功,则发送消息,关闭指定的窗口
SendMessage(hWndClose,WM_CLOSE,0,0)
else//否则,给出提示信息
ShowMessage('没找到指定的应用程序,所以无法关闭!');
end;
end;