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 );
假设要终止的程序的文件名为: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
);
另外,最好不要用TerminateProcess关闭进程,要改
用向程序的主进程PostThreadMessage(WM_QUIT)的方
式让它自己关闭。
相信看过就会用了。var
sCommandLine: string;
bCreateProcess: boolean;
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
sCommandLine := 'ARJ.EXE /?';
bCreateProcess := CreateProcessA(nil,
PChar(sCommandLine),
nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
lpStartupInfo, lpProcessInformation);
if bCreateProcess then
WaitForSingleObject(lpProcessInformation.hProcess,
INFINITE);
end;
㈡ 编程语言Delphi3.0,操作系统 Window95
同样是上面的例子的部分代码 var
pWindowsList: pointer;
hActiveWindow: HWnd;
hExeHandle: THandle;
begin
pWindowsList := DisableTaskWindows(0);
hActiveWindow := GetActiveWindow;
try
hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL);
while GetModuleUsage(hExeHandle) <> 0 do
Application.ProcessMessages;
finally
EnableTaskWindows(pWindowsList);
SetActiveWindow(hActiveWindow);
end;
end;
// 相信你明白了。
题外话:如果执行的是 MSDOS 外部程序,如何能让它的窗口不显示
出来呢? [ 接上例 ]:
TStartupInfo 这个结构中有一个 sShowWindow 栏位, 将之设为
SW_HIDE即可,
同时, dwFlags 标志中至少需含有 STARTF_USESHOWWINDOW, 否则
CreateProcess
时, sShowWindow 栏位的设定会无效, 以下是修改过的程式:var
sCommandLine: string;
bCreateProcess: boolean;
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
// sCommandLine 的内容请视您的情况修改
sCommandLine :='Xcopy d:\temp\temp1\*.* d:\temp\temp2
/v/y';
lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
lpStartupInfo.wShowWindow := SW_HIDE;
bCreateProcess := CreateProcess(nil,
PChar(sCommandLine),nil,nil,True,
HIGH_PRIORITY_CLASS, nil,
nil,lpStartupInfo, lpProcessInformation);
if bCreateProcess then
WaitForSingleObject(lpProcessInformation.hProcess,
INFINITE);
end;