#include <stdio.h>
#include <string>
#include <iostream>
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;
getchar();
return 0;
}
10-03