Qt 在Windows下设置Wifi连接和有线连接

Wifi连接-自动获取IP

实现程序如下:

// 设置网络为DHCP
QProcess cmd;
cmd.start(QString("netsh interface ip set address \"%1\" dhcp").arg("WLAN"));
cmd.waitForStarted();
cmd.waitForFinished();
QString strCmdOut = QString::fromLocal8Bit(cmd.readAllStandardOutput());
// 有时候执行命令后等待时间太短,已连接wifi还没来得及可以正常使用,则会返回"\r\n"
// 但实际已经成功启用DHCP。(必须先连接wifi,才能正常启用DHCP)
if (strCmdOut.contains("已在此接口上启用 DHCP。") || strCmdOut == "\r\n")
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("自动获取IP成功", SuccessIcon);
    pPromptBox->exec();
}
else
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("自动获取IP失败", FailIcon);
    pPromptBox->exec();
}

Wifi连接-手动获取IP

实现程序如下:

// 1. 设置为静态IP
QString strCommand("netsh interface ip set address");

// 2. 执行CMD命令
strCommand += QString("\"%1\" ").arg("WLAN");
strCommand += QString("static %1 ").arg(m_pEditIP->text());
strCommand += QString("%1 ").arg(m_pEditMask->text());
strCommand += QString("%1").arg(m_pEditGway->text());
QProcess cmd;
cmd.start(strCommand);
cmd.waitForStarted();
cmd.waitForFinished();
// cmd.readAllStandardError()这些信息只能输出一次,所以要赋值给临时QString
// 有线连接和wifi网卡的IP不能设置为一样的,否则会提示:"对象已存在"
QString strCmdOut = QString::fromLocal8Bit(cmd.readAllStandardOutput());
QString strError = QString::fromLocal8Bit(cmd.readAllStandardError());
qDebug() << "设置为静态IP_strCmdOut: " << strCmdOut;
qDebug() << "设置为静态IP_error: " << strError;
// 判断cmd是否执行成功
if (strCmdOut.contains("address"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("无效IP参数", PromptIcon);
    pPromptBox->exec();
}
else if (strCmdOut.contains("mask"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("无效mask参数", PromptIcon);
    pPromptBox->exec();
}
else if (strCmdOut.contains("gateway"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("无效gateway参数", PromptIcon);
    pPromptBox->exec();
}
else if (strCmdOut == "\r\n" && strError == "")
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("手动设置成功", SuccessIcon);
    pPromptBox->exec();

    this->accept();
    emit NetConfigSuc(); // 发射网络配置成功的信号
}
else if (strCmdOut.contains("对象已存在。"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("网络配置未改动", SuccessIcon);
    pPromptBox->exec();

    this->accept();
    emit NetConfigSuc(); // 发射网络配置成功的信号
}
else
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("手动设置失败", FailIcon);
    pPromptBox->exec();
}

有线连接-自动获取IP

实现程序如下:

// 设置网络为DHCP
QProcess cmd;
cmd.start(QString("netsh interface ip set address \"%1\" dhcp").arg("以太网"));
cmd.waitForStarted();
cmd.waitForFinished();
QString strCmdOut = QString::fromLocal8Bit(cmd.readAllStandardOutput());
// 有时候执行命令后等待时间太短,已连接wifi还没来得及可以正常使用,则会返回"\r\n"
// 但实际已经成功启用DHCP。(必须先连接wifi,才能正常启用DHCP)
if (strCmdOut.contains("已在此接口上启用 DHCP。") || strCmdOut == "\r\n")
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("自动获取IP成功", SuccessIcon);
    pPromptBox->exec();
}
else
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("自动获取IP失败", FailIcon);
    pPromptBox->exec();
}

有线连接-手动获取IP

实现程序如下:

// 1. 设置为静态IP
QString strCommand("netsh interface ip set address");

// 2. 执行CMD命令
strCommand += QString("\"%1\" ").arg("以太网");
strCommand += QString("static %1 ").arg(m_pEditIP->text());
strCommand += QString("%1 ").arg(m_pEditMask->text());
strCommand += QString("%1").arg(m_pEditGway->text());
QProcess cmd;
cmd.start(strCommand);
cmd.waitForStarted();
cmd.waitForFinished();
// cmd.readAllStandardError()这些信息只能输出一次,所以要赋值给临时QString
// 有线连接和wifi网卡的IP不能设置为一样的,否则会提示:"对象已存在"
QString strCmdOut = QString::fromLocal8Bit(cmd.readAllStandardOutput());
QString strError = QString::fromLocal8Bit(cmd.readAllStandardError());
qDebug() << "设置为静态IP_strCmdOut: " << strCmdOut;
qDebug() << "设置为静态IP_error: " << strError;
// 判断cmd是否执行成功
if (strCmdOut.contains("address"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("无效IP参数", PromptIcon);
    pPromptBox->exec();
}
else if (strCmdOut.contains("mask"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("无效mask参数", PromptIcon);
    pPromptBox->exec();
}
else if (strCmdOut.contains("gateway"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("无效gateway参数", PromptIcon);
    pPromptBox->exec();
}
else if (strCmdOut == "\r\n" && strError == "")
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("手动设置成功", SuccessIcon);
    pPromptBox->exec();

    this->accept();
    emit NetConfigSuc(); // 发射网络配置成功的信号
}
else if (strCmdOut.contains("对象已存在。"))
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("网络配置未改动", SuccessIcon);
    pPromptBox->exec();

    this->accept();
    emit NetConfigSuc(); // 发射网络配置成功的信号
}
else
{
    MyPromptBox *pPromptBox = new MyPromptBox;
    pPromptBox->setLabInfo("手动设置失败", FailIcon);
    pPromptBox->exec();
}

完整工程的GitHub链接

https://github.com/confidentFeng/VSAppProject/tree/NetControl


posted @ 2020-05-11 17:57  fengMisaka  阅读(3120)  评论(0编辑  收藏  举报