用VisualC 干干净净地清除进程(2)
2008-04-09 04:10:36来源:互联网 阅读 ()
// CFindKillProcess - to find/kill a process by module name.
//
CFindKillProcess::CFindKillProcess()
{}
CFindKillProcess::~CFindKillProcess()
{}
//////////////////
// Search for process whose module name matches parameter.
// Finds "foo" or "foo.exe"
DWORD CFindKillProcess::FindProcess(LPCTSTR modname, BOOL bAddExe)
{
CProcessIterator itp;
for (DWORD pid=itp.First(); pid; pid=itp.Next()) {
TCHAR name[_MAX_PATH];
CProcessModuleIterator itm(pid);
HMODULE hModule = itm.First(); // .EXE
if (hModule) {
GetModuleBaseName(itm.GetProcessHandle(),hModule, name, _MAX_PATH);
string sModName = modname;
if (strcmpi(sModName.c_str(),name)==0)
return pid;
sModName = ".exe";
if (bAddExe && strcmpi(sModName.c_str(),name)==0)
return pid;
}
}
return 0;
}
//////////////////
// Kill a process cleanly: Close main windows and wait.
// bZap=TRUE to force kill.
BOOL CFindKillProcess::KillProcess(DWORD pid, BOOL bZap)
{
CMainWindowIterator itw(pid);
for (HWND hwnd=itw.First(); hwnd; hwnd=itw.Next()) {
DWORD bOKToKill = FALSE;
SendMessageTimeout(hwnd, WM_QUERYENDSESSION, 0, 0,
SMTO_ABORTIFHUNG|SMTO_NOTIMEOUTIFNOTHUNG,100, &bOKToKill);
if (!bOKToKill)
return FALSE; // window doesn't want to die: abort
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
// I've closed the main windows; now wait for process to die.
BOOL bKilled = TRUE;
HANDLE hp=OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE,FALSE,pid);
if (hp) {
if (WaitForSingleObject(hp, 5000) != WAIT_OBJECT_0) {
if (bZap) { // didn't die: force kill it if zap requested
TerminateProcess(hp,0);
} else {
bKilled = FALSE;
}
}
CloseHandle(hp);
}
return bKilled;
}
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "EnumProc.h"
#define tpf _tprintf // to save typing
typedef list<string> CStringList; // like MFC, but with STL
// pre-declare functions
int help();
// check for switch: / or -
inline BOOL isswitch(TCHAR c) { return c==L'/' || c==L'-'; }
int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
CStringList cmdargs; // command-line args (processes to kill)
BOOL bDisplayOnly=FALSE; // don't kill, just show results
BOOL bQuiet=FALSE; // suppress error messages
BOOL bZap=FALSE; // force-kill process
// Parse command line. Switches can come in any order.
for (int i=1; i<argc; i ) {
if (isswitch(argv[i][0])) {
for (UINT j=1; j<strlen(argv[i]); j ) {
switch(tolower(argv[i][j])) {
case '?': help(); return 0;
case 'n': bDisplayOnly=TRUE; break;
case 'q': bQuiet=TRUE; break;
case 'z': bZap=TRUE; break;
default:
return help();
}
}
} else {
cmdargs.push_back(argv[i]); // got a non-switch arg: add to list
}
}
if (cmdargs.size()<=0)
help();
// Now iterate args (module names), killing each one
CStringList::iterator it;
for (it=cmdargs.begin(); it!=cmdargs.end(); it ) {
CFindKillProcess fkp;
DWORD pid = fkp.FindProcess(it->c_str());
if (pid) {
if (bDisplayOnly) {
tpf(_T("Kill process %d(0xx)\n"),pid,pid);
} else {
fkp.KillProcess(pid, bZap);
}
} else if (!bQuiet) {
tpf(_T("Error: Can't find process '%s'.\n"),it->c_str());
}
}
return 0;
}
int help()
{
tpf(_T("kp: Kill process from command line.\n"));
tpf(_T(" Copyright 2002 Paul DiLascia.\n\n"));
tpf(_T(" kp [/nqz?] modname1 [modname2....]\n"));
tpf(_T(" where modnameN is a module name; eg foo or foo.exe\n"));
tpf(_T("\n"));
tpf(_T(" /n(othing) don't kill, just show results\n"));
tpf(_T(" /q(uiet) don't show errors\n"));
tpf(_T(" /z(ap) force kill (ignore WM_QUERYENDSESSION)\n"));
tpf(_T("\n"));
return 0;
}
四、小结
本实例通过介绍CfindKillProcess类探讨了在Windows2000下彻底消除进程的方法,虽然该程序只能在Windows2000环境下编译运行,但是该方法对Windows98下进程的控制也是有借鉴意义的。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
