CSystemStatus.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef __CSystemStatus_h__
  2. #define __CSystemStatus_h__
  3. #include <vector>
  4. #include <string>
  5. typedef struct
  6. {
  7. std::string _strdir; //磁盘名称
  8. float _Total; //磁盘总容量MB
  9. float _OfFree; //磁盘剩余容量MB
  10. }EACHDISKSTATUS,*LPEACHDISKSTATUS;
  11. typedef struct
  12. {
  13. std::string Name; //网卡名称
  14. std::string Description; //网卡描述
  15. std::string Local_IP; //IP地址
  16. std::string Local_Mac; //MAC地址
  17. }NETCARDINFO,*LPNETCARDINFO;
  18. #define SYSSTATE_NONE 0x00000000
  19. #define SYSSTATE_CPU_USAGE 0x00000001
  20. #define SYSSTATE_DISK_READ 0x00000002
  21. #define SYSSTATE_DISK_WRITE 0x00000004
  22. #define SYSSTATE_NET_DOWNLOAD 0x00000008
  23. #define SYSSTATE_NET_UPLOAD 0x00000010
  24. typedef struct
  25. {
  26. BYTE bVersion; // Binary driver version.
  27. BYTE bRevision; // Binary driver revision.
  28. BYTE bReserved; // Not used.
  29. BYTE bIDEDeviceMap; // Bit map of IDE devices.
  30. DWORD fCapabilities; // Bit mask of driver capabilities.
  31. DWORD dwReserved[4]; // For future use.
  32. } GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;
  33. typedef struct
  34. {
  35. USHORT wGenConfig;
  36. USHORT wNumCyls;
  37. USHORT wReserved;
  38. USHORT wNumHeads;
  39. USHORT wBytesPerTrack;
  40. USHORT wBytesPerSector;
  41. USHORT wSectorsPerTrack;
  42. USHORT wVendorUnique[3];
  43. CHAR sSerialNumber[20];
  44. USHORT wBufferType;
  45. USHORT wBufferSize;
  46. USHORT wECCSize;
  47. CHAR sFirmwareRev[8];
  48. CHAR sModelNumber[40];
  49. USHORT wMoreVendorUnique;
  50. USHORT wDoubleWordIO;
  51. USHORT wCapabilities;
  52. USHORT wReserved1;
  53. USHORT wPIOTiming;
  54. USHORT wDMATiming;
  55. USHORT wBS;
  56. USHORT wNumCurrentCyls;
  57. USHORT wNumCurrentHeads;
  58. USHORT wNumCurrentSectorsPerTrack;
  59. ULONG ulCurrentSectorCapacity;
  60. USHORT wMultSectorStuff;
  61. ULONG ulTotalAddressableSectors;
  62. USHORT wSingleWordDMA;
  63. USHORT wMultiWordDMA;
  64. BYTE bReserved[128];
  65. } IDSECTOR, *PIDSECTOR;
  66. #define DFP_GET_VERSION 0x00074080
  67. #define DFP_SEND_DRIVE_COMMAND 0x0007c084
  68. #define DFP_RECEIVE_DRIVE_DATA 0x0007c088
  69. #include "pdh.h"
  70. class CSystemStatus
  71. {
  72. public:
  73. CSystemStatus();
  74. ~CSystemStatus();
  75. public:
  76. void SystemInit(DWORD object = SYSSTATE_CPU_USAGE); //系统初始化(初始化多个项目时使用或运算连接)
  77. void SystemUnInit(); //释放资源
  78. double GetSystemNetDownloadRate(); //获取网络下载速度
  79. double GetSystemNetUploadRate(); //获取网络上传速度
  80. double GetSystemDiskReadRate(); //获取当前磁盘读速率
  81. double GetSystemDiskWriteRate(); //获取当前磁盘写速率
  82. double GetSystemCpuCurrentUsage(); //系统CPU使用率
  83. void GetSystemDiskStatus(std::vector<EACHDISKSTATUS> &vectorDisk); //获取各个磁盘使用状态
  84. void GetSystemDiskStatus(ULONGLONG& AllDiskTotal, ULONGLONG& AllDiskFree); //获取系统总得磁盘使用状态
  85. void GetSystemCurrentDiskStatus(ULONGLONG& TatolMB, ULONGLONG& FreeCaller); //获取当前磁盘使用状态
  86. double GetSystemCurrentDiskUsage(); //获取当前磁盘使用率
  87. BOOL GetPhysicalMemoryState(ULONGLONG& totalPhysMem, ULONGLONG& physMemUsed);//获取物理内存状态
  88. double GetTotalPhysicalMemory(); //获取可用内存大小
  89. double GetTotalPhysicalMemoryFree(); //获取空闲内存
  90. double GetTotalPhysicalMemoryUsed(); //获取已使用内存大小
  91. double GetPhysicalMemoryUsage(); //获取内存使用率
  92. void GetNetCardInfo(std::vector<NETCARDINFO> &vectorNetCard); //获取网卡信息
  93. void GetOsInfo(std::string& strOSName, std::string& strOSArch); //获取操作系统信息
  94. void GetCpuInfo(std::string &CPUinfo); //获取CPU硬件信息
  95. void GetCPUid(std::string &CPUid); //获取CPUid
  96. BOOL GetHDSerial(std::string &HDSerial); //获取硬盘物理序列号(需要管理员权限)
  97. private:
  98. PDH_HQUERY m_Query;
  99. PDH_HCOUNTER m_CpuTotal,m_DiskRead,m_DiskWrite,m_NetDownload,m_NetUpload;
  100. public:
  101. /*
  102. 参数:const char *cmd
  103. systeminfo:查看详细的系统信息
  104. wmic logicaldisk:查看盘符
  105. fsutil volume diskfree + 盘符名称:查看某个盘符的容量大小。
  106. wmic path win32_physicalmedia get SerialNumber;查看硬盘系列号
  107. wmic diskdrive get serialnumber;查看硬盘系列号(和上面效果一样)
  108. wmic cpu:查看CPU运行信息
  109. wmic cpu list brief:查看CPU硬件信息
  110. wmic memorychip;查看系统内存信息
  111. wmic bios:查看系统的bios信息
  112. wmic memorychip list brief:查看内存条数
  113. wmic memcache list brief:查看缓存内存
  114. wmic diskdrive:查看磁盘详细信息
  115. wmic diskdrive get Name, Model:查看硬盘名称,型号(使用get)
  116. ...
  117. */
  118. std::string execCmd(const char *cmd)
  119. {
  120. char buffer[128] = { 0 };
  121. std::string result;
  122. FILE *pipe = _popen(cmd, "r");
  123. if (!pipe) throw std::runtime_error("_popen() failed!");
  124. while (!feof(pipe))
  125. {
  126. if (fgets(buffer, 128, pipe) != NULL)
  127. result += buffer;
  128. }
  129. _pclose(pipe);
  130. return result;
  131. }
  132. _inline void ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
  133. {
  134. USHORT i = 0;
  135. CHAR temp= '\0';
  136. for (i = 0; i < uscStrSize; i+=2)
  137. {
  138. temp = szString[i];
  139. szString[i] = szString[i+1];
  140. szString[i+1] = temp;
  141. }
  142. }
  143. };
  144. #endif