| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #ifndef PROCESSMONITOR_H
- #define PROCESSMONITOR_H
- #include "qdebug.h"
- #include <chrono>
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <thread>
- #include <vector>
- #include <windows.h>
- #include <QString>
- #include <psapi.h>
- #include <tlhelp32.h>
- BOOL EnablePrivilege(LPCWSTR privilege);
- class ProcessMonitor
- {
- public:
- struct ProcessInfo
- {
- std::string name;
- std::string processName;
- DWORD pid;
- DWORD parentPid;
- FILETIME creationTime;
- FILETIME exitTime;
- FILETIME kernelTime;
- FILETIME userTime;
- };
- ProcessMonitor();
- // void startMonitoring()
- // {
- // while (true) {
- // EnablePrivilege(SE_DEBUG_NAME);
- // // 全局 父进程 和 explorer 的子进程
- // std::vector<std::shared_ptr<ProcessInfo>> timeProcess = checkProcesses();
- // std::this_thread::sleep_for(std::chrono::seconds(5000));
- // }
- // }
- // 获取 父进程 以及 explorer 的子进程 信息 使用过滤后的
- std::vector<std::shared_ptr<ProcessInfo>> checkProcesses();
- private:
- // 过滤信息
- std::map<std::string, bool> filter;
- // 通过进程pid 获取根节点的 pid
- DWORD getRootPid(DWORD pid);
- // 全部根节点
- std::set<std::shared_ptr<ProcessInfo>> root();
- // 进程详细信息
- std::shared_ptr<ProcessInfo> getProcessInfo(DWORD pid);
- private:
- std::vector<std::shared_ptr<ProcessInfo>> data;
- std::map<int, std::shared_ptr<ProcessInfo>> mapData; // 辅助快速定位
- // void printProcessRunTimes()
- // {
- // std::cout << "\nPrinting process run times:\n";
- // std::cout << "---------------------------------\n";
- // for (const auto& entry : processMap) {
- // const ProcessInfo& info = entry.second;
- // // 计算进程运行时间(秒)
- // FILETIME currentTime;
- // GetSystemTimeAsFileTime(¤tTime);
- // ULARGE_INTEGER creation, current;
- // creation.LowPart = info.creationTime.dwLowDateTime;
- // creation.HighPart = info.creationTime.dwHighDateTime;
- // current.LowPart = currentTime.dwLowDateTime;
- // current.HighPart = currentTime.dwHighDateTime;
- // ULONGLONG elapsedSeconds = (current.QuadPart - creation.QuadPart) / 10000000;
- // // 计算天、小时、分钟和秒
- // ULONGLONG days = elapsedSeconds / 86400; // 1天 = 86400秒
- // ULONGLONG hours = (elapsedSeconds % 86400) / 3600; // 1小时 = 3600秒
- // ULONGLONG minutes = (elapsedSeconds % 3600) / 60; // 1分钟 = 60秒
- // ULONGLONG seconds = elapsedSeconds % 60; // 剩余秒数
- // // 打印进程的运行时间
- // std::cout << std::setfill('0') << std::setw(2) << days << " " //
- // << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":"
- // << std::setw(2) << seconds << " " //
- // << "Process " << info.name << " (PID: " << info.pid
- // << " PPID: " << info.parentPid << ") has been running for " //
- // << days << " days, " << hours << " hours, " << minutes << " minutes, and "
- // << seconds << " seconds.\n";
- // }
- // std::cout << "---------------------------------\n";
- // }
- };
- #endif // PROCESSMONITOR_H
|