获取CPU和内存呢信息
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <winsock.h> 4 #pragma comment(lib, "ws2_32.lib") 5 #include <nb30.h> 6 #pragma comment(lib,"NETAPI32.LIB") 7 #include <string> 8 #include <iostream> 9 using namespace std; 10 #include "psapi.h" 11 #pragma comment(lib, "Psapi.lib") 12 13 #include "hardcard.h" 14 //------获取硬盘----------------------------------------------------------------------- 15 16 typedef BOOL(WINAPI *PGETDISKFREESPACEEX)(LPCSTR, 17 PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER); 18 19 BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive, float *out) 20 { 21 PGETDISKFREESPACEEX pGetDiskFreeSpaceEx; 22 __int64 i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; 23 24 DWORD dwSectPerClust, 25 dwBytesPerSect, 26 dwFreeClusters, 27 dwTotalClusters; 28 29 BOOL fResult; 30 31 pGetDiskFreeSpaceEx = (PGETDISKFREESPACEEX) GetProcAddress( 32 GetModuleHandle("kernel32.dll"), 33 "GetDiskFreeSpaceExA"); 34 35 if (pGetDiskFreeSpaceEx) 36 { 37 // printf("\n1.%x\n",pGetDiskFreeSpaceEx); 38 fResult = pGetDiskFreeSpaceEx(pszDrive, 39 (PULARGE_INTEGER)&i64FreeBytesToCaller, 40 (PULARGE_INTEGER)&i64TotalBytes, 41 (PULARGE_INTEGER)&i64FreeBytes); 42 43 // Process GetDiskFreeSpaceEx results. 44 if (fResult) 45 { 46 // printf("剩余字节 = %I64d\n", i64FreeBytes); 47 // printf("总共字节 = %I64d\n", i64TotalBytes); 48 double f = i64FreeBytes, t = i64TotalBytes; 49 for (int i = 0; i < 3; ++i) 50 { 51 f /= 1024; 52 t /= 1024; 53 } 54 out[0] = f; 55 out[1] = t; 56 // printf("%0.2fG,", f); 57 // printf("%0.2fG", t); 58 } 59 /// printf("\n"); 60 return fResult; 61 } 62 else 63 { 64 //printf("\n2.%x\n",pGetDiskFreeSpaceEx); 65 fResult = GetDiskFreeSpaceA(pszDrive, 66 &dwSectPerClust, 67 &dwBytesPerSect, 68 &dwFreeClusters, 69 &dwTotalClusters); 70 71 // Process GetDiskFreeSpace results. 72 if (fResult) 73 { 74 // printf("剩余字节 = I64d",dwFreeClusters*dwSectPerClust*dwBytesPerSect); 75 } 76 //printf("\n"); 77 return fResult; 78 } 79 } 80 __int64 CompareFileTime(FILETIME time1, FILETIME time2) 81 { 82 __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime; 83 __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime; 84 85 return (b - a); 86 } 87 void getcpumemory(int *_cpu, int *_memory) 88 { 89 HANDLE hEvent; 90 BOOL res; 91 92 FILETIME preidleTime; 93 FILETIME prekernelTime; 94 FILETIME preuserTime; 95 96 FILETIME idleTime; 97 FILETIME kernelTime; 98 FILETIME userTime; 99 MEMORYSTATUS memo; 100 101 res = GetSystemTimes(&idleTime, &kernelTime, &userTime); 102 103 preidleTime = idleTime; 104 prekernelTime = kernelTime; 105 preuserTime = userTime; 106 107 hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled 108 109 //while (1) 110 { 111 112 113 WaitForSingleObject(hEvent, 500); //等待500毫秒 114 res = GetSystemTimes(&idleTime, &kernelTime, &userTime); 115 116 int idle = CompareFileTime(preidleTime, idleTime); 117 int kernel = CompareFileTime(prekernelTime, kernelTime); 118 int user = CompareFileTime(preuserTime, userTime); 119 120 int cpu = (kernel + user - idle) * 100 / (kernel + user);//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率 121 int cpuidle = (idle) * 100 / (kernel + user); 122 GlobalMemoryStatus(&memo); 123 //system("cls"); 124 cout << "CPU" << cpu << "%,";// << " CPU空闲率:" <<cpuidle << "%" <<endl; 125 cout << "内存" << memo.dwMemoryLoad << "%;"; 126 preidleTime = idleTime; 127 prekernelTime = kernelTime; 128 preuserTime = userTime; 129 *_cpu = cpu; 130 *_memory = memo.dwMemoryLoad; 131 // Sleep(500); 132 } 133 134 }
1 #pragma once 2 3 BOOL MyGetDiskFreeSpaceEx(LPCSTR pszDrive, float *out); 4 void getcpumemory(int *_cpu, int *_memory);