VC使用TerminateProcess结束进程实例
复制代码 代码如下:int KillProcess(LPCSTR pszClassName, LPCSTR
pszWindo
本文实例讲述了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程序设计有所帮助。