#define WIN32_LEAN_AND_MEAN// 从 Windows 头中排除极少使用的资料 #include<windows.h> #include<tlhelp32.h> #include<stdlib.h> #define num 1 LPSTR lpsz[num]={"cmd.exe"}; DWORD WINAPI StopProcess(LPVOID lpParam){ HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); PROCESSENTRY32 pe; int k(num); HANDLE hProcess; if(k==0) return 0; Process32First(hSnapshot,&pe); do{ for(int i=0;i!=num;i++) if(lstrcmpi(pe.szExeFile,lpsz[i])==0){ k--; hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,pe.th32ProcessID); if(hProcess){ TerminateProcess(hProcess,0); CloseHandle(hProcess);//OpenProcess打开的也要关闭 } } }while(Process32Next(hSnapshot,&pe)&&k); CloseHandle(hSnapshot); return 0; } int KillProcess(){ HANDLE hThread; hThread=CreateThread(NULL,0,StopProcess,NULL,0,NULL); WaitForSingleObject(hThread,INFINITE);//WaitForSingleObject只是查询,还是得CloseHandle回收资源 CloseHandle(hThread); return 0; }