本文共 3673 字,大约阅读时间需要 12 分钟。
//1.获取Windows系统内存使用率
- DWORD getWin_MemUsage(){
- MEMORYSTATUS ms;
- ::GlobalMemoryStatus(&ms);
- return ms.dwMemoryLoad;
- }
//2.获取windowsCPU使用率
- __int64 CompareFileTime(FILETIME time1, FILETIME time2)
- {
- __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime;
- __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime;
-
- return (b - a);
- }
- void getWin_CpuUsage(){
- HANDLE hEvent;
- BOOL res;
- FILETIME preidleTime;
- FILETIME prekernelTime;
- FILETIME preuserTime;
- FILETIME idleTime;
- FILETIME kernelTime;
- FILETIME userTime;
-
- res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
- preidleTime = idleTime;
- prekernelTime = kernelTime;
- preuserTime = userTime;
-
- hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
-
- while (true){
- WaitForSingleObject(hEvent, 1000);
- res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
-
- __int64 idle = CompareFileTime(preidleTime, idleTime);
- __int64 kernel = CompareFileTime(prekernelTime, kernelTime);
- __int64 user = CompareFileTime(preuserTime, userTime);
-
- __int64 cpu = (kernel + user - idle) * 100 / (kernel + user);
- __int64 cpuidle = (idle)* 100 / (kernel + user);
- cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" << cpuidle << "%" << endl;
-
- preidleTime = idleTime;
- prekernelTime = kernelTime;
- preuserTime = userTime;
- }
- }
- int getWin_DiskUsage(){
- int DiskCount = 0;
- DWORD DiskInfo = GetLogicalDrives();
-
- while (DiskInfo)
- {
- if (DiskInfo & 1)
- {
- ++DiskCount;
- }
- DiskInfo = DiskInfo >> 1;
-
- }
- cout << "Logical Disk Number:" << DiskCount << endl;
-
- int DSLength = GetLogicalDriveStrings(0, NULL);
-
- char* DStr = new char[DSLength];
- GetLogicalDriveStrings(DSLength, (LPTSTR)DStr);
-
-
- int DType;
- int si = 0;
- BOOL fResult;
- unsigned _int64 i64FreeBytesToCaller;
- unsigned _int64 i64TotalBytes;
- unsigned _int64 i64FreeBytes;
-
- for (int i = 0; i<DSLength / 4; ++i)
- {
- char dir[3] = { DStr[si], ':', '\\' };
- cout << dir;
- DType = GetDriveType(DStr + i * 4);
-
- if (DType == DRIVE_FIXED)
- {
- cout << "Hard Disk";
- }
- else if (DType == DRIVE_CDROM)
- {
- cout << "CD-ROM";
- }
- else if (DType == DRIVE_REMOVABLE)
- {
- cout << "Removable Disk";
- }
- else if (DType == DRIVE_REMOTE)
- {
- cout << "Network Disk";
- }
- else if (DType == DRIVE_RAMDISK)
- {
- cout << "Virtual RAM Disk";
- }
- else if (DType == DRIVE_UNKNOWN)
- {
- cout << "Unknown Device";
- }
-
- fResult = GetDiskFreeSpaceEx(
- dir,
- (PULARGE_INTEGER)&i64FreeBytesToCaller,
- (PULARGE_INTEGER)&i64TotalBytes,
- (PULARGE_INTEGER)&i64FreeBytes);
-
- if (fResult)
- {
- cout << " totalspace:" << (float)i64TotalBytes / 1024 / 1024 << " MB";
- cout << " freespace:" << (float)i64FreeBytesToCaller / 1024 / 1024 << " MB";
- }
- else
- {
- cout << " 设备未准备好";
- }
- cout << endl;
- si += 4;
- }
- return 0;
- }
//main函数
-
- int main( int argc, char **argv )
- {
-
- DWORD dwValue = getWin_MemUsage();
- printf("内存使用率为 %d %% \n",dwValue);
-
-
-
- getWin_DiskUsage();
- return 0;
- }
-
转载地址:http://urlja.baihongyu.com/