当前位置:首页 > 通信资讯 > 正文

c++ 结束进程(c 结束程序)

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

说明:

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

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

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

思路:

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

编译环境:

Windows VS2017
代码需要在支持C++11标准的编译器下编译

代码:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 #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] = '\0'; 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; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_46239972/article/details/113523031

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。