VC應用TerminateProcess停止過程實例。本站提示廣大學習愛好者:(VC應用TerminateProcess停止過程實例)文章只能為提供參考,不一定能成為您想要的結果。以下是VC應用TerminateProcess停止過程實例正文
本文實例講述了VC應用TerminateProcess停止過程的辦法,分享給年夜家供年夜家參考。詳細完成辦法以下:
int KillProcess(LPCSTR pszClassName, LPCSTR
pszWindowTitle)
{
HANDLE hProcessHandle;
ULONG nProcessID;
HWND TheWindow;
TheWindow = ::FindWindow( NULL, pszWindowTitle );
::GetWindowThreadProcessId( TheWindow, &nProcessID );
hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE,
nProcessID );
return ::TerminateProcess( hProcessHandle, 4 );
}
void CZxDlg::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
KillProcess("radmin.exe","Radmin Viewer")
}
void CZxDlg::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);
CString jincheng ="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);
}
本身改進後的代碼以下:
void kill(CString jin)
{
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);
CString jincheng ;
jincheng=thePE.szExeFile;
jincheng.MakeLower();
//找到響應的過程 **.exe
if ( jincheng == jin)
{
bHaveFlag = true;
ProcessID = thePE.th32ProcessID;
//AfxMessageBox(thePE.szExeFile,MB_ICONWARNING||MB_OK);
//停止指定的過程 ProcessID
if(!TerminateProcess(OpenProcess (PROCESS_TERMINATE||PROCESS_QUERY_INFORMATION,false,ProcessID),0))
{
AfxMessageBox("沒法終止指定的過程!",MB_ICONWARNING||MB_OK);
}
break;
}
}
CloseHandle(hSnapShot);
}
願望本文所述對年夜家的VC法式設計有所贊助。