WINDOWS怎么设置IP地址
WINDOWS如何设置IP地址?
最近一直为要设置网卡地址烦恼,网上找了好久资料都没有我真正想要的,下面是我从网上找的一些方法,但我要的是用程序像手动从网络连接属性里该的那种方式
在iphlpapi.dll中有个函数AddIpAddress
但看到人家说的下面这段话,很明显不是我需要的
“最常用的函数 AddIPAddress。这个函数能够设置本地网络的IP。但不幸的是,这个IP是临时的,当系统重新启动或者发生其它的PNP事件的时候这个IP就不存在了。”
另外有些方法如用WMI对象
strComputer = ". "
Set objWMIService = GetObject( "winmgmts:\\ " & strComputer & "\root\cimv2 ")
Set colNetAdapters = objWMIService.ExecQuery _
( "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE ")
strIPAddress = Array( "192.168.1.141 ")
strSubnetMask = Array( "255.255.255.0 ")
strGateway = Array( "192.168.1.100 ")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed. "
Else
WScript.Echo "The IP address could not be changed. "
End If
Next
对于WMI不是很懂,用起来也麻烦
还有直接改注册表的
HKEY_LOCAL_MACHINE\SYSTEM\Controlset001\Services\{b8584ff4-32a6-43DC-BC53-982A46165FAF}\parameters\Tcpip 里面的IPADDRESS项
可是这路径中的这串数字{b8584ff4-32a6-43DC-BC53-982A46165FAF}在不同的机器上似乎不同的,也不知道用没有什么规律
若要用一个程序设置IP就想手动设置IP一样的可靠应该怎么办呢。甚至有什么办法可以跟踪手动设置IP时WINDOWS是怎么操作的吗?总之有没有什么线索.万分感谢呀
------解决方案--------------------
设置IP地址就是通过写注册表实现的,需要重启系统,因为TCP/IP协议栈需要重启以应用新的IP参数。
------解决方案--------------------
网卡地址要用网卡供应商的驱动程序来改吧,如果随便能改,那这世界不乱套了
------解决方案--------------------
我曾经做过一个定时改IP的小工具,里面有获取网卡信息,更改IP的代码,不过代码在公司电脑里,你留下邮箱,我明天上午发给你可好?
------解决方案--------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
CString strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\ ";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,strKeyName.GetString(), 0, KEY_WRITE, &hKey) != ERROR_SUCCESS)
{
return FALSE;
}
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);
int nIP, nMask, nGate;
nIP = (int)strlen(mszIPAddress);
nMask = (int)strlen(mszNetMask);
nGate = (int)strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress ", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask ", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway ", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
最近一直为要设置网卡地址烦恼,网上找了好久资料都没有我真正想要的,下面是我从网上找的一些方法,但我要的是用程序像手动从网络连接属性里该的那种方式
在iphlpapi.dll中有个函数AddIpAddress
但看到人家说的下面这段话,很明显不是我需要的
“最常用的函数 AddIPAddress。这个函数能够设置本地网络的IP。但不幸的是,这个IP是临时的,当系统重新启动或者发生其它的PNP事件的时候这个IP就不存在了。”
另外有些方法如用WMI对象
strComputer = ". "
Set objWMIService = GetObject( "winmgmts:\\ " & strComputer & "\root\cimv2 ")
Set colNetAdapters = objWMIService.ExecQuery _
( "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE ")
strIPAddress = Array( "192.168.1.141 ")
strSubnetMask = Array( "255.255.255.0 ")
strGateway = Array( "192.168.1.100 ")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed. "
Else
WScript.Echo "The IP address could not be changed. "
End If
Next
对于WMI不是很懂,用起来也麻烦
还有直接改注册表的
HKEY_LOCAL_MACHINE\SYSTEM\Controlset001\Services\{b8584ff4-32a6-43DC-BC53-982A46165FAF}\parameters\Tcpip 里面的IPADDRESS项
可是这路径中的这串数字{b8584ff4-32a6-43DC-BC53-982A46165FAF}在不同的机器上似乎不同的,也不知道用没有什么规律
若要用一个程序设置IP就想手动设置IP一样的可靠应该怎么办呢。甚至有什么办法可以跟踪手动设置IP时WINDOWS是怎么操作的吗?总之有没有什么线索.万分感谢呀
------解决方案--------------------
设置IP地址就是通过写注册表实现的,需要重启系统,因为TCP/IP协议栈需要重启以应用新的IP参数。
------解决方案--------------------
网卡地址要用网卡供应商的驱动程序来改吧,如果随便能改,那这世界不乱套了
------解决方案--------------------
我曾经做过一个定时改IP的小工具,里面有获取网卡信息,更改IP的代码,不过代码在公司电脑里,你留下邮箱,我明天上午发给你可好?
------解决方案--------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
CString strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\ ";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,strKeyName.GetString(), 0, KEY_WRITE, &hKey) != ERROR_SUCCESS)
{
return FALSE;
}
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);
int nIP, nMask, nGate;
nIP = (int)strlen(mszIPAddress);
nMask = (int)strlen(mszNetMask);
nGate = (int)strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress ", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask ", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway ", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)