- 1#include "stdafx.h"2#include 3#include 4#include 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 地址的修改。
来源: http://www.cnblogs.com/morixiaofeng/p/5867906.html