獲得當前激活的進程:
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);
ViewProcess();