Use WMI to modify the network settings of your computer

xiaoxiao2021-03-06  52

Use WMI to modify the network settings of your computer

MONTAQUE (Hou Yongfeng)

Received:

1, personal personal experience, for reference 2, when reprinted, please keep the original

Overview:

In the previous article, briefly introduces how to use the WMI to get software and hardware settings, interested in refer to the last article (http://www.9cbs.net/develop/read_article.asp?id=15744). Today, we use the programmed way to read and write system network settings, mainly including the IP address, network mask, and DNS server and gateway of the local network card. Mainly using Win32_NetworkAdapterConfiguration WMI class.

Idea:

The ManagementObject class represents the data management object, and the WIN32_NetworkAdapterConfiguration class is then called by using WQL (WMI Query Language). For the ManagementObject object, the name and parameters of the call method can be passed to the InvokeMethod method to obtain the method of managing the management object class.

The Win32_NetworkAdapterConfiguration class contains some properties and methods related to the computer network adapter settings. This class supports some methods and properties of TCP / IP protocols that are independent of specific network adapters and IPX protocol management.

The properties of some Win32_NetworkAdapterConfiguration are listed below (managed object format)

Class Win32_NetworkAdapterConfiguration: CIM_SETTING

{

Boolean ArpAlwaysSources; Boolean Arpuseethersnap;

String Caption;

String DatabasePath;

Boolean deadGwdetectenable;

String defaultiPgateway [];

Uint8 defaulttos;

Uint8 defaulttl;

String description;

Boolean DHCPENABLED;

DateTime DHCPLEAXPIRES;

DateTime DHCPLEaseObtained;

String DHCPSERVER;

String DNSDomain;

String DNSDomainsuffixsearchOrder [];

Boolean DNSenableDforwinsResolution;

String DNSHOSTNAME;

String DNSSERVERSEARCHORDER [];

Boolean DomainDnsRegistrationnabled;

Uint32 forwardbuffermemory;

Boolean FulldnsRegistrationnable;

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 ipxenable;

UINT32 IPXFRAMEPE [];

UINT32 IPXMEDITYPE;

String ipxnetworknumber [];

String ipxvirtualnetNumber;

Uint32 KeepaliveInterVal;

UINT32 KeepaliveTime;

String macaddress;

UINT32 MTU;

Uint32 NumForwardPacket;

Boolean Pmtubhdtectectenable;

Boolean PmtudiscoveryEnabled;

String serviceName;

String settingId;

UINT32 TCPIPNETBIOSOPTIONS;

UINT32 TCPMAXCONNECTRENSMISSIONS;

Uint32 TCPMaxDataRetransmissions;

UINT32 TCPNUMCONNECTIONS;

Boolean tcpuserfc1222urgentpointer;

UINT16 TCPWINDOWSIZE;

Boolean Winsenablelmhostslookup;

String WinShostLookupfile;

String WinsprimaryServer;

String WinsScopeID;

String WinssecondaryServer;

}

We can use the following code to get the network card settings:

Code:

Imports System.Management

DIM Searcher As New ManagementObjectSearcher ("SELECT * from Win32_NetworkAdapterConfiguration")

Dim Share As ManagementObject

For Each Share in Searcher.get ()

Messagebox.show (Share.getText (TextFormat.mof))

Next Share

result:

Instance of Win32_NetworkAdapterConfiguration

{

CAPTION = "[00000001] Intel (r) Pro / 100 S Desktop Adapter";

DatabasePath = "% systemroot% // system32 // drivers // ETC";

DEFAULTIPGATEWAY = {"192.168.0.254"}; "gateway

Description = "Intel (r) Pro / 100 S Desktop Adapter"; 'This is the only one

DHCPENABLED = FALSE;

DNSENABLEDFORWINSRESD = false;

DNSHOSTNAME = "Monkey";

DNSSERVERSEARCHORDER = {"192.168.0.254", "202.120.190.208"}; 'two alternative DNS

DomainDnsregistrationenabled = false;

FullDnsregistrationenabled = true;

GatewayCostMetric = {1};

INDEX = 1;

InterfaceIndex = 65539;

Ipaddress = {"192.168.0.161"}; 'Local iPipConnectionMetric = 20;

Ipenabled = true;

Ipfiltersecurityenabled = false;

IpsecPermitipProtocols = {"0"};

IPsecPermittcpports = {"0"};

IpsecPermitudpports = {"0"};

Ipsubnet = {"255.255.255.0"}; 'mask

IPXENABLED = FALSE;

Macaddress = "52: 54: AB: DD: 64: 03";

ServiceName = "E100B";

SettingId = "{F5553268-53F8-48F6-A818-1432EEE04AE1}"

Tcpipnetbiosoptions = 0;

Winsenablelmhostslookup = true;

WinsScopeID = ""

}

How to change these settings for the NIC? We use the Win32_NetworkAdapterConfiguration to use the method of win32_networkadapterconfiguration. The methods we use have the following methods:

Enablestatic

Enable static TCP / IP settings, enter two parameters one is a local IP address, and the other is a network mask. // General We all use this method to set up a network card IP attribute

Setgateway

Specify a series of gateways

SetDNSSERVERSEARCHORDER

Incorporate DNS list in the form of an array

Below is a snippet, set the correspondence network card according to the NIC Description in the list box.

The main code:

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

GetAllConfigurationalLenetCard ()

End Sub

'Modification setting

Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click

DIM Searcher As New ManagementObjectSearcher ("Select * from win32_networkadapterconfiguration where description =" "" & me.combobox1.selectedItem.toString & "" ")

Dim Share As ManagementObject

DIM IRESULT AS INTEGER = 0 'Judging whether to modify success. If successful, the return value of the following three methods is 0, pay attention to the returned value is the UINT32 type.

For Each Share in Searcher.get ()

Console.writeline (Share.getText (TextFormat.mof))

IF not (Share.Item ("ipaddress" is nothing) THEN

'set address

DIM ipaddresses () as string = {textbox1.text} 'IP address DIM IPSUBNETMASK () AS STRING = {textBox2.text}' mask

Dim objpara () as object = {ipaddresses, ipsubnetmask}

IRESULT = Convert.Toint32 (Share.InvokeMethod ("EnableStatic", Objpara)

DIM ipgateway () as string = {textbox3.text} 'gateway

DIM GATEWAYCOSTMETRIC () AS uint16 = {uint16.parse ("1")}

Dim objpara1 () as object = {ipgateway, gatewaycostmetric}

IRESULT = Convert.Toint32 (Share.InvokeMethod ("setgateways", objpara1))

'set DNS Server Sercer Sera ORDER

'Middle DNS is separated by minus

If Me.TextBox4.text.indexof ("-")> 0 THEN

DIM DNSSERVERS AS STRING () = textbox4.text.split ("-")

Dim objPara2 () as object = {dnsservers}

IRESULT = Convert.Toint32 (Share.InvokeMethod ("SetDnsserversearchOrder", Objpara2))

Else

DIM DNSSERVERS AS STRING () = {me.textbox4.text}

Dim objPara2 () as object = {dnsservers}

IRESULT = Convert.Toint32 (Share.InvokeMethod ("SetDnsserversearchOrder", Objpara2))

END IF

If IResult = 0 THEN

Messagebox.show ("Successful Modification")

Else

Messagebox.show ("Modify Failure, Check the format of the input and whether you have permission")

END IF

END IF

Next Share

End Sub

'Get a list of network cards

Private sub getAllConfigurationArtCard ()

DIM Searcher As New ManagementObjectSearcher ("SELECT * from Win32_NetworkAdapterConfiguration")

Dim Share As ManagementObject

Me.comboBox1.Items.clear ()

For Each Share in Searcher.get ()

IF not (Share.Item ("ipaddress" is nothing) THEN

Me.comBobox1.Items.Ad (Share ("Description"))

END IF

Next Share

Me.comboBox1.selectedIndex = 0

End Sub

'Get detailed information on the network card

Private sub getconfiguraton (ByVal Description As String)

IF description is nothing or description.length = 0 thenexit SUB

Else

DIM Searcher As New ManagementObjectSearcher ("Select * from win32_networkadapterconfiguration where description =" "" & description & "" ")

Dim Share As ManagementObject

For Each Share in Searcher.get ()

Me.TextBox1.text = DirectCast (Share.Item ("ipaddress"), string ()) (0) .tostring

Me.TextBox2.text = Directcast (Share.Item ("IPSUBNET"), String ()) (0) .tostring

Me.TextBox3.Text = Directcast (Share.Item ("defaultiPgateway"), string ()) (0) .tostring

DIM I as integer

Me.TextBox4.clear ()

For i = 0 to Directcast (Share.Item ("DNSSERVERSEARCHORDER"), STRING ()). GetLength (0) - 1

Me.TextBox4.text & = Directcast (Share.Item ("DNSSERVERSEARCHORDER"), String ()) (i) .tostring & "-"

NEXT

IF i> = 2 THEN

Me.TextBox4.forecolor = systemcolors.Hottrack

Else

Me.TextBox4.ForeColor = Me.TextBox3.Forecolor

END IF

Me.TextBox4.text = Me.TextBox4.text.remove (Me.TextBox4.Text.length - 1, 1)

Me.TextBox5.text = Share.Item ("macaddress"). Tostring

EXIT for

Next Share

END IF

End Sub

Note where you need to pay attention:

The calling method parameter passed by InvokeMethod is an Object type and requires a packing operation.

The return value of EnableStatic SetGateWays SetDnsserverseArchOrder is UINT32, if the execution is successful returns 0, if it is successful, then returns 1 if it is executed and needs to restart the computer.

These properties of the Win32_NetworkAdapterConfiguration method are generally read-only, and the SETPROPERTYVALUE method for calling the management object is invalid.

reference:

MS-Help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/wmisdk/wmi/win32_networkadapterconfiguration.htm

If you need a detailed code example, please send me email: rainman_am_i@hotmail.com Please do not send more than 100K attachments, otherwise it is directly dropped directly :)

2003-10-03 Shanghai

转载请注明原文地址:https://www.9cbs.com/read-116842.html

New Post(0)