#include <Tlhelp32.h>
#include "stdio.h"
if(ListBox1->Items->Count>0){
for (int i=0;i<ListBox1->Items->Count;i++){
KillProcessByName(ListBox1->Items->Strings[i].c_str());
}
tip->Caption="列表中数据清理结束";
}else{
tip->Caption="列表中没有数据";
}
BOOL KillProcessByName(char *ProcessName)
{
char cmd[256] = {0};
PROCESSENTRY32 pe32;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if(::Process32First(hProcessSnap,&pe32)){
do{
if(stricmp(pe32.szExeFile,ProcessName) == 0)//忽略大小写
//if(AnsiString(pe32.szExeFile) == AnsiString(ProcessName))
{
//sprintf(cmd,"taskkill /pid %d", pe32.th32ProcessID);
sprintf(cmd,"ntsd -c q -p %d", pe32.th32ProcessID);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
CreateProcess( NULL, // No module name (use command line).
cmd, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ); // Pointer to PROCESS_INFORMATION structure.
//system(cmd);
//break;
}
}while(Process32Next(hProcessSnap,&pe32));
}
CloseHandle(hProcessSnap);
return TRUE;
}