processmonitor.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef PROCESSMONITOR_H
  2. #define PROCESSMONITOR_H
  3. #include "qdebug.h"
  4. #include <chrono>
  5. #include <iostream>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <thread>
  10. #include <vector>
  11. #include <windows.h>
  12. #include <QString>
  13. #include <psapi.h>
  14. #include <tlhelp32.h>
  15. BOOL EnablePrivilege(LPCWSTR privilege);
  16. class ProcessMonitor
  17. {
  18. public:
  19. struct ProcessInfo
  20. {
  21. std::string name;
  22. std::string processName;
  23. DWORD pid;
  24. DWORD parentPid;
  25. FILETIME creationTime;
  26. FILETIME exitTime;
  27. FILETIME kernelTime;
  28. FILETIME userTime;
  29. };
  30. ProcessMonitor();
  31. // void startMonitoring()
  32. // {
  33. // while (true) {
  34. // EnablePrivilege(SE_DEBUG_NAME);
  35. // // 全局 父进程 和 explorer 的子进程
  36. // std::vector<std::shared_ptr<ProcessInfo>> timeProcess = checkProcesses();
  37. // std::this_thread::sleep_for(std::chrono::seconds(5000));
  38. // }
  39. // }
  40. // 获取 父进程 以及 explorer 的子进程 信息 使用过滤后的
  41. std::vector<std::shared_ptr<ProcessInfo>> checkProcesses();
  42. private:
  43. // 过滤信息
  44. std::map<std::string, bool> filter;
  45. // 通过进程pid 获取根节点的 pid
  46. DWORD getRootPid(DWORD pid);
  47. // 全部根节点
  48. std::set<std::shared_ptr<ProcessInfo>> root();
  49. // 进程详细信息
  50. std::shared_ptr<ProcessInfo> getProcessInfo(DWORD pid);
  51. private:
  52. std::vector<std::shared_ptr<ProcessInfo>> data;
  53. std::map<int, std::shared_ptr<ProcessInfo>> mapData; // 辅助快速定位
  54. // void printProcessRunTimes()
  55. // {
  56. // std::cout << "\nPrinting process run times:\n";
  57. // std::cout << "---------------------------------\n";
  58. // for (const auto& entry : processMap) {
  59. // const ProcessInfo& info = entry.second;
  60. // // 计算进程运行时间(秒)
  61. // FILETIME currentTime;
  62. // GetSystemTimeAsFileTime(&currentTime);
  63. // ULARGE_INTEGER creation, current;
  64. // creation.LowPart = info.creationTime.dwLowDateTime;
  65. // creation.HighPart = info.creationTime.dwHighDateTime;
  66. // current.LowPart = currentTime.dwLowDateTime;
  67. // current.HighPart = currentTime.dwHighDateTime;
  68. // ULONGLONG elapsedSeconds = (current.QuadPart - creation.QuadPart) / 10000000;
  69. // // 计算天、小时、分钟和秒
  70. // ULONGLONG days = elapsedSeconds / 86400; // 1天 = 86400秒
  71. // ULONGLONG hours = (elapsedSeconds % 86400) / 3600; // 1小时 = 3600秒
  72. // ULONGLONG minutes = (elapsedSeconds % 3600) / 60; // 1分钟 = 60秒
  73. // ULONGLONG seconds = elapsedSeconds % 60; // 剩余秒数
  74. // // 打印进程的运行时间
  75. // std::cout << std::setfill('0') << std::setw(2) << days << " " //
  76. // << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":"
  77. // << std::setw(2) << seconds << " " //
  78. // << "Process " << info.name << " (PID: " << info.pid
  79. // << " PPID: " << info.parentPid << ") has been running for " //
  80. // << days << " days, " << hours << " hours, " << minutes << " minutes, and "
  81. // << seconds << " seconds.\n";
  82. // }
  83. // std::cout << "---------------------------------\n";
  84. // }
  85. };
  86. #endif // PROCESSMONITOR_H