C++获取序列号方式
-
使用
_popen
来调用cmd
内部命令来获取 -
代码
#include <bits/stdc++.h> using namespace std; string exe_cmd(const char *cmd) { char buffer[128] = { 0 }; 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; } int main(int argc, char *argv[]) { string s = exe_cmd("wmic path win32_physicalmedia get SerialNumber"); //或者: wmic diskdrive get SerialNumbe cout << "硬盘序列号: \n" << s << endl; s = exe_cmd("wmic cpu get ProcessorID"); cout << "CPU序列号:\n" << s << endl; s = exe_cmd("wmic BaseBoard get serialnumber"); cout << "主板序列号:\n" << s << endl; s = exe_cmd("wmic bios get serialnumber"); cout << "产品序列号:\n" << s << endl; return 0; }
-
执行结果
-
核心指令(可以直接在
cmd
里面执行)//硬盘序列号: wmic diskdrive get SerialNumbe //CPU序列号: wmic cpu get ProcessorID //主板序列号: wmic BaseBoard get serialnumber //产品序列号: wmic bios get serialnumber
Python获取序列号方式
-
代码
import wmi c = wmi.WMI() #获取主板序列号 print(c.Win32_BaseBoard()[0].SerialNumber.strip()) #获取CPU序列号 速度有点慢 print(c.Win32_Processor()[0].ProcessorId.strip()) #获取硬盘序列号 for physical_disk in c.Win32_DiskDrive(): print(physical_disk.SerialNumber) input()