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
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/240442
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!