KillProc : Kills processes, lists modules in the processes

Download killproc.zip

Synopsis:

killproc.cpp


killproc.cpp

Synopsis
#include <iostream>
using namespace std;

#include <wtypes.h>
#include <psapi.h>
#pragma comment (lib, "psapi.lib")

void ErrorOut(char errstring[30])
  {
  DWORD Error= GetLastError();
  cout << "Error on "<< errstring << " = " << Error << endl;
  }

bool IsCommonModule(char* pname)
  {
  return (stricmp(pname, "cmd.exe") == 0 ||
    stricmp(pname, "msdev.exe") == 0 ||
    stricmp(pname, "devenv.exe") == 0 ||
    stricmp(pname, "appctl32.exe") == 0 ||
    stricmp(pname, "winlogon.exe") == 0 ||
    stricmp(pname, "services.exe") == 0 ||
    stricmp(pname, "lsass.exe") == 0 ||
    stricmp(pname, "smss.exe") == 0 ||
    stricmp(pname, "nddeagnt.exe") == 0 ||
    stricmp(pname, "rpcss.exe") == 0 ||
    stricmp(pname, "spoolss.exe") == 0 ||
    stricmp(pname, "outlook.exe") == 0 ||
    stricmp(pname, "explorer.exe") == 0 ||
    stricmp(pname, "mapisp32.exe") == 0 ||
    stricmp(pname, "pcmwin32.exe") == 0 ||
    stricmp(pname, "climonnt.exe") == 0 ||
    stricmp(pname, "iexplore.exe") == 0 ||
    stricmp(pname, "taskmgr.exe") == 0 ||
    stricmp(pname, "notepad.exe") == 0 ||
    stricmp(pname, "conTEXT.exe") == 0 ||
    stricmp(pname, "ssexp.exe") == 0 ||
    stricmp(pname, "killproc.exe") == 0
    );
  }

void KillModule(HANDLE hProcess, DWORD pid, char* pname)
  {
  //if (
  //  stricmp(pname, "add_your_exe_here_if_you_want.exe") == 0
  //  )
  //  {
  //  BOOL TermSucc;
  //  TermSucc = TerminateProcess(hProcess, 0);
  //  cout << "Pid=" << pid << " " << pname;
  //  if (TermSucc)
  //    cout << " killed." << endl;
  //  else
  //    cout << " kill failed GLE=" << GetLastError() << endl;
  //  }
  }

//--------------------------
bool PrintModuleInfo(DWORD pid, HANDLE hProcess, HMODULE module, int nummodules, int j)
  {
  char name[265];
  name[0] = 0;
  GetModuleFileNameEx(
    hProcess,      // handle to the process
    module,        // handle to the module
    name,          // buffer that receives the base name
    sizeof(name)); // size of the buffer

  char pname[265];
  pname[0] = 0;
  GetModuleBaseName( hProcess, module, pname, sizeof(pname));

  strlwr(pname);
  if (j == 0 && IsCommonModule(pname))
    return true;

  if (j == 0)
    cout << pname << " (" << name << ")\nPid=" << pid << " Number of Modules: " << nummodules - 1 << endl;
  else
    cout << "    Module[" << module << "]=" << pname << "  (" << name << ")" << endl;
  KillModule(hProcess, pid, pname);
  return false;
  }

//--------------------------
void ListModules(DWORD pid, HANDLE hProcess)
  {
  HMODULE modules[500]; 
  memset(modules, 0x00, sizeof(modules));

  DWORD bNeeded = 0;
  BOOL b = EnumProcessModules(
    hProcess,        // handle to the process
    modules,         // array to receive the module handles
    sizeof(modules), // size of the array
    &bNeeded         // receives the number of bytes returned
    );
  if (!b)
    {
    cout << "Pid=" << pid << " EnumProcessModules failed: GLE= " << GetLastError() << endl;
    return;
    }

  int nummodules = bNeeded / sizeof(DWORD);
  int j;
  for(j = 0; j < nummodules; ++j)
    {
    if (PrintModuleInfo(pid, hProcess, modules[j], nummodules, j))
      break;
    }

  if (j >= nummodules)
    cout << "    End of module list" << endl;
  }

//--------------------------
void  HandleProcess(DWORD pid)
  {
  HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pid );
  if (hProcess == NULL)
    {
    cout << "Pid=" << pid << " open failed: GLE= " << GetLastError() << endl;
    return;
    }
  ListModules(pid, hProcess);

  CloseHandle(hProcess);
  }

//--------------------------
void ListProcesses()
  {
  DWORD pids[500];
  memset(pids, 0x0, sizeof(pids));

  DWORD bNeeded = 0;
  BOOL b = EnumProcesses(
    pids,         // array to receive the process identifiers
    sizeof(pids), // size of the array
    &bNeeded      // receives the number of bytes returned
    );
  if (!b)
    {
    ErrorOut("EnumProcesses");
    exit(1);
    }

  int numpids = bNeeded / sizeof(DWORD);
  cout << "There are " << numpids << " processes." << endl;

  for (int i = 0; i < numpids; ++i)
    HandleProcess(pids[i]);

  cout << "End of process list" << endl;
  }

//--------------------------
int main(int argc, char **argv)
  {
  ListProcesses();
  return 0;
  }






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010