c/c++语言开发共享C++实现结束应用进程小工具

c++实现结束应用进程小工具(windows)说明:在电脑上一些软件或系统有时可能会将程序偷偷运行在后台,占用计算机资源的情况。一般我们通过可以找到程序文件所在位置,禁止程序启动的方法解决这个问题,但

c++实现结束应用进程小工具(windows)

说明:

在电脑上一些软件或系统有时可能会将程序偷偷运行在后台,占用计算机资源的情况。一般我们通过可以找到程序文件所在位置,禁止程序启动的方法解决这个问题,但也可以通过从任务管理器直接结束进程方法做到。从而减少无用程序对计算机资源的占用。

该程序可以自动检查设定进程名称的进程是否正在运行,如果是则结束该进程,从而免去手动关闭的步骤。

使用步骤为在该程序exe文件目录下names.txt文件中(可改变)将需要结束的进程名写在文件中,多个进程名以换行分割,然后点击exe程序执行。

思路:

封装获取系统进程名称、id、结束系统进程方法。从文件中获取需要结束进程的名称,根据名称结束进程。

编译环境:

windows vs2017
代码需要在支持c++11标准的编译器下编译

代码:

  #include <iostream>  #include <windows.h>  #include <tlhelp32.h>  #include <vector>  #include <fstream>  #include <map>  using namespace std;    // 需要关闭进程免费精选名字大全所在文件路径  #define close_file_path "names.txt"  // 每check_interval时间(毫秒)后检查并结束一次进程  #define check_interval 3000    class controller {    private:      // 进程信息结构体,包含进程id和进程名   struct pinfo {    long pid;    wchar_t *pname;      pinfo(long pid, wchar_t *pname) : pid(pid), pname(pname) {}   };     // 根据pid关闭进程   static int closeprocess(unsigned long pid) {      handle hprocess = openprocess(process_terminate, false, pid);       if (hprocess) {     terminateprocess(hprocess, -1);    }        return 0;   }     // 获取所有进程信息   static map<wstring, long> getprocessinfo() {    handle hprocess = createtoolhelp32snapshot(th32cs_snapprocess, 0);    processentry32 currentprocess;    currentprocess.dwsize = sizeof(currentprocess);      map<wstring, long> pinfosmap = map<wstring, long>();      process32first(hprocess, &currentprocess);      wchar_t *pnamestr;    bool flag = true;      while (flag) {     pnamestr = new wchar[max_path];       // wcscpy(pnamestr, currentprocess.szexefile);     wcscpy_s(pnamestr, wcslen(currentprocess.szexefile) + 1, currentprocess.szexefile);       pinfosmap.insert(pair<wstring, long>(pnamestr, currentprocess.th32processid));     flag = process32next(hprocess, &currentprocess);    }      return pinfosmap;   }     // char*转wchar*   static wchar_t * chartowchar(const char* cchar)   {    wchar_t *m_wchar;    int len = multibytetowidechar(cp_acp, 0, cchar, strlen(cchar), null, 0);    m_wchar = new wchar_t[len + 1];    multibytetowidechar(cp_acp, 0, cchar, strlen(cchar), m_wchar, len);    m_wchar[len] = '';    return m_wchar;   }     // 循环获取需要关闭的进程名   template <typename callback>   static void forclosepname(callback callback) {    ifstream examplefile(close_file_path);    if (!examplefile.is_open()) {     cout << "error opening file";     exit(1);    }      char buffer[260];        while (!examplefile.eof()) {       examplefile.getline(buffer, 260);       callback(buffer);    }   }     // 将需要关闭的进程名保存到vector<wchar_t*>中返回   vector<wchar_t*> getclosepname() {    vector<wchar_t*> closepnames =  vector<wchar_t*>();    forclosepname([&](auto pname) {     closepnames.push_back(chartowchar(pname));    });    return closepnames;   }    public:   controller() {      // 需要结束的进程名    vector<wchar_t*> closepnames = getclosepname();    // 所有进程信息map    map<wstring, long> processmap;      while (true) {       processmap = getprocessinfo();       for (auto pname : closepnames) {            //printf("%ls n", pname);        long closepid = processmap[pname];        if (closepid == 0 || wstring(pname) == l"系统空闲进程")       continue;        closeprocess(closepid);     }       sleep(check_interval);    }   }  };    int main() {     controller();     return 0;  }

以上就是c/c++开发分享C++实现结束应用进程小工具的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/609344.html

(0)
上一篇 2021年5月17日
下一篇 2021年5月17日

精彩推荐