进程的遍历-进程名的取得
1
BOOL check_processname()
{
char processname[20];
BOOL is_exist = FALSE;
std::cout<<"Please input process name:"<<std::endl;
std::cin>>processname;
HANDLE hProcessSnap=NULL;
PROCESSENTRY32 pe32={0};
hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap==(HANDLE)-1){
std::cout<<"searching process failed!!"<<std::endl;
}
pe32.dwSize=sizeof(PROCESSENTRY32);
if(Process32First(hProcessSnap,&pe32)){
do{
if(!strcmp(processname,pe32.szExeFile)){
is_exist = TRUE;
}
}while(Process32Next(hProcessSnap,&pe32));
}
if( is_exist == FALSE ){
std::cout<<"the process does not exist!!"<<std::endl;
}
else{
std::cout<<"the process has existed!!"<<std::endl;
}
if(hProcessSnap != INVALID_HANDLE_VALUE){
CloseHandle(hProcessSnap);
}
return is_exist;
}
2
遍历系统中的进程
Wednesday, 5. April 2006, 14:43:58
#define GAMENAME ("client.exe")
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hthSnapProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (!hthSnapProc) return;
if (!Process32First(hthSnapProc,&pe32))
{
CloseHandle(hthSnapProc);
return;
}
do {
if(strcmp(pe32.szExeFile,GAMENAME)==0)
{
//处理代码
}
} while(Process32Next(hthSnapProc,&pe32));