你的代码存在如下bug:未定义标识符PROCESS_MEMORY_COUNTERS和GetProcessMemoryInfo
时间: 2024-03-05 20:50:04 浏览: 237
非常抱歉,您所指出的问题是由于我在代码中未添加必要的头文件所致,以下是修复后的代码:
```cpp
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
using namespace std;
int main() {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
BOOL bMore = Process32First(hSnapshot, &pe32);
cout << "Process List:" << endl;
while (bMore) {
cout << "Process Name: " << pe32.szExeFile << endl;
cout << "PID: " << pe32.th32ProcessID << endl;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
if (hProcess) {
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) {
cout << "Working Set Size: " << pmc.WorkingSetSize / 1024 << "KB" << endl;
}
CloseHandle(hProcess);
}
bMore = Process32Next(hSnapshot, &pe32);
}
CloseHandle(hSnapshot);
return 0;
}
```
在代码中添加了 `psapi.h` 头文件,该头文件中定义了 `PROCESS_MEMORY_COUNTERS` 和 `GetProcessMemoryInfo`。这样就可以解决未定义标识符的问题了。感谢您指出代码中的漏洞!
阅读全文