VC使用TerminateProcess结束进程实例
本文实例讲述了VC使用TerminateProcess结束进程的方法,分享给大家供大家参考。具体实现方法如下:
intKillProcess(LPCSTRpszClassName,LPCSTR pszWindowTitle) { HANDLEhProcessHandle; ULONGnProcessID; HWNDTheWindow; TheWindow=::FindWindow(NULL,pszWindowTitle); ::GetWindowThreadProcessId(TheWindow,&nProcessID); hProcessHandle=::OpenProcess(PROCESS_TERMINATE,FALSE, nProcessID); return::TerminateProcess(hProcessHandle,4); } voidCZxDlg::OnOK() { //TODO:Addextravalidationhere //CDialog::OnOK(); KillProcess("radmin.exe","RadminViewer") }
voidCZxDlg::OnButton1() { HANDLE hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(hSnapShot == 0) return; PROCESSENTRY32 thePE; thePE.dwSize = sizeof(PROCESSENTRY32); //遍历正在运行的第一个系统进程 bool Status = Process32First(hSnapShot,&thePE); bool bHaveFlag = false; DWORD ProcessID=0; while(Status) { //遍历正在运行的下一个系统进程 Status = Process32Next(hSnapShot,&thePE); CStringjincheng="qq.exe"; jincheng.MakeLower(); //找到相应的进程**.exe if(0==_tcscmp(thePE.szExeFile,_T(jincheng))) { bHaveFlag = true; ProcessID = thePE.th32ProcessID; //结束指定的进程ProcessID if(!TerminateProcess(OpenProcess(PROCESS_TERMINATE||PROCESS_QUERY_INFORMATION,false,ProcessID),0)) { AfxMessageBox("无法终止指定的进程!",MB_ICONWARNING||MB_OK); } break; } } CloseHandle(hSnapShot); }