设为首页 加入收藏

TOP

在Visual C++应用程序中彻底清除进程(二)
2014-11-23 19:52:52 】 浏览:537
Tags:Visual 应用程序 彻底 清除 进程
e.
//
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 doesnt want to die: abort
   PostMessage(hwnd, WM_CLOSE, 0, 0);
 }
 // Ive 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) { // didnt 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 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; // dont 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)
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC++编程实现对波形数据的频谱分析 下一篇VC++实现混合静态分裂视窗的方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目