VC使用TerminateProcess结束进程实例
本文实例讲述了VC使用TerminateProcess结束进程的方法,分享给大家供大家参考。具体实现方法如下:
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);
}
自己改良后的代码如下:
{
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程序设计有所帮助。
上一篇:C++函数模板与类模板实例解析
栏 目:C代码
下一篇:C语言链表完整操作演示
本文标题:VC使用TerminateProcess结束进程实例
本文地址:http://www.codeinn.net/misctech/196284.html