C/C++獲取作業系統、CPU、記憶體資訊、硬碟、IP和MAC(windows和linux)
有時候需要在工程裡面獲取一些系統或者硬體的資訊,比如系統版本,cpu,記憶體,顯示卡,硬碟,網路等,作為後續軟體功能判斷的依據,甚至參與效能演算法自適應建模
Windows
作業系統和記憶體資訊在windows下通過系統的API來獲取,CPU資訊則需要需要通過底層CPUID指令取得
程式碼:
#include <iostream> #include <string> #include <string.h> #include <winsock2.h> // include must before window.h #include <iphlpapi.h> #include <windows.h> #pragma comment(lib, "iphlpapi.lib") #pragma warning(disable: 4996) // avoid GetVersionEx to be warned // ***** global macros ***** // static const int kMaxInfoBuffer = 256; #define GBYTES 1073741824 #define MBYTES 1048576 #define KBYTES 1024 #define DKBYTES 1024.0 // ---- get os info ---- // void getOsInfo() { // get os name according to version number OSVERSIONINFO osver = { sizeof(OSVERSIONINFO) }; GetVersionEx(&osver); std::string os_name; if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0) os_name = "Windows 2000"; else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1) os_name = "Windows XP"; else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0) os_name = "Windows 2003"; else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2) os_name = "windows vista"; else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1) os_name = "windows 7"; else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2) os_name = "windows 10"; std::cout << "os name: " << os_name << std::endl; std::cout << "os version: " << osver.dwMajorVersion << '.' << osver.dwMinorVersion << std::endl; } // ---- get cpu info ---- // #ifdef _WIN64 // method 2: usde winapi, works for x86 and x64 #include <intrin.h> void getCpuInfo() { int cpuInfo[4] = {-1}; char cpu_manufacture[32] = { 0 }; char cpu_type[32] = { 0 }; char cpu_freq[32] = { 0 }; __cpuid(cpuInfo, 0x80000002); memcpy(cpu_manufacture, cpuInfo, sizeof(cpuInfo)); __cpuid(cpuInfo, 0x80000003); memcpy(cpu_type, cpuInfo, sizeof(cpuInfo)); __cpuid(cpuInfo, 0x80000004); memcpy(cpu_freq, cpuInfo, sizeof(cpuInfo)); std::cout << "CPU manufacture: " << cpu_manufacture << std::endl; std::cout << "CPU type: " << cpu_type << std::endl; std::cout << "CPU main frequency: " << cpu_freq << std::endl; } #else // mothed 1: this kind asm embedded in code only works in x86 build // save 4 register variables DWORD deax; DWORD debx; DWORD decx; DWORD dedx; // init cpu in assembly language void initCpu(DWORD veax) { __asm { mov eax, veax cpuid mov deax, eax mov debx, ebx mov decx, ecx mov dedx, edx } } long getCpuFreq() { int start, over; _asm { RDTSC mov start, eax } Sleep(50); _asm { RDTSC mov over, eax } return (over - start) / 50000; } std::string getManufactureID() { char manuID[25]; memset(manuID, 0, sizeof(manuID)); initCpu(0); memcpy(manuID + 0, &debx, 4); // copy to array memcpy(manuID + 4, &dedx, 4); memcpy(manuID + 8, &decx, 4); return manuID; } std::string getCpuType() { const DWORD id = 0x80000002; // start 0x80000002 end to 0x80000004 char cpuType[49]; memset(cpuType, 0, sizeof(cpuType)); for (DWORD t = 0; t < 3; t++) { initCpu(id + t); memcpy(cpuType + 16 * t + 0, &deax, 4); memcpy(cpuType + 16 * t + 4, &debx, 4); memcpy(cpuType + 16 * t + 8, &decx, 4); memcpy(cpuType + 16 * t + 12, &dedx, 4); } return cpuType; } void getCpuInfo() { std::cout << "CPU manufacture: " << getManufactureID() << std::endl; std::cout << "CPU type: " << getCpuType() << std::endl; std::cout << "CPU main frequency: " << getCpuFreq() << "MHz" << std::endl; } #endif // ---- get memory info ---- // void getMemoryInfo() { std::string memory_info; MEMORYSTATUSEX statusex; statusex.dwLength = sizeof(statusex); if (GlobalMemoryStatusEx(&statusex)) { unsigned long long total = 0, remain_total = 0, avl = 0, remain_avl = 0; double decimal_total = 0, decimal_avl = 0; remain_total = statusex.ullTotalPhys % GBYTES; total = statusex.ullTotalPhys / GBYTES; avl = statusex.ullAvailPhys / GBYTES; remain_avl = statusex.ullAvailPhys % GBYTES; if (remain_total > 0) decimal_total = (remain_total / MBYTES) / DKBYTES; if (remain_avl > 0) decimal_avl = (remain_avl / MBYTES) / DKBYTES; decimal_total += (double)total; decimal_avl += (double)avl; char buffer[kMaxInfoBuffer]; sprintf_s(buffer, kMaxInfoBuffer, "total %.2f GB (%.2f GB available)", decimal_total, decimal_avl); memory_info.append(buffer); } std::cout << memory_info << std::endl; } // ---- get harddisk info ---- // std::string execCmd(const char *cmd) { char buffer[128] = { 0 }; std::string result; FILE *pipe = _popen(cmd, "r"); if (!pipe) throw std::runtime_error("_popen() failed!"); while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) result += buffer; } _pclose(pipe); return result; } void getHardDiskInfo() { std::string hd_seiral = execCmd("wmic path win32_physicalmedia get SerialNumber"); std::cout << "HardDisk Serial Number: " << hd_seiral << std::endl; } // ---- get network info ---- // void getNetworkInfo() { // PIP_ADAPTER_INFO struct contains network information PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO(); unsigned long adapter_size = sizeof(IP_ADAPTER_INFO); int ret = GetAdaptersInfo(pIpAdapterInfo, &adapter_size); if (ret == ERROR_BUFFER_OVERFLOW) { // overflow, use the output size to recreate the handler delete pIpAdapterInfo; pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[adapter_size]; ret = GetAdaptersInfo(pIpAdapterInfo, &adapter_size); } if (ret == ERROR_SUCCESS) { int card_index = 0; // may have many cards, it saved in linklist while (pIpAdapterInfo) { std::cout << "---- " << "NetworkCard " << ++card_index << " ----" << std::endl; std::cout << "Network Card Name: " << pIpAdapterInfo->AdapterName << std::endl; std::cout << "Network Card Description: " << pIpAdapterInfo->Description << std::endl; // get IP, one card may have many IPs PIP_ADDR_STRING pIpAddr = &(pIpAdapterInfo->IpAddressList); while (pIpAddr) { char local_ip[128] = { 0 }; strcpy(local_ip, pIpAddr->IpAddress.String); std::cout << "Local IP: " << local_ip << std::endl; pIpAddr = pIpAddr->Next; } char local_mac[128] = { 0 }; int char_index = 0; for (int i = 0; i < pIpAdapterInfo->AddressLength; i++) { char temp_str[10] = { 0 }; sprintf(temp_str, "%02X-", pIpAdapterInfo->Address[i]); // X for uppercase, x for lowercase strcpy(local_mac + char_index, temp_str); char_index += 3; } local_mac[17] = '\0'; // remove tail '-' std::cout << "Local Mac: " << local_mac << std::endl; // here just need the first card info break; // iterate next //pIpAdapterInfo = pIpAdapterInfo->Next; } } if (pIpAdapterInfo) delete pIpAdapterInfo; } int main(int argc, char *argv[]) { std::cout << "=== os information ===" << std::endl; getOsInfo(); std::cout << "=== cpu infomation ===" << std::endl; getCpuInfo(); std::cout << "=== memory information ===" << std::endl; getMemoryInfo(); std::cout << "=== harddisk information ===" << std::endl; getHardDiskInfo(); std::cout << "=== network information ===" << std::endl; getNetworkInfo(); system("pause"); return 0; }
結果:
===os information=== os name: windows 10 os version: 6.2 ===cpu infomation=== CPU main frequency: 2612MHz CPU manufacture: GenuineIntel CPU type: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz ===memory information=== total 7.86 GB (1.38 GB available) === harddisk information === HardDisk Serial Number: SerialNumber 0520B6776730A1F4 === network information === ---- NetworkCard 1 ---- Network Card Name: {403C08C9-5600-4BE9-8021-AAAADB63D678} Network Card Description: Intel(R) Ethernet Connection (5) I219-LM Local IP: 10.12.0.142 Local Mac: 50-9A-4C-14-B5-7F
Linux
linux下很多資訊都是存放著系統的/proc目錄下,因此讀檔案就可以獲取到了
程式碼:
#include <stdio.h> #include <stdlib.h> #include <string.h> // mostly need to read the linux config files to get system info // ---- get os info ---- // void getOsInfo() { FILE *fp = fopen("/proc/version", "r"); if(NULL == fp) printf("failed to open version\n"); char szTest[1000] = {0}; while(!feof(fp)) { memset(szTest, 0, sizeof(szTest)); fgets(szTest, sizeof(szTest) - 1, fp); // leave out \n printf("%s", szTest); } fclose(fp); } // ---- get cpu info ---- // void getCpuInfo() { FILE *fp = fopen("/proc/cpuinfo", "r"); if(NULL == fp) printf("failed to open cpuinfo\n"); char szTest[1000] = {0}; // read file line by line while(!feof(fp)) { memset(szTest, 0, sizeof(szTest)); fgets(szTest, sizeof(szTest) - 1, fp); // leave out \n printf("%s", szTest); } fclose(fp); } // ---- get memory info ---- // void getMemoryInfo() { FILE *fp = fopen("/proc/meminfo", "r"); if(NULL == fp) printf("failed to open meminfo\n"); char szTest[1000] = {0}; while(!feof(fp)) { memset(szTest, 0, sizeof(szTest)); fgets(szTest, sizeof(szTest) - 1, fp); printf("%s", szTest); } fclose(fp); } // ---- get harddisk info ---- // #include <fcntl.h> #include <sys/ioctl.h> #include <linux/hdreg.h> void getHardDiskInfo() { // use cmd, this is the only way static struct hd_driveid hd; int fd; if ((fd = open("/dev/sda", O_RDONLY | O_NONBLOCK)) < 0) { printf("ERROR opening /dev/sda\n"); return; } if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) { printf("model ", hd.model); printf("HardDisk Serial Number: %.20s\n", hd.serial_no); } else printf("no available harddisk info"); } // ---- get network info ---- // #include <ifaddrs.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <linux/if.h> void getNetworkInfo() { // get ip, works for linux and mac os, best method struct ifaddrs *ifAddrStruct = NULL; struct ifaddrs *ifa = NULL; getifaddrs(&ifAddrStruct); for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { if (!ifa->ifa_addr) continue; // check if IP4 if (ifa->ifa_addr->sa_family == AF_INET) { void *tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; char local_ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, tmpAddrPtr, local_ip, INET_ADDRSTRLEN); // actually only need external ip printf("%s IP: %s\n", ifa->ifa_name, local_ip); } } if (ifAddrStruct) freeifaddrs(ifAddrStruct); // get mac, need to create socket first, may not work for mac os struct ifreq ifr; int fd = socket(AF_INET, SOCK_DGRAM, 0); char local_mac[128] = { 0 }; strcpy(ifr.ifr_name, "eth0"); // only need ethernet card if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr)) { char temp_str[10] = { 0 }; memcpy(temp_str, ifr.ifr_hwaddr.sa_data, 6); sprintf(local_mac, "%02x-%02x-%02x-%02x-%02x-%02x", temp_str[0]&0xff, temp_str[1]&0xff, temp_str[2]&0xff, temp_str[3]&0xff, temp_str[4]&0xff, temp_str[5]&0xff); } printf("Local Mac: %s\n", local_mac); } int main(int argc, char **argv) { printf("=== os information ===\n"); getOsInfo(); printf("=== cpu infomation ===\n"); getCpuInfo(); printf("=== memory information ===\n"); getMemoryInfo(); printf("=== harddisk information ===\n"); getHardDiskInfo(); printf("=== network information ===\n"); getNetworkInfo(); return 0; }
結果:
===os information===
Linux version 4.8.6-300.fc25.x86_64 ([email protected]) (gcc version 6.2.1 20160916 (Red Hat 6.2.1-2) (GCC) ) #1 SMP Tue Nov 1 12:36:38 UTC 2016
===cpu infomation===
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 58
model name : Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
stepping : 9
microcode : 0x1b
cpu MHz : 2599.755
cache size : 3072 KB
===memory information===
MemTotal: 3061552 kB
MemFree: 89584 kB
MemAvailable: 596876 kB
Buffers: 70372 kB
Cached: 707548 kB
SwapCached: 392 kB
=== harddisk information ===
HardDisk Serial Number: 0520B6776730A1F4
=== network information ===
lo IP: 127.0.0.1
eth0 IP: 192.168.136.140
Local Mac: 00-0c-29-ec-7a-80
PS:顯示卡引數可以用製造商提供的驅動API來取得
相關推薦
C/C++獲取作業系統、CPU、記憶體資訊、硬碟、IP和MAC(windows和linux)
有時候需要在工程裡面獲取一些系統或者硬體的資訊,比如系統版本,cpu,記憶體,顯示卡,硬碟,網路等,作為後續軟體功能判斷的依據,甚至參與效能演算法自適應建模 Windows 作業系統和記憶體資訊在windows下通過系統的API來獲取,CPU資訊則需要需要通過底層CPUI
Qt:獲取系統CPU使用率(Windows+macOS+Linux)
最近專案上有需求,需要根據CPU使用率來調整排程邏輯,因此寫了這個庫,沒有多少程式碼,直接開源好了 以下是README內容,直接搬過來: 介紹 JQCPUMonitor,是一個給Qt開發的,CPU使用率檢測庫,目前只檢測CPU整體使用率。 支援3個主要平臺,Windows、
資料庫概述、mysql-5.7.11-winx64.zip 的下載、安裝、配置和使用(windows裡安裝)圖文詳解
本博文的主要內容有 .資料庫的概述 .mysql-5.7.11-winx64.zip 的下載 .mysql-5.7.11-winx64.zip 的安裝 .mysql-5.7.11-winx64.zip 的配置 .mysql-5.7.11-winx64.zip 的使用
C語言中二維陣列名與陣列地址、首行地址、首行首元素地址關係與區別詳解(初學者必須掌握)
C語言作為很多大學理工科都會學習的語言,作為一種程式設計入門語言。但是相對於其他高階程式語言來說相對是比較難,尤其是指針,不知道有多少莘莘學子都是因為它,從C語言入門到放棄。想當年,筆者在大一學習C語言
[Linux OS] Ubuntu 16.04 上實時顯示上下行網速、CPU及記憶體使用率--indicator-sysmonitor
在終端安裝外掛: sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor sudo apt-get update
Ubuntu 14.04 標題欄實時顯示上下行網速、CPU及記憶體狀態
用wget下載indicator-sysmonitor 終端執行wget -c https://launchpad.net/indicator-sysmonitor/trunk/4.0/+download/indicator-sysmonitor_0.4.3_al
C#怎麼獲取已知USB裝置驅動資訊(請看內容)
工具條問題如何才能使子類和父類的非預設建構函式和預設建構函式都執行一次工具條問題如何才能使子類和父類的非預設建構函式和預設建構函式都執行一次 請問如何獲取比例字型的寬度這個的主要意思是請問如何獲取比例字型的寬度這個的主要意思是 請問mschart如何能畫出來一條豎線WebBr
C++ 記憶體分配和回收 (new 和delete)
“new”是C++的一個關鍵字,同時也是操作符。對於new的內容總結。 1、new的過程 當我們使用關鍵字new在堆上動態建立一個物件時,它實際上做了三件事:獲得一塊記憶體空間、呼叫建構函式、返回正確的指標。當然,如果我們建立的是簡單型別的變數,那麼第二步會被省略。假如我們
Linux下檢視作業系統,cpu,記憶體,計算機名,硬碟大小
# uname -a # 檢視核心/作業系統/CPU資訊 # head -n 1 /etc/issue # 檢視作業系統版本 # cat /proc/cpuinfo # 檢視CPU資訊 # hostname # 檢視計算機名 # lspci
關於C語言中的Complex(復數類型)和imaginary(虛數類型)
http 個人 time 編譯 pop oat float environ real 關於C語言中的Complex(復數類型)和imaginary(虛數類型) 其實這裏的復數complex就是數學裏的復數,包含實部和虛部兩個部分,比如:x=2.1+6i,下面進行詳細介紹
C# 編寫Windows Service(windows服務程序)
init override services ges 計算 public control etime 令行 Windows Service簡介: 一個Windows服務程序是在Windows操作系統下能完成特定功能的可執行的應用程序。Windows服務程序雖然是可執行
MSVC2012終於“真正”支持C++11了(Windows XP Targeting)!
http target lan left info href userinfo iuc cow 剖o0辜4R趙64yo貉業http://www.docin.com/app/user/userinfo?userid=181333168 8柿7c06油萊4耪chttp://ww
二、三級等保建議安全設備及其主要依據(毫無保留版)
二三級等保二級等保序號建議功能或模塊建議方案或產品重要程度主要依據備註安全層面二級分項二級測評指標權重1邊界防火墻非常重要網絡安全訪問控制(G2)a)應在網絡邊界部署訪問控制設備,啟用訪問控制功能;1b)應能根據會話狀態信息為數據流提供明確的允許/拒絕訪問的能力,控制粒度為網段級。12入侵檢測系統 (
C之 #error 和 #line(二十一)
C語言 #error #line 我們今天來講下 C 語言中的兩個比較偏僻的知識點,之所以說偏僻是因為在平時的代碼中我們見得很少。首先來說下 #error,它是用於生成一個編譯錯誤消息。用法如下:#error message;註意 message 不需要用雙引號包圍。#error
C++ Primer Plus第六版編程練習---第3章 處理數據(未完待續)
foo ons enter ++ har HA CI return include 1. #include <iostream> const int CONVER_FACTOR = 12; int main(int argc, char* argv[]){ in
kettle的下載、安裝和初步使用(windows平臺下)(圖文詳解)
1.0 index java_home 網站 int 目錄 解決辦法 for sql kettle的下載 ?Kettle可以在http://kettle.pentaho.org/網站下載 http:
安裝JDK、Tomcat和Mysql(CentOS 7.4)
pen state 需要 密碼 bootstrap commands set register mysq 上傳軟件包 rz上傳jdk-8u181-linux-x64.tar.gz和apache-tomcat-8.5.32.tar.gz 或者FileZilla Client
java正則校驗必須包括字母、數字和字元(6-12位)
java正則校驗,必須包括字母數字字元 public static String checkPassword(String passwordStr) { if (passwordStr !=
javaSE (三十五)多執行緒 ( 多執行緒實現方法和區別、同步程式碼塊和方法(執行緒安全))
主要還是熟悉api,熟悉方法,簡單,需要多實踐 1、 多執行緒實現方法和區別: 多執行緒實現的兩種方法: 1)類繼承Thread類或實現Runnable介面,重寫run()方法 2)建立Thread的子類物件(需要開幾個執行緒就建立幾個物件,可建立匿名內部類) 3)子類
weblogic系列漏洞整理————8、WebLogic 兩處任意檔案上傳漏洞動態分析(CVE-2018-2894)
0x01 前言 CNCERT前幾天發公告稱發現Oracle公司出品的基於JavaEE結構的中介軟體WebLogic產品存在一個遠端上傳漏洞,並得到了廠商的確認,危害程度評分高達9.8分。鑑於廠商已進行了安全修復,筆者對該漏洞進行了一次分析。WebLogic管理端未授權的