WMI 修改PC ip地址,子网掩码,网关
(为什么采用这种方式,因为采用修改注册表,通知网络适配器(DhcpNotifyConfigChange)的方式,不支持Vista,和Windows7操作系统,为此才采用这种方式);
讲解一下基本流程:
CoInitializeEx
CoInitializeSecurity
CoCreateInstance
ConnectServer
GetMethod
SpawnInstance
SafeArrayCreate
SafeArrayPutElement
pInParams->Put
ExecMethod
以上是基本流程;
对于本地PC有多个网卡的,如何解决啊;
解决方式:
1.通过GetAdaptersInfo获取,IP_ADAPTER_INFO结构体中AdaterName;(参考:不重起Windows直接更改IP地址
http://www.vckbase.com/document/viewdoc/?id=851)
2.在通过Win32_NetworkAdapterConfiguration 中的SettingID等于 AdaterName查询出index;
3.通过index,保存(Put)到相应的值;
注意的问题:
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 ;
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:
EnableDHCP
EnableStatic(修改IP地址和子网掩码)对应Win32_NetworkAdapterConfiguration 中的IPAddress和SubnetMask;
上面两个要单独使用,用ExecMethod调用了EnableDHCP就不要调用EnableStatic,在执行过程中容易使PC产生两个IP地址(Windows7);
SetGateways 设置网关的方法:对应Win32_NetworkAdapterConfiguration 中的DefaultIPGateway;
用此种方法(WMI)就不要用,修改注册表的方式来修改IP地址其它参数了。
微信
支付宝