如何用VC代码实现修改本机的Ip、子网掩码、默认网关、首选DNS服务器啊?
解决方案 »
- 怎样使用mcisendstring中的break命令?
- VS自动化扩展:请问"语言的区域设置标识符"是什么意思
- _ApplicationPtr和CComPtr的区别
- 如果想在网络中传输long 型和double的数据应该怎么办?
- help me 送分,简单题:)
- 100分,解决后立即揭帖!工具栏中动态创建的Combobox的CBN_SELCHANGE消息函数怎么添加上去的?
- 简单的问题,求帮忙
- 如何程序实现动态注册远程odbc数据源?
- 如何在formview中添加树控件treectrl啊?急求高手解答!
- 快来抢分---如何在程序运行时改变鼠标形状???
- 标准菜单消息处理问题
- 建立了一个新线程 出现问题 高手来~~
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;
}
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
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.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);//GetNumberOfInterfacestypedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfotypedef DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfotypedef DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTabletypedef 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;}
不过需要WIN2000以上的系统才支持。