C++调用CMD修改IP地址



1
#include "stdafx.h" 2 #include <windows.h> 3 #include <shellapi.h> 4 #include <iostream> 5 using namespace std; 6 7 int _tmain(int argc, _TCHAR* argv[]) 8 { 9 char strIP[50]; //修改后的IP 10 cout<<"Enter your IP:"; 11 cin>>strIP; 12 13 //合成命令 14 char temp[500]; 15 sprintf(temp,"/c netsh interface ip set address name=\"本地连接\" source=static addr=%s mask=255.255.255.0 gateway=192.168.0.1 1",strIP); 16 17 HINSTANCE hresult = ShellExecuteA(NULL, "open", "cmd.exe", temp, NULL, SW_HIDE); //启动cmd 18 19 int nRes = (int)hresult; 20 if(nRes>32) 21 printf("调用成功"); 22 else 23 printf("调用失败,error:%d",nRes); 24 Sleep(1000); 25 return 0; 26 }

需要注意的是,在用cmd的netsh指令时,前面要加"/c"。
name=\"本地连接\" 即表示修改本机IP;

source=static 表示修改的的是静态IP;

addr=%s 表示要修改的IP地址;

mask=255.255.255.0 表示子网掩码;

gateway=192.168.0.1 表示默认网关;

在默认网关后的“1”,表示确定;

调用ShellExecuteA()函数来启动cmd程序,完成IP地址的修改。

转载于:https://www.cnblogs.com/morixiaofeng/p/5867906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值