Problems Using WMI EnableStatic Method

I am trying to create a tool that converts a dynamic DHCP IPv4 address, gateway and dns settings to a static configuration. I tried using WMI to solve this puzzle, but I have a problem that I cannot understand.

The application is completed, the DNS and the gateway are configured, but the EnableStatic method (for setting the IP address and subnet) was unsatisfactory, which means that the IP address is still obtained from DHCP (with the fields highlighted), although the default gateway is set. How to fix it?

The return value from EnableStatic is 70 (invalid IP address). The strange thing is that the input parameters are the same that I extracted from the NIC 2 seconds earlier.

Here is the code (except for the GUI), http://pastebin.com/AE3dGhUz :

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Management; namespace Static_NIC_Settings_Creator { public partial class Form1 : Form { private ManagementObjectCollection queryCollection; private string[] networkInterfaces; private int currentNIC; private string[] ipAddress; private string[] subnetMask; private string[] defaultIPGateway; private string[] dnsServerSearchOrder; public Form1() { InitializeComponent(); getNICs(); } private void convertButton_Click(object sender, EventArgs e) { if (networkInterfaces.Count() > 0) { //Get current NIC settings if (!getNICSettings()) { MessageBox.Show("Retrieving current NIC settings failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Convert to static NIC settings if (!setNICStatic()) { MessageBox.Show("Setting NIC settings to static failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } private void nicSelecter_SelectedIndexChanged(object sender, EventArgs e) { currentNIC = nicSelecter.SelectedIndex; } private void getNICs() { //Get NICS ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"); queryCollection = query.Get(); //Make nic string array int i = queryCollection.Count; networkInterfaces = new string[i]; //Fill nic string array i = 0; foreach (ManagementObject mo in queryCollection) { networkInterfaces[i] = (String)mo["Description"]; i++; } //Fill dropbox with arraylist-data nicSelecter.DataSource = networkInterfaces; } private Boolean getNICSettings() { //Get selected NIC int i = 0; foreach (ManagementObject mo in queryCollection) { //Get settings for specific NIC if (i == currentNIC) { try { ipAddress = (String[])mo["IPAddress"]; subnetMask = (String[])mo["IPSubnet"]; defaultIPGateway = (String[])mo["DefaultIPGateway"]; dnsServerSearchOrder = (String[])mo["DNSServerSearchOrder"]; return true; } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error"); return false; } } i++; } return false; } private Boolean setNICStatic() { //Get selected NIC int i = 0; foreach (ManagementObject mo in queryCollection) { //Get settings for specific NIC if (i == currentNIC) { try { //Set static IP and subnet mask ManagementBaseObject setIP; ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"); newIP["IPAddress"] = ipAddress; newIP["SubnetMask"] = subnetMask; setIP = mo.InvokeMethod("EnableStatic", newIP, null); //Set default gateway ManagementBaseObject setGateway; ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways"); newGateway["DefaultIPGateway"] = defaultIPGateway; newGateway["GatewayCostMetric"] = new int[] { 1 }; setGateway = mo.InvokeMethod("SetGateways", newGateway, null); //Set dns servers ManagementBaseObject setDNS; ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); newDNS["DNSServerSearchOrder"] = dnsServerSearchOrder; setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); System.Windows.Forms.MessageBox.Show("Setting NIC settings returned: " + setDNS); return true; } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString(), "Critical: Unhandled error"); return false; } } i++; } //No NICs return false; } } //End class } 

Any ideas?

+8
c # wmi
source share
1 answer

Maybe you enter IPv6 addresses? Just playing with PowerShell doesn't seem to like it. Perhaps you can post the actual values ​​that are entered during debugging, this would help a lot. Also try statically entering some values, for example:

 new string[]{"192.168.0.1"}, new string[] {"255.255.255.255"} 

Also, if you really do not really need C # and a graphical interface, you can think about how to use PowerShell (this requirement is mandatory), since WMI is much easier to manipulate there (unfortunately, you still have this learning curve) .

This is just an example of using PowerShell, you can at least use it for some testing:

 Get-WmiObject Win32_NetworkAdapterConfiguration 

Then run the index of your adapter, and then replace the index number:

 $obj = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.Index -eq 1} $obj.EnableStatic("192.168.0.1", "255.255.255.0") 

To get only the method parameters:

 $obj.EnableStatic 

He will return:

 MemberType : Method OverloadDefinitions : {System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask)} TypeNameOfValue : System.Management.Automation.PSMethod Value : System.Management.ManagementBaseObject EnableStatic(System.String[]IPAddress, System.String[] SubnetMask) Name : EnableStatic IsInstance : True 
+3
source share

All Articles