怎样终止外部程序

wcabcd 2003-02-14 08:47:26
怎样终止外部程序。
...全文
31 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Billy_Chen28 2003-02-14
  • 打赏
  • 举报
回复
再给你一个:
atty:要终止的程序
procedure TForm1.Button36Click(Sender: TObject);
const
PROCESS_TERMINATE=$0001;
var
ExeFileName: String;
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
ExeFileName := 'atty.exe';
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
TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0);
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;


Billy_Chen28 2003-02-14
  • 打赏
  • 举报
回复
//为了控制外部报表打印程序
MyHandle: THandle;
MyStartupInfo: TStartupInfo;
MyProcessInfo: TProcessInformation;
//hyadd end;
//以下是打开外部应用程序
procedure ExecuteApp(MyPath: String);
begin
FillChar(MyStartupInfo, SizeOf(MyStartupInfo), 0);
//清除MyStartupInfo中的数据

MyStartupInfo.cb:=SizeOf(MyStartupInfo);

CreateProcess(PChar(MyPath), nil, nil, nil, False,
DETACHED_PROCESS, nil, nil, MyStartupInfo,MyProcessInfo);

MyHandle:=MyProcessInfo.hProcess;
//把执行的程序的句柄赋值给MyHandle,它会在终止程序时用到
end;

//以下程序是关闭外部应用程序
procedure CloseApp(MyHandle: THandle);
begin
TerminateProcess(MyHandle, 0);
end;
grail_ 2003-02-14
  • 打赏
  • 举报
回复
假设要终止的程序的文件名为: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
);

13161795500 2003-02-14
  • 打赏
  • 举报
回复
killprocess
13161795500 2003-02-14
  • 打赏
  • 举报
回复
up

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧