用C++写一个进程管理系统
时间: 2023-11-08 19:03:57 浏览: 194
用C++写的进程管理
5星 · 资源好评率100%
好的,进程管理系统可以包含以下功能:
1. 创建进程
2. 终止进程
3. 列出所有进程
4. 显示进程信息
5. 暂停进程
6. 恢复进程
以下是一个简单的C++代码示例:
```c++
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
using namespace std;
void listProcesses() {
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE) {
cout << "Error: Unable to create process snapshot" << endl;
return;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcessSnap, &pe32)) {
CloseHandle(hProcessSnap);
cout << "Error: Unable to get first process" << endl;
return;
}
cout << "Processes:" << endl;
do {
cout << " " << pe32.th32ProcessID << " " << pe32.szExeFile << endl;
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
}
void showProcessInfo(DWORD pid) {
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (hProcess == NULL) {
cout << "Error: Unable to open process" << endl;
return;
}
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
cout << "Process ID: " << pid << endl;
cout << "Working Set Size: " << pmc.WorkingSetSize << endl;
cout << "Pagefile Usage: " << pmc.PagefileUsage << endl;
cout << "Peak Working Set Size: " << pmc.PeakWorkingSetSize << endl;
cout << "Peak Pagefile Usage: " << pmc.PeakPagefileUsage << endl;
} else {
cout << "Error: Unable to get process memory info" << endl;
}
CloseHandle(hProcess);
}
void createProcess(string cmd) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(NULL, const_cast<char*>(cmd.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
cout << "Error: Unable to create process" << endl;
return;
}
cout << "Process created with ID: " << pi.dwProcessId << endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
void terminateProcess(DWORD pid) {
HANDLE hProcess;
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (hProcess == NULL) {
cout << "Error: Unable to open process" << endl;
return;
}
if (!TerminateProcess(hProcess, 0)) {
cout << "Error: Unable to terminate process" << endl;
} else {
cout << "Process terminated" << endl;
}
CloseHandle(hProcess);
}
void suspendProcess(DWORD pid) {
HANDLE hProcess;
hProcess = OpenProcess(PROCESS_SUSPEND_RESUME, FALSE, pid);
if (hProcess == NULL) {
cout << "Error: Unable to open process" << endl;
return;
}
if (SuspendThread(hProcess) == (DWORD)-1) {
cout << "Error: Unable to suspend process" << endl;
} else {
cout << "Process suspended" << endl;
}
CloseHandle(hProcess);
}
void resumeProcess(DWORD pid) {
HANDLE hProcess;
hProcess = OpenProcess(PROCESS_SUSPEND_RESUME, FALSE, pid);
if (hProcess == NULL) {
cout << "Error: Unable to open process" << endl;
return;
}
if (ResumeThread(hProcess) == (DWORD)-1) {
cout << "Error: Unable to resume process" << endl;
} else {
cout << "Process resumed" << endl;
}
CloseHandle(hProcess);
}
int main() {
int choice;
DWORD pid;
string cmd;
while (true) {
cout << "1. List processes" << endl;
cout << "2. Show process info" << endl;
cout << "3. Create process" << endl;
cout << "4. Terminate process" << endl;
cout << "5. Suspend process" << endl;
cout << "6. Resume process" << endl;
cout << "0. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
listProcesses();
break;
case 2:
cout << "Enter process ID: ";
cin >> pid;
showProcessInfo(pid);
break;
case 3:
cout << "Enter command: ";
cin.ignore();
getline(cin, cmd);
createProcess(cmd);
break;
case 4:
cout << "Enter process ID: ";
cin >> pid;
terminateProcess(pid);
break;
case 5:
cout << "Enter process ID: ";
cin >> pid;
suspendProcess(pid);
break;
case 6:
cout << "Enter process ID: ";
cin >> pid;
resumeProcess(pid);
break;
case 0:
return 0;
default:
cout << "Invalid choice" << endl;
break;
}
cout << endl;
}
return 0;
}
```
该代码使用了Windows API函数来实现各种进程管理功能。您可以根据需要进行修改和扩展。
阅读全文