procedure TForm1.Button1Click(Sender: TObject);
begin
with TTest.Create(false) do
begin
FreeOnTerminate := true;
sleep(1000);
ShowMessage('Terminating');
TerminateThread(Handle,0);
ShowMessage('Terminated');
end;
end;
没有报错呀!
说错了,是在delphi中结束线程,用TerminateThread(TheThread.Handle,0)
这个函数会报错,错误为project project1.exe raised exception class
EAccessviolation with message 'access violation at address 0441ed3
in modile project1.exe
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
);