Windows編程:開啟/關閉/遍曆程式的類 代碼(C++)

來源:互聯網
上載者:User

類包含4個函數, 啟動程式, 遍曆所有進程, 關閉程式, 遍曆進程依賴的動態連結程式庫.

樣本: Image.exe是預先產生的可執行程式(exe), 啟動程式, 間隔5秒, 關閉程式.

使用方法參加測試程式.

代碼:

/*  * process.h  *  *  Created on: 2014.06.08  *      Author: Spike  */      /*vs 2012*/      #ifndef TRAVERSEPROCESSMODEL_H  #define TRAVERSEPROCESSMODEL_H        #include <iostream>  #include <iomanip>  #include <string>  #include <map>        #include <windows.h>  #include <TlHelp32.h>     //snapshot        class Process   {  public:      bool startProcess (const std::string _name);      bool terminateProcess (const std::string _name);      bool traverseModels (const std::string _name);      bool traverseProcesses (std::map<std::string, int>& _nameID);  };        #endif //TRAVERSEPROCESSMODEL_H                          /*  * process.cpp  *  *  Created on: 2014.06.08  *      Author: Spike  */      /*vs 2012*/      #include "process.h"        using namespace std;        bool Process::startProcess (const std::string name)   {      STARTUPINFO si; //參數設定      memset(&si, 0, sizeof(STARTUPINFO));      si.cb = sizeof(STARTUPINFO);      si.dwFlags = STARTF_USESHOWWINDOW;      si.wShowWindow = SW_SHOW;            PROCESS_INFORMATION pi; //參數結束            /*printf("Please enter the name of process to start:");     std::string name;     cin >> name;*/          if (!CreateProcessA(NULL, (LPSTR)name.c_str(), NULL, NULL, FALSE, 0,NULL,NULL,&si,&pi)) {          cout<<"Start Process Error!"<<endl;          return false;      } else {          cout<<"Start Process Sucess!"<<endl;      }      return true;  }        bool Process::traverseProcesses (std::map<std::string, int>& _nameID)   {      PROCESSENTRY32 pe32;      pe32.dwSize = sizeof(pe32);            HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);      if (hProcessSnap == INVALID_HANDLE_VALUE) {          std::cout << "CreateToolhelp32Snapshot Error!" << std::endl;;          return false;      }            BOOL bResult =Process32First(hProcessSnap, &pe32);            while(bResult)       {          std::string name = pe32.szExeFile;          int id = pe32.th32ProcessID;                //std::cout << "Process Name:" << name << "  " << "ProcessID:" << id<< std::endl;      //更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/        _nameID.insert(std::pair<string, int>(name, id)); //字典儲存          bResult = Process32Next(hProcessSnap,&pe32);      }            CloseHandle(hProcessSnap);            return true;  }        bool Process::terminateProcess (const std::string _name)   {      std::map<std::string, int> nameID;            if (!traverseProcesses(nameID)) {          cout << "Traverse Processes Error!" << endl;      }            DWORD dwId;            /*printf("Please enter the name of process to terminal:");     std::string name;     cin >> name;*/          dwId = nameID[_name];            BOOL bRet = FALSE;            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwId);      if(hProcess != NULL)          bRet = TerminateProcess(hProcess, 0);      CloseHandle(hProcess);            if(bRet){          std::cout << "Terminate Process Success!" << std::endl;      } else {          std::cout << "Terminate Process Error!" << std::endl;          return false;      }            return true;  }        bool Process::traverseModels (const std::string _name)   {      std::map<std::string, int> nameID;            if (!traverseProcesses(nameID)) {          cout << "Traverse Processes Error!" << endl;      }            DWORD dwId;            /*printf("Please enter the name of process to traverse processmodels:");     std::string name;     cin >> name;*/          dwId = nameID[_name];            HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwId);      if(hModuleSnap == INVALID_HANDLE_VALUE){          printf("CreateToolhelp32SnapshotError! \n");          return false;      }            MODULEENTRY32 module32;      module32.dwSize = sizeof(module32);      BOOL bResult = Module32First(hModuleSnap, &module32);            while(bResult){          std::wcout << "Module:" << std::left << std::setw(25) << module32.szModule << "  "            << endl << "Path:" << module32.szExePath << std::endl;          bResult = Module32Next(hModuleSnap, &module32);      }            CloseHandle(hModuleSnap);            return true;        }

測試代碼:

/*  * main.cpp  *  *  Created on: 2014.06.08  *      Author: Spike  */      /*vs 2012*/      #include "process.h"        using namespace std;        int main(void)  {      Process tpm;            const std::string name = "D:/Test/Image.exe";      const std::string programme = "Image.exe";            if (!tpm.startProcess(name)) {          cout << "Start Processe Error!" << endl;      }            Sleep(5000);            if(!tpm.terminateProcess(programme)){          cout << "Terminate Process Error!" << endl;      }            return 0;  }

輸出:

作者:csdn部落格 Spike_King

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。