1、Windows下可在CMD控制终端使用Dos命令对指定网卡进行IP配置,需要使用管理员权限打开CMD终端

往网卡添加IP主要有以下两种方法:

方法1:

netsh interface ip add address “网卡名称” IP 掩码 网关

方法2:

netsh interface ip set address \"网卡名称\" static IP 掩码 网关 

其中方法1是往指定网卡添加相应的IP地址,并不影响网卡原来的IP配置,方法2是将网卡重置后再添加IP。

删除网卡IP:

netsh interface ip delete address “网卡名称” IP 掩码 网关

2、Qt中的实现原理

头文件:

#include <QProcess>

.cpp文件:

    QProcess *process = new QProcess();
    QString cmd = "netsh interface ip set address ";
    cmd += "\"以太网\" ";
    cmd += "static ";
    cmd += "173.75.40.20";
    cmd += " 255.255.0.0 ";/*开启CMD进程,输入DOS指令*/
    process->start(cmd);
    /*等待CMD进程结束*/
process->waitForFinished();
QString cmd_add
= "netsh interface ip add address "; cmd_add += "\"以太网\" "; cmd_add += "173.75.40.12"; cmd_add += " 255.255.0.0 "; process->start(cmd_add); /*等待CMD进程结束*/
process->waitForFinished();
    QString cmd_delete = "netsh interface ip delete address ";
    cmd_delete += "\"以太网\" ";
    cmd_delete += "173.75.40.12";
    cmd_delete += " 255.255.0.0 ";
    process->start(cmd_delete);
    /*等待CMD进程结束*/
process->waitForFinished();
    delete process;
process = NULL;
02-20 14:41