首页 > 代码库 > Qt修改mac地址

Qt修改mac地址

1.通过修改注册表NetworkAddress实现,如果注册表没有此项,找到DriverDesc描述信息是网卡信息,如Intel(R) Ethernet Connection I218-LM,在此新建字符串NetworkAddress,其值就是mac地址。

 mac地址修改后,要重启网卡。 网卡禁用启用,运行cmd命令netsh interface set interface 本地连接 enabled 和netsh interface set interface 本地连接 disabled。

 

 

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QProcess>#include <QSettings>#include <QString>#include <QDebug>#include <QFile>#include <QMessageBox>#include <QDir>MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);}MainWindow::~MainWindow(){    delete ui;}//const QString strReg="HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\0007";void MainWindow::on_btnRecover_clicked(){  ui->btnRecover->setEnabled(false);  if(!loadIni())  {      ui->btnRecover->setEnabled(true);      return;  }  setButtonStatus(ui->btnModify,false);   QSettings *reg=new QSettings(mReg,QSettings::NativeFormat);    reg->setValue("NetworkAddress",mOriginal);  delete reg;  reg=NULL;   reStartNetWork();  setButtonStatus(ui->btnRecover,true);  ui->btnRecover->setEnabled(true);}void MainWindow::on_btnModify_clicked(){    ui->btnModify->setEnabled(false);    if(!loadIni())    {        ui->btnModify->setEnabled(true);        return;    }    setButtonStatus(ui->btnRecover,false);    QSettings *reg=new QSettings(mReg,QSettings::NativeFormat);        reg->setValue("NetworkAddress",mModify);    delete reg;    reg=NULL;    reStartNetWork();    setButtonStatus(ui->btnModify,true);    ui->btnModify->setEnabled(true);}void MainWindow::reStartNetWork(){   setNetEnabled(false);   setNetEnabled(true);}void MainWindow::setNetEnabled(bool isEnabled){    QStringList cmdlst;    if(isEnabled)    {      cmdlst<<"/c";      cmdlst<<"netsh interface set interface 本地连接 enabled";    }    else    {      cmdlst<<"/c";      cmdlst<<"netsh interface set interface 本地连接 disabled";    }    runCMD(cmdlst);}void MainWindow::testCmd(){    QProcess p(0);    p.start("cmd", QStringList()<<"/c"<<"ping www.baidu.com");    p.waitForStarted();    p.waitForFinished();    QString strTemp=QString::fromLocal8Bit(p.readAllStandardOutput());    QMessageBox testMassage;    testMassage.setText(strTemp);    testMassage.exec();}void MainWindow::runCMD(const QStringList &cmdlst){    QProcess p(0);    p.start("cmd",cmdlst);    qDebug()<<cmdlst;    p.waitForStarted();    p.waitForFinished();    qDebug()<<QString::fromLocal8Bit(p.readAllStandardOutput());}void MainWindow::setButtonStatus(QPushButton *btn,bool isClick){    if(isClick)    {        btn->setStyleSheet("QPushButton{background-color: rgb(220, 241, 252);"                           "border-color: rgb(170, 255, 255);"                           "border-radius: 10px;"                           "border: 1px solid;"                           "}");    }    else    {       btn->setStyleSheet("QPushButton{background-color: rgb(240, 240, 240);"                          "border-color: rgb(170, 255, 255);"                          "border-radius: 10px;"                          "border: 1px solid;"                          "}");    }}bool MainWindow::loadIni(){    if(!QFile::exists(qApp->applicationDirPath()+"\\config.ini"))    {        QMessageBox box;        box.setText("config.ini don‘t exist!");        box.setStandardButtons(QMessageBox::Ok);        box.exec();        return false;    }    QSettings *setIni = new QSettings(qApp->applicationDirPath()+"\\config.ini", QSettings::IniFormat);    mReg=setIni->value("Regedit/reg").toString();    mOriginal=setIni->value("MacAddress/original").toString();    mModify=setIni->value("MacAddress/modify").toString();    return true;}

 

[Regedit]#找到DriverDesc值是网卡信息,如Intel(R) Ethernet Connection I218-LM,就是添加此网卡mac地址NetworkAddress的地方#reg=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0007[MacAddress]original=ECF4BB4D144Fmodify=B8AC6F3F148D#modify=#modify=#modify=

 

Qt修改mac地址