vc 获取计算机网络参数,使用VC++执行Windows命令读取计算机硬件信息

这段代码展示了如何使用C++通过执行系统命令获取计算机关键配置信息,如系统安装日期、设备序列号、MAC地址、硬盘序列号等,并将其整理到CSV文件中。函数`execmd`负责执行命令,`SplitString`和`string_replace`用于处理字符串。
摘要由CSDN通过智能技术生成

#include "pch.h"

#include

#include

#include

#include

#include

#include

using namespace std;

// 描述:execmd函数执行命令,并将结果存储到result字符串数组中

// 参数:cmd表示要执行的命令

// result是执行的结果存储的字符串数组

// 函数执行成功返回1,失败返回0

#pragma warning(disable:4996)

int execmd(char* cmd, char* result) {

char buffer[128]; //定义缓冲区

FILE* pipe = _popen(cmd, "r"); //打开管道,并执行命令

if (!pipe)

return 0; //返回0表示运行失败

while (!feof(pipe)) {

if (fgets(buffer, 128, pipe)) { //将管道输出到result中

strcat(result, buffer);

}

}

_pclose(pipe); //关闭管道

return 1; //返回1表示运行成功

}

//字符分割

void SplitString(const string& s, vector& v, const string& c)

{

string::size_type pos1, pos2;

pos2 = s.find(c);

pos1 = 0;

while (string::npos != pos2)

{

v.push_back(s.substr(pos1, pos2 - pos1));

pos1 = pos2 + c.size();

pos2 = s.find(c, pos1);

}

if (pos1 != s.length())

v.push_back(s.substr(pos1));

}

//字符串替换

void string_replace(string&s1, const string&s2, const string&s3)

{

string::size_type pos = 0;

string::size_type a = s2.size();

string::size_type b = s3.size();

while ((pos = s1.find(s2, pos)) != string::npos)

{

s1.replace(pos, a, s3);

pos += b;

}

}

//正则表达式字符串分割函数

std::vector<:string> split(std::string str, std::string pattern)

{

std::string::size_type pos;

std::vector<:string> result;

str += pattern;//扩展字符串以方便操作

int size = str.size();

for (int i = 0; i < size; i++)

{

pos = str.find(pattern, i);

if (pos < size)

{

std::string s = str.substr(i, pos - i);

result.push_back(s);

i = pos + pattern.size() - 1;

}

}

return result;

}

//去掉多余的空格

string newtrim(string &s)

{

if (s.empty())

{

return s;

}

s.erase(0, s.find_first_not_of(" "));

s.erase(s.find_last_not_of(" ") + 1);

return s;

}

//去掉多余的空行

string newtrimline(string &s)

{

if (s.empty())

{

return s;

}

s.erase(0, s.find_first_not_of("\r\n"));

s.erase(s.find_last_not_of("\r\n") + 1);

return s;

}

int main()

{

char SystemInstallDate[] = "c:\\windows\\system32\\systeminfo|findstr 初始安装日期";

char PCserialnumber[] = "wmic bios get serialnumber";

char MACAddress[] = "ipconfig /all|findstr 物理地址";

char IPAddress[] = "ipconfig /all|findstr IPv4";

char MACIPAddress[] = "wmic nicconfig get IPAddress,MACAddress";

char HDserial[] = "wmic diskdrive get Caption,SerialNumber";

char SystemInstallDateResult[1024] = "";

char PCserialnumberResult[1024] = "";

char MACAddressResult[1024] = "";

char IPAddressResult[1024] = "";

char HDserialResult[1024] = "";

char MACIPAddressResult[1024] = "";

ofstream fout("计算机配置信息.csv"); //创建一个data.txt的文件

cout << endl;

cout << endl;

cout << endl;

cout << endl;

if (1 == execmd(SystemInstallDate, SystemInstallDateResult)) {

string installdate = SystemInstallDateResult;

//cout << installdate << endl;

cout << " 正在获取系统安装日期 ... ..." << endl;

string trimInstallDate = newtrimline(installdate);

string str = trimInstallDate.substr(14);

std::vector<:string> InstallDateResultVector = split(str, ",");

string finalInstallDateValue = newtrim(InstallDateResultVector[0]);

string installDateName = "系统安装日期:";

//cout <

fout << installDateName << "," << finalInstallDateValue << endl; //将变量的值写入文件

}

if (1 == execmd(PCserialnumber, PCserialnumberResult)) {

string pcserial = PCserialnumberResult;

//cout << pcserial << endl;

string PCSerialString = newtrim(pcserial);

vector PCSerialVector = split(PCSerialString, "\r\n");

string PCSerialValue = newtrim(PCSerialVector[1]);

string PCSerialKey = "设备序列号:";

//cout <

fout << PCSerialKey << "," << PCSerialValue << endl; //将变量的值写入文件

cout << " 正在获取设备序列号 ... ..." << endl;

}

if (1 == execmd(MACIPAddress, MACIPAddressResult)) {

string macipaddr = MACIPAddressResult;

vector MACIPString = split(macipaddr, "\r\n");

for (int i = 0; i < MACIPString.size();i++) {

string::size_type idx;

idx = MACIPString[i].find("}");

if (idx == string::npos) {

}

//cout << "null" << endl;

else {

//cout << MACIPString[i]<

const regex pattern("\\s{2,}");

const regex maohao(":");

vector MACandIPVector;

string temp = regex_replace(MACIPString[i], pattern, "-");

SplitString(temp, MACandIPVector, "-");

string ipString = MACandIPVector[0];

string macString = MACandIPVector[1];

string MACValue = regex_replace(macString, maohao, "-");

int beginIP = ipString.find_first_of(",");

string IPValue = ipString.substr(2,beginIP-3);

//cout << IPValue << "|" << MACValue << "|"<

fout << "MAC地址:"+MACValue << "," << "IP地址:"+IPValue << endl; //将变量的值写入文件

}

}

cout << " 正在获取网卡MAC地址和IP地址 ... ..." << endl;

}

if (1 == execmd(HDserial, HDserialResult)) {

string hdserial = HDserialResult;

string hdvar, hdvar1, hdvar2;

vector HDVector;

SplitString(newtrimline (hdserial), HDVector, "\r\n");

//cout << hdserial<

for (int k = 1; k < HDVector.size();k++) {

vector HDKeyValue;

string tempHDSerialstring = newtrim(HDVector[k]);

int beginHDSerial = tempHDSerialstring.find_last_of(" ");

string HDserialValue = tempHDSerialstring.substr(beginHDSerial+1, tempHDSerialstring.size());

string HDserialKey = tempHDSerialstring.substr(0, beginHDSerial);

char str[2];

itoa(k,str,10);

//cout << "|" << HDserialKey <

fout << "硬盘"<< str<< "("<

//fout << "硬盘"<< str<

}

cout << " 正在获取硬盘序列号 ... ..." << endl;

}

fout.close(); //关闭文件

cout << " 完成。 " << endl;

cout << endl;

cout << endl;

cout << endl;

cout << endl;

cout << endl;

cout << " ==========================================================" << endl;

cout << " = =" << endl;

cout << " = 程序执行完毕 !!! =" << endl;

cout << " = =" << endl;

cout << " = 文件 计算机配置信息.csv 已输出到程序所在目录。 =" << endl;

cout << " = =" << endl;

cout << " ==========================================================" << endl;

cout << endl;

cout << endl;

cout << endl;

cout << endl;

cout << endl;

cout << endl;

system("start 计算机配置信息.csv");

system("pause"); //暂停以查看结果

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值