用Visual C++干干净净地清除进程(3)

分类: VC.NET   出处:iocblog整理  更新时间:2008-07-25   添加到收藏  

//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "EnumProc.h"
#define tpf _tprintf // to save typing
typedef list 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
  if (isswitch(argv[i][0])) {
   for (UINT j=1; 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(0x%08x)\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下进程的控制也是有借鉴意义的。

上一页 [1] [2] [3]


Tag: 进程



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。