The TerminateThread function terminates a thread. BOOL TerminateThread( HANDLE hThread, // handle to the thread DWORD dwExitCode // exit code for the thread ); ParametershThreadIdentifies the thread to terminate. Windows NT: The handle must have THREAD_TERMINATE access. For more information, see Thread Objects.dwExitCodeSpecifies the exit code for the thread. Use the GetExitCodeThread function to retrieve a thread's exit value. Return ValuesIf the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. ResTerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating. TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:?If the target thread owns a critical section, the critical section will not be released. ?If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. ?If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread. If the target thread is the last thread of a process when this function is called, the thread's process is also terminated. The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter. Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.
<B>You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination.</B> For example, TerminateThread can result in the following problems: <List> <LI>If the target thread owns a critical section, the critical section will not be released. </LI> <LI>If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. </LI> <LI>If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL</LI> </List>
我这里有一段bcb编的获得所有进程,并杀除选定的进程的例子,你自己改一改就适合你了。 获得当前激活的进程: 1:包含头文件tlhelp32.h 2:.cpp如下: ListView2->Items->Clear(); TListItem *mItem; AnsiString ExeFile; Pointer pt,pt2; unsigned int s; DWORD size,size2; HANDLE snapshot; PROCESSENTRY32 processinfo; processinfo.dwSize = sizeof(processinfo); snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if (snapshot==NULL) return; bool flag = Process32First (snapshot,&processinfo); while (flag){
1.取线程中止码:GetExitCodeThread((void *)thread->Handle,&ExitCode);
2.强行中止: TerminateThread((void *)thread->Handle,ExitCode);
如此而已
DWORD dwExitCode // exit code for the thread
);
ParametershThreadIdentifies the thread to terminate.
Windows NT: The handle must have THREAD_TERMINATE access. For more information, see Thread Objects.dwExitCodeSpecifies the exit code for the thread. Use the GetExitCodeThread function to retrieve a thread's exit value. Return ValuesIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError. ResTerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating.
TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:?If the target thread owns a critical section, the critical section will not be released.
?If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
?If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.
If the target thread is the last thread of a process when this function is called, the thread's process is also terminated.
The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter. Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.
__fastcall MyThread::MyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
extern int js;
void __fastcall MyThread::Execute()
{
//---- Place thread code here ----
while (true)
js++; // 死循环,不停地计数
}
======================================================
//下面是主程序:
#include <vcl.h>
#pragma hdrstop#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int js=0; // 全局变量 js
MyThread *thread=NULL;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Edit1->Text=js; // 时钟不断显示js值
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) // 运行线程
{
if (thread==NULL)
{
thread= new MyThread(true);
thread->Priority =tpHigher;
thread->Resume(); // 线程运行
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) // 中止线程
{
if (thread!=NULL)
{
unsigned long ExitCode;
if (!GetExitCodeThread((void *)thread->Handle,&ExitCode))
ShowMessage("得不到退出码,中止失败!");
else
{
if (TerminateThread((void *)thread->Handle,ExitCode))
{
delete thread;
thread=NULL;
}
else
ShowMessage("中止失败!");
}
}
}中止线程后,Edit1的值就不再变化
For example, TerminateThread can result in the following problems:
<List>
<LI>If the target thread owns a critical section, the critical section will not be released. </LI>
<LI>If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. </LI>
<LI>If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL</LI>
</List>
获得当前激活的进程:
1:包含头文件tlhelp32.h
2:.cpp如下: ListView2->Items->Clear();
TListItem *mItem;
AnsiString ExeFile;
Pointer pt,pt2;
unsigned int s;
DWORD size,size2;
HANDLE snapshot;
PROCESSENTRY32 processinfo;
processinfo.dwSize = sizeof(processinfo);
snapshot =
CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (snapshot==NULL) return;
bool flag = Process32First (snapshot,&processinfo);
while (flag){
mItem=ListView2->Items->Add();
ExeFile=AnsiString(processinfo.szExeFile);
mItem->Caption=ExeFile;
mItem->SubItems->Add(
IntToStr(int(processinfo.th32ParentProcessID))); mItem->SubItems->Add(
IntToHex(int(processinfo.th32ProcessID),8).UpperCase());
size=GetFileVersionInfoSize(ExeFile.c_str(),&size2);
pt=malloc(size);
GetFileVersionInfo(ExeFile.c_str(),NULL,size,pt);
if(VerQueryValue(pt,
"\\StringFileInfo\\040904E4\\FileVersion",
&pt2,&s))
mItem->SubItems->Add(PChar(pt2));
if(VerQueryValue(pt,
"\\StringFileInfo\\040904E4\\CompanyName",
&pt2,&s))
mItem->SubItems->Add(PChar(pt2));
if(VerQueryValue(pt,
"\\StringFileInfo\\040904E4\\FileDescription",
&pt2,&s))
mItem->SubItems->Add(PChar(pt2));
free(pt);
flag = Process32Next(snapshot,&processinfo);
}要杀除一个进程,必须获得该进程的父线成ID(避免仅仅杀除子进程)。 if (ListView2->SelCount==0){
MessageBox(Handle,"请首先选择一个进程!",
"中止进程",MB_OK¦MB_ICONWARNING);
return;
}
int pPid=StrToInt(ListView2->Selected->
SubItems->Strings[0]);
HANDLE ps = OpenProcess(1,false,pPid);
if(ps&&TerminateProcess(ps,-9)){
MessageBox(Handle,"成功中止进程!",
"中止进程",MB_OK¦MB_ICONINFORMATION);
}
else
MessageBox(Handle,"中止进程失败!",
"中止进程",MB_OK¦MB_ICONWARNING);