VC 默认网关
场景:怎么用VC代码实现修改本机的Ip、子网掩码、默认网关、首选DNS服务器啊
如何用VC代码实现修改本机的Ip、子网掩码、默认网关、首选DNS服务器啊?
如何用VC代码实现修改本机的Ip、子网掩码、默认网关、首选DNS服务器啊?
------解决方案--------------------
Ip、子网掩码、默认网关、首选DNS服务器相关信息在注册表之中,改变注册表的值,然后通知所以应用程序即可,代码如下:
BOOL SetIP(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,
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 = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = 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);
//通知IP地址改变
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
if((hDhcpDll = LoadLibrary( "dhcpcsvc ")) == NULL)
return FALSE;
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange ")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return TRUE;
}
------解决方案--------------------
可用wmi
------解决方案--------------------
这是vbscript写法
'将计算机的 IP 地址设置为 192.168.1.141,并将 IP 网关设置为 192.168.1.100。
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
------解决方案--------------------
Win32_NetworkAdapterConfiguration
The Win32_NetworkAdapterConfiguration WMI class represents the attributes and behaviors of a network adapter. This class includes extra properties and methods that support the management of the TCP/IP and Internetworking Packet Exchange (IPX) protocols that are independent from the network adapter.
The following syntax is simplified from Managed Object Format (MOF) code and includes all inherited properties.
class Win32_NetworkAdapterConfiguration : CIM_Setting
{
boolean ArpAlwaysSourceRoute;
boolean ArpUseEtherSNAP;
string Caption;
string DatabasePath;
boolean DeadGWDetectEnabled;
string DefaultIPGateway[];
uint8 DefaultTOS;
uint8 DefaultTTL;
string Description;
boolean DHCPEnabled;
datetime DHCPLeaseExpires;
datetime DHCPLeaseObtained;
string DHCPServer;
string DNSDomain;
string DNSDomainSuffixSearchOrder[];
boolean DNSEnabledForWINSResolution;
string DNSHostName;
string DNSServerSearchOrder[];
boolean DomainDNSRegistrationEnabled;
uint32 ForwardBufferMemory;
boolean FullDNSRegistrationEnabled;
uint16 GatewayCostMetric[];
uint8 IGMPLevel;
uint32 Index;
uint32 InterfaceIndex;
string IPAddress[];
uint32 IPConnectionMetric;
boolean IPEnabled;
boolean IPFilterSecurityEnabled;
boolean IPPortSecurityEnabled;
string IPSecPermitIPProtocols[];
string IPSecPermitTCPPorts[];
string IPSecPermitUDPPorts[];
string IPSubnet[];
boolean IPUseZeroBroadcast;
string IPXAddress;
boolean IPXEnabled;
uint32 IPXFrameType[];
uint32 IPXMediaType;
string IPXNetworkNumber[];
string IPXVirtualNetNumber;
uint32 KeepAliveInterval;
uint32 KeepAliveTime;
string MACAddress;
uint32 MTU;
uint32 NumForwardPackets;
boolean PMTUBHDetectEnabled;
boolean PMTUDiscoveryEnabled;
string ServiceName;
string SettingID;
uint32 TcpipNetbiosOptions;
uint32 TcpMaxConnectRetransmissions;
uint32 TcpMaxDataRetransmissions;
uint32 TcpNumConnections;
boolean TcpUseRFC1122UrgentPointer;
uint16 TcpWindowSize;
boolean WINSEnableLMHostsLookup;
string WINSHostLookupFile;
string WINSPrimaryServer;
string WINSScopeID;
string WINSSecondaryServer;
};
Methods
The Win32_NetworkAdapterConfiguration class defines the following methods.
------解决方案--------------------
用iphelper可以解决很多问题,下面是使用的例子
// IpHelper.cpp : Defines the entry point for the console application.
//
#include "stdafx.h "
#include <stdio.h>
#include "windows.h "
#include "iostream.h "
#include "iphlpapi.h "
typedef DWORD(CALLBACK * PGNOINTERFACE)(PDWORD);//GetNumberOfInterfaces
typedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfo
typedef DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfo
typedef DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTable
typedef DWORD(CALLBACK * PAIA)(IPAddr,IPMask,DWORD,PULONG,PULONG);//AddIPAddress
int main(int argc, char* argv[])
{
DWORD index=0;
//函数指针
PGAINFO pGAInfo;
//加载IP Helper API 所需的库文件
HINSTANCE hInst;//实例句柄
hInst=LoadLibrary( "iphlpapi.dll ");
if(!hInst)
cout < < "iphlpapi.dll not supported in this platform!\n ";
cout < < "net adapters information: " < <endl < <endl;
//------------------------------------》获得网卡数据
pGAInfo=(PGAINFO)GetProcAddress(hInst, "GetAdaptersInfo ");
PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
ULONG ulSize=0;
pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
pGAInfo(pInfo,&ulSize);
//遍历每一张网卡
while(pInfo)
{
//网卡名
cout < < "adapter name: " < <pInfo-> AdapterName < <endl;
//网卡描述信息
cout < < "description: " < <pInfo-> Description < <endl;
//记录下它的索引值以便后面使用。
index=pInfo-> Index;
//物理地址的长度
cout < < "hardware address length: " < <pInfo-> AddressLength < <endl;
//显示物理地址
cout < < "hardware address: ";
cout.setf(ios::hex);
cout.unsetf(ios::dec);
for(int i=0;i <(int)pInfo-> AddressLength;i++)
cout < <(unsigned int)pInfo-> Address[i];
cout < <endl;
cout.unsetf(ios::hex);
cout.setf(ios::dec);
//显示绑定于这张网卡之上的IP地址
cout < < "ip address bound to this chapter: " < <endl;
PIP_ADDR_STRING pAddTemp=&(pInfo-> IpAddressList);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//显示当前使用的IP地址
//因为在离线状态下pInfo-> CurrentIpAddress被置空,所以有必要做检查
if(pInfo-> CurrentIpAddress)
{
cout < < "current ip using: " < <endl;
pAddTemp=pInfo-> CurrentIpAddress;
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
}
else
cout < < "network malfunctioning,no ip is in use!\n " < <endl;
//显示DHCP 服务器数据
cout < < "DHCP in use: " < <endl;
pAddTemp=&(pInfo-> DhcpServer);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//将当前指针移向下一个
pInfo=pInfo-> Next;
}
delete pInfoTemp;//回收无用内存
//------------------------------------》获得网卡数据部分结束
cout < <endl < <endl;
//函数指针
PGNOINTERFACE pGNOInterface;
PGIINFO pGIInfo;
cout < < "network interfaces information: " < <endl < <endl;
//------------------------------------》网络接口信息部分
//显示网络接口的个数
DWORD ulNumOfInterfaces=0;
pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst, "GetNumberOfInterfaces ");
pGNOInterface(&ulNumOfInterfaces);
cout < < "U have " < <ulNumOfInterfaces < < " network interfaces\n ";
//获取网络接口信息
pGIInfo=(PGIINFO)GetProcAddress(hInst, "GetInterfaceInfo ");
PIP_INTERFACE_INFO pIInfo=NULL;
ulSize=0;
pGIInfo(pIInfo,&ulSize);//第一次调用,获取缓冲区大小
pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]);
pGIInfo(pIInfo,&ulSize);
//显示网络接口信息
for(int i=0;i <pIInfo-> NumAdapters;i++)
{
cout < < "Adapter index: " < <pIInfo-> Adapter[i].Index < <endl;
cout < < "and name: " < <pIInfo-> Adapter[i].Name < <endl;
}
delete pIInfo;
//------------------------------------》网络接口信息部分结束
cout < <endl < <endl;
//函数指针
PGIAT pGIAT;
cout < < "ip information: " < <endl < <endl;
//------------------------------------》IP绑定信息
pGIAT=(PGIAT)GetProcAddress(hInst, "GetIpAddrTable ");
PMIB_IPADDRTABLE pIPTable=NULL;
ulSize=0;
pGIAT(pIPTable,&ulSize,TRUE);//获得缓冲区大小
pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]);
pGIAT(pIPTable,&ulSize,TRUE);
for(i=0;i <pIPTable-> dwNumEntries;i++)
{
//取出每一个字段,显示IP
cout < < "ip address: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwAddr)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwAddr)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwAddr)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwAddr))> > 8) < <endl;
//显示绑定网络接口的索引
cout < < "it is bound to interface: " < <pIPTable-> table[i].dwIndex < <endl;
//显示子网掩码
/* cout < < "it 's net mask: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwMask)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwMask)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwMask)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwMask))> > 16) < <endl;*/
char szMask[200];
sprintf( "%s ",inet_ntoa(pIPTable-> table[0].dwMask));
cout < <szMask < <endl;
//显示广播地址
cout < < "and broadcast addres: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwBCastAddr)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwBCastAddr)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwBCastAddr)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwBCastAddr))> > 16) < <endl;
//显示最大报文大小
cout < < "it 's reassembly size: " < <pIPTable-> table[i].dwReasmSize < <endl;
}
//------------------------------------》IP绑定信息结束
PAIA pAIA;
/*
//------------------------------------》IP设置部分
if(!index)
{
cout < < "no adapters available, cannot set ip address! " < <endl;
return 0;//没有网络接口可以设置
}
pAIA=(PAIA)GetProcAddress(hInst, "AddIPAddress ");
IPAddr addr=0x184BC5CA;
IPMask mask=0x00FFFFFF;
ULONG context;
ULONG Inst;
pAIA(addr,mask,index,&context,&Inst);
//------------------------------------》IP设置部分结束
cout < <endl < <endl;
*/
return 0;
}
------解决方案--------------------
最方便的办法就是使用iphelper(需要下载新的Platform SDK),
不过需要WIN2000以上的系统才支持。
如何用VC代码实现修改本机的Ip、子网掩码、默认网关、首选DNS服务器啊?
如何用VC代码实现修改本机的Ip、子网掩码、默认网关、首选DNS服务器啊?
------解决方案--------------------
Ip、子网掩码、默认网关、首选DNS服务器相关信息在注册表之中,改变注册表的值,然后通知所以应用程序即可,代码如下:
BOOL SetIP(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,
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 = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = 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);
//通知IP地址改变
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
if((hDhcpDll = LoadLibrary( "dhcpcsvc ")) == NULL)
return FALSE;
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange ")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return TRUE;
}
------解决方案--------------------
可用wmi
------解决方案--------------------
这是vbscript写法
'将计算机的 IP 地址设置为 192.168.1.141,并将 IP 网关设置为 192.168.1.100。
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
------解决方案--------------------
Win32_NetworkAdapterConfiguration
The Win32_NetworkAdapterConfiguration WMI class represents the attributes and behaviors of a network adapter. This class includes extra properties and methods that support the management of the TCP/IP and Internetworking Packet Exchange (IPX) protocols that are independent from the network adapter.
The following syntax is simplified from Managed Object Format (MOF) code and includes all inherited properties.
class Win32_NetworkAdapterConfiguration : CIM_Setting
{
boolean ArpAlwaysSourceRoute;
boolean ArpUseEtherSNAP;
string Caption;
string DatabasePath;
boolean DeadGWDetectEnabled;
string DefaultIPGateway[];
uint8 DefaultTOS;
uint8 DefaultTTL;
string Description;
boolean DHCPEnabled;
datetime DHCPLeaseExpires;
datetime DHCPLeaseObtained;
string DHCPServer;
string DNSDomain;
string DNSDomainSuffixSearchOrder[];
boolean DNSEnabledForWINSResolution;
string DNSHostName;
string DNSServerSearchOrder[];
boolean DomainDNSRegistrationEnabled;
uint32 ForwardBufferMemory;
boolean FullDNSRegistrationEnabled;
uint16 GatewayCostMetric[];
uint8 IGMPLevel;
uint32 Index;
uint32 InterfaceIndex;
string IPAddress[];
uint32 IPConnectionMetric;
boolean IPEnabled;
boolean IPFilterSecurityEnabled;
boolean IPPortSecurityEnabled;
string IPSecPermitIPProtocols[];
string IPSecPermitTCPPorts[];
string IPSecPermitUDPPorts[];
string IPSubnet[];
boolean IPUseZeroBroadcast;
string IPXAddress;
boolean IPXEnabled;
uint32 IPXFrameType[];
uint32 IPXMediaType;
string IPXNetworkNumber[];
string IPXVirtualNetNumber;
uint32 KeepAliveInterval;
uint32 KeepAliveTime;
string MACAddress;
uint32 MTU;
uint32 NumForwardPackets;
boolean PMTUBHDetectEnabled;
boolean PMTUDiscoveryEnabled;
string ServiceName;
string SettingID;
uint32 TcpipNetbiosOptions;
uint32 TcpMaxConnectRetransmissions;
uint32 TcpMaxDataRetransmissions;
uint32 TcpNumConnections;
boolean TcpUseRFC1122UrgentPointer;
uint16 TcpWindowSize;
boolean WINSEnableLMHostsLookup;
string WINSHostLookupFile;
string WINSPrimaryServer;
string WINSScopeID;
string WINSSecondaryServer;
};
Methods
The Win32_NetworkAdapterConfiguration class defines the following methods.
------解决方案--------------------
用iphelper可以解决很多问题,下面是使用的例子
// IpHelper.cpp : Defines the entry point for the console application.
//
#include "stdafx.h "
#include <stdio.h>
#include "windows.h "
#include "iostream.h "
#include "iphlpapi.h "
typedef DWORD(CALLBACK * PGNOINTERFACE)(PDWORD);//GetNumberOfInterfaces
typedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfo
typedef DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfo
typedef DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTable
typedef DWORD(CALLBACK * PAIA)(IPAddr,IPMask,DWORD,PULONG,PULONG);//AddIPAddress
int main(int argc, char* argv[])
{
DWORD index=0;
//函数指针
PGAINFO pGAInfo;
//加载IP Helper API 所需的库文件
HINSTANCE hInst;//实例句柄
hInst=LoadLibrary( "iphlpapi.dll ");
if(!hInst)
cout < < "iphlpapi.dll not supported in this platform!\n ";
cout < < "net adapters information: " < <endl < <endl;
//------------------------------------》获得网卡数据
pGAInfo=(PGAINFO)GetProcAddress(hInst, "GetAdaptersInfo ");
PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
ULONG ulSize=0;
pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
pGAInfo(pInfo,&ulSize);
//遍历每一张网卡
while(pInfo)
{
//网卡名
cout < < "adapter name: " < <pInfo-> AdapterName < <endl;
//网卡描述信息
cout < < "description: " < <pInfo-> Description < <endl;
//记录下它的索引值以便后面使用。
index=pInfo-> Index;
//物理地址的长度
cout < < "hardware address length: " < <pInfo-> AddressLength < <endl;
//显示物理地址
cout < < "hardware address: ";
cout.setf(ios::hex);
cout.unsetf(ios::dec);
for(int i=0;i <(int)pInfo-> AddressLength;i++)
cout < <(unsigned int)pInfo-> Address[i];
cout < <endl;
cout.unsetf(ios::hex);
cout.setf(ios::dec);
//显示绑定于这张网卡之上的IP地址
cout < < "ip address bound to this chapter: " < <endl;
PIP_ADDR_STRING pAddTemp=&(pInfo-> IpAddressList);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//显示当前使用的IP地址
//因为在离线状态下pInfo-> CurrentIpAddress被置空,所以有必要做检查
if(pInfo-> CurrentIpAddress)
{
cout < < "current ip using: " < <endl;
pAddTemp=pInfo-> CurrentIpAddress;
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
}
else
cout < < "network malfunctioning,no ip is in use!\n " < <endl;
//显示DHCP 服务器数据
cout < < "DHCP in use: " < <endl;
pAddTemp=&(pInfo-> DhcpServer);
while(pAddTemp)/*遍历IP列表中的每一个元素*/
{
cout < <pAddTemp-> IpAddress.String < <endl;
pAddTemp=pAddTemp-> Next;
}
//将当前指针移向下一个
pInfo=pInfo-> Next;
}
delete pInfoTemp;//回收无用内存
//------------------------------------》获得网卡数据部分结束
cout < <endl < <endl;
//函数指针
PGNOINTERFACE pGNOInterface;
PGIINFO pGIInfo;
cout < < "network interfaces information: " < <endl < <endl;
//------------------------------------》网络接口信息部分
//显示网络接口的个数
DWORD ulNumOfInterfaces=0;
pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst, "GetNumberOfInterfaces ");
pGNOInterface(&ulNumOfInterfaces);
cout < < "U have " < <ulNumOfInterfaces < < " network interfaces\n ";
//获取网络接口信息
pGIInfo=(PGIINFO)GetProcAddress(hInst, "GetInterfaceInfo ");
PIP_INTERFACE_INFO pIInfo=NULL;
ulSize=0;
pGIInfo(pIInfo,&ulSize);//第一次调用,获取缓冲区大小
pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]);
pGIInfo(pIInfo,&ulSize);
//显示网络接口信息
for(int i=0;i <pIInfo-> NumAdapters;i++)
{
cout < < "Adapter index: " < <pIInfo-> Adapter[i].Index < <endl;
cout < < "and name: " < <pIInfo-> Adapter[i].Name < <endl;
}
delete pIInfo;
//------------------------------------》网络接口信息部分结束
cout < <endl < <endl;
//函数指针
PGIAT pGIAT;
cout < < "ip information: " < <endl < <endl;
//------------------------------------》IP绑定信息
pGIAT=(PGIAT)GetProcAddress(hInst, "GetIpAddrTable ");
PMIB_IPADDRTABLE pIPTable=NULL;
ulSize=0;
pGIAT(pIPTable,&ulSize,TRUE);//获得缓冲区大小
pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]);
pGIAT(pIPTable,&ulSize,TRUE);
for(i=0;i <pIPTable-> dwNumEntries;i++)
{
//取出每一个字段,显示IP
cout < < "ip address: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwAddr)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwAddr)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwAddr)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwAddr))> > 8) < <endl;
//显示绑定网络接口的索引
cout < < "it is bound to interface: " < <pIPTable-> table[i].dwIndex < <endl;
//显示子网掩码
/* cout < < "it 's net mask: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwMask)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwMask)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwMask)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwMask))> > 16) < <endl;*/
char szMask[200];
sprintf( "%s ",inet_ntoa(pIPTable-> table[0].dwMask));
cout < <szMask < <endl;
//显示广播地址
cout < < "and broadcast addres: " < <(unsigned int)((LOWORD(pIPTable-> table[i].dwBCastAddr)&0x00FF)) < < ". "\
< <(unsigned int)((LOWORD(pIPTable-> table[i].dwBCastAddr)> > 8)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwBCastAddr)&0x00FF)) < < ". "\
< <(unsigned int)((HIWORD(pIPTable-> table[i].dwBCastAddr))> > 16) < <endl;
//显示最大报文大小
cout < < "it 's reassembly size: " < <pIPTable-> table[i].dwReasmSize < <endl;
}
//------------------------------------》IP绑定信息结束
PAIA pAIA;
/*
//------------------------------------》IP设置部分
if(!index)
{
cout < < "no adapters available, cannot set ip address! " < <endl;
return 0;//没有网络接口可以设置
}
pAIA=(PAIA)GetProcAddress(hInst, "AddIPAddress ");
IPAddr addr=0x184BC5CA;
IPMask mask=0x00FFFFFF;
ULONG context;
ULONG Inst;
pAIA(addr,mask,index,&context,&Inst);
//------------------------------------》IP设置部分结束
cout < <endl < <endl;
*/
return 0;
}
------解决方案--------------------
最方便的办法就是使用iphelper(需要下载新的Platform SDK),
不过需要WIN2000以上的系统才支持。