且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C/C++获取操作系统、CPU、内存信息(windows和linux)

更新时间:2022-06-29 17:20:37

有时候需要在工程里面获取一些系统或者硬件的信息,比如系统版本,cpu,内存,显卡,硬盘等,作为后续软件功能判断的依据,甚至参与性能算法自适应建模

Windows

操作系统和内存信息在windows下通过系统的API来获取,CPU信息则需要需要通过底层CPUID指令取得

代码:

 

[cpp] view plain copy
 
 print?
  1. #include <iostream>   
  2. #include <string>  
  3. #include <windows.h>    
  4.   
  5. #pragma warning(disable: 4996) // avoid GetVersionEx to be warned  
  6.   
  7. // ***** global macros ***** //  
  8. static const int kMaxInfoBuffer = 256;  
  9. #define  GBYTES  1073741824    
  10. #define  MBYTES  1048576    
  11. #define  KBYTES  1024    
  12. #define  DKBYTES 1024.0    
  13.   
  14. // ---- get os info ---- //  
  15. void getOsInfo()  
  16. {  
  17.     // get os name according to version number  
  18.     OSVERSIONINFO osver = {sizeof(OSVERSIONINFO)};  
  19.     GetVersionEx(&osver);  
  20.     std::string os_name;  
  21.     if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0)  
  22.         os_name = "Windows 2000";  
  23.     else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1)  
  24.         os_name = "Windows XP";  
  25.     else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0)  
  26.         os_name = "Windows 2003";  
  27.     else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2)  
  28.         os_name = "windows vista";  
  29.     else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1)  
  30.         os_name = "windows 7";  
  31.     else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2)  
  32.         os_name = "windows 10";  
  33.       
  34.     std::cout << "os name: " << os_name << std::endl;  
  35.     std::cout << "os version: " << osver.dwMajorVersion << '.' << osver.dwMinorVersion << std::endl;  
  36.   
  37. }  
  38.   
  39. // ---- get cpu info ---- //  
  40. // save 4 register variables  
  41. DWORD deax;  
  42. DWORD debx;  
  43. DWORD decx;  
  44. DWORD dedx;  
  45.   
  46. // init cpu in assembly language  
  47. void initCpu(DWORD veax)    
  48. {  
  49.     __asm  
  50.     {  
  51.         mov eax, veax  
  52.         cpuid  
  53.             mov deax, eax  
  54.             mov debx, ebx  
  55.             mov decx, ecx  
  56.             mov dedx, edx  
  57.     }  
  58. }  
  59.   
  60. long getCpuFreq()         
  61. {  
  62.     int start, over;  
  63.     _asm  
  64.     {  
  65.         RDTSC  
  66.         mov start, eax  
  67.     }  
  68.     Sleep(50);  
  69.     _asm  
  70.     {  
  71.         RDTSC  
  72.         mov over, eax  
  73.     }  
  74.     return (over - start) / 50000;  
  75. }  
  76.   
  77. std::string getManufactureID()      
  78. {  
  79.     char manuID[25];  
  80.     memset(manuID, 0, sizeof(manuID));  
  81.   
  82.     initCpu(0);          
  83.     memcpy(manuID + 0, &debx, 4); // copy to array  
  84.     memcpy(manuID + 4, &dedx, 4);  
  85.     memcpy(manuID + 8, &decx, 4);  
  86.   
  87.     return manuID;  
  88. }  
  89.   
  90. std::string getCpuType()  
  91. {  
  92.     const DWORD id = 0x80000002; // start 0x80000002 end to 0x80000004  
  93.     char cpuType[49];  
  94.     memset(cpuType, 0, sizeof(cpuType));  
  95.   
  96.     for (DWORD t = 0; t < 3; t++)  
  97.     {  
  98.         initCpu(id + t);  
  99.           
  100.         memcpy(cpuType + 16 * t + 0, &deax, 4);  
  101.         memcpy(cpuType + 16 * t + 4, &debx, 4);  
  102.         memcpy(cpuType + 16 * t + 8, &decx, 4);  
  103.         memcpy(cpuType + 16 * t + 12, &dedx, 4);  
  104.     }  
  105.   
  106.     return cpuType;  
  107. }  
  108.   
  109. void getCpuInfo()  
  110. {  
  111.     std::cout << "CPU main frequency: " << getCpuFreq() << "MHz" << std::endl;  
  112.     std::cout << "CPU manufacture: " << getManufactureID() << std::endl;  
  113.     std::cout << "CPU type: " << getCpuType() << std::endl;  
  114. }  
  115.   
  116. // ---- get memory info ---- //  
  117. void getMemoryInfo()  
  118. {  
  119.     std::string memory_info;  
  120.     MEMORYSTATUSEX statusex;  
  121.     statusex.dwLength = sizeof(statusex);  
  122.     if (GlobalMemoryStatusEx(&statusex))  
  123.     {  
  124.         unsigned long long total = 0, remain_total = 0, avl = 0, remain_avl = 0;  
  125.         double decimal_total = 0, decimal_avl = 0;  
  126.         remain_total = statusex.ullTotalPhys % GBYTES;  
  127.         total = statusex.ullTotalPhys / GBYTES;  
  128.         avl = statusex.ullAvailPhys / GBYTES;  
  129.         remain_avl = statusex.ullAvailPhys % GBYTES;  
  130.         if (remain_total > 0)  
  131.             decimal_total = (remain_total / MBYTES) / DKBYTES;  
  132.         if (remain_avl > 0)  
  133.             decimal_avl = (remain_avl / MBYTES) / DKBYTES;  
  134.   
  135.         decimal_total += (double)total;  
  136.         decimal_avl += (double)avl;  
  137.         char  buffer[kMaxInfoBuffer];  
  138.         sprintf_s(buffer, kMaxInfoBuffer, "total %.2f GB (%.2f GB available)", decimal_total, decimal_avl);  
  139.         memory_info.append(buffer);  
  140.     }  
  141.     std::cout << memory_info << std::endl;  
  142. }  
  143.   
  144. int main(int argc, char *argv[])  
  145. {  
  146.     std::cout << "===os information===" << std::endl;  
  147.     getOsInfo();  
  148.   
  149.     std::cout << "===cpu infomation===" << std::endl;  
  150.     getCpuInfo();  
  151.   
  152.     std::cout << "===memory information===" << std::endl;  
  153.     getMemoryInfo();  
  154.   
  155.     system("pause");  
  156.     return 0;  
  157. }  

 

 

结果:

 

[plain] view plain copy
 
 print?
  1. ===os information===  
  2. os name: windows 10  
  3. os version: 6.2  
  4. ===cpu infomation===  
  5. CPU main frequency: 2612MHz  
  6. CPU manufacture: GenuineIntel  
  7. CPU type:        Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz  
  8. ===memory information===  
  9. total 7.86 GB (1.38 GB available)  

 

 

Linux
linux下很多信息都是存放着系统的/proc目录下,因此读文件就可以获取到了

代码:

 

[cpp] view plain copy
 
 print?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. // read the linux config files to get system info  
  6.   
  7. void getOsInfo()  
  8. {  
  9.     FILE *fp = fopen("/proc/version", "r");    
  10.     if(NULL == fp)     
  11.         printf("failed to open version\n");     
  12.     char szTest[1000] = {0};    
  13.     while(!feof(fp))    
  14.     {    
  15.         memset(szTest, 0, sizeof(szTest));    
  16.         fgets(szTest, sizeof(szTest) - 1, fp); // leave out \n  
  17.         printf("%s", szTest);    
  18.     }    
  19.     fclose(fp);   
  20. }  
  21.   
  22. void getCpuInfo()  
  23. {  
  24.     FILE *fp = fopen("/proc/cpuinfo", "r");    
  25.     if(NULL == fp)     
  26.         printf("failed to open cpuinfo\n");     
  27.     char szTest[1000] = {0};     
  28.     // read file line by line   
  29.     while(!feof(fp))    
  30.     {    
  31.         memset(szTest, 0, sizeof(szTest));    
  32.         fgets(szTest, sizeof(szTest) - 1, fp); // leave out \n  
  33.         printf("%s", szTest);    
  34.     }    
  35.     fclose(fp);   
  36. }  
  37.   
  38. void getMemoryInfo()  
  39. {  
  40.     FILE *fp = fopen("/proc/meminfo", "r");    
  41.     if(NULL == fp)     
  42.         printf("failed to open meminfo\n");     
  43.     char szTest[1000] = {0};    
  44.     while(!feof(fp))    
  45.     {    
  46.         memset(szTest, 0, sizeof(szTest));    
  47.         fgets(szTest, sizeof(szTest) - 1, fp);   
  48.         printf("%s", szTest);    
  49.     }    
  50.     fclose(fp);   
  51. }  
  52.   
  53. int main(int argc, char **argv)  
  54. {  
  55.     printf("===os information===\n");  
  56.     getOsInfo();  
  57.   
  58.     printf("===cpu infomation===\n");  
  59.     getCpuInfo();  
  60.   
  61.     printf("===memory information===\n");  
  62.     getMemoryInfo();  
  63.   
  64.     return 0;  
  65. }  

 

 

结果:

 

[plain] view plain copy
 
 print?
  1. ===os information===  
  2. Linux version 4.8.6-300.fc25.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 6.2.1 20160916 (Red Hat 6.2.1-2) (GCC) ) #1 SMP Tue Nov 1 12:36:38 UTC 2016  
  3. ===cpu infomation===  
  4. processor   : 0  
  5. vendor_id   : GenuineIntel  
  6. cpu family  : 6  
  7. model       : 58  
  8. model name  : Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz  
  9. stepping    : 9  
  10. microcode   : 0x1b  
  11. cpu MHz     : 2599.755  
  12. cache size  : 3072 KB  
  13. ===memory information===  
  14. MemTotal:        3061552 kB  
  15. MemFree:           89584 kB  
  16. MemAvailable:     596876 kB  
  17. Buffers:           70372 kB  
  18. Cached:           707548 kB  
  19. SwapCached:          392 kB  

 

 

PS:硬盘和网络参数也可以通过这种方法拿到,显卡参数可以用制造商提供的驱动API来取得