Question

I am using this code to add IP addresses to a nic card:

[DllImport("iphlpapi.dll", SetLastError = true)]
        private static extern UInt32 AddIPAddress(UInt32 address, UInt32 ipMask, int ifIndex, out IntPtr nteContext,
                                                  out IntPtr nteInstance);

public static UInt32 AddIpAddressToInterface(string ipAddress, string subnetMask, int ifIndex)
        {
            var ipAdd = System.Net.IPAddress.Parse(ipAddress);
            var subNet = System.Net.IPAddress.Parse(subnetMask);
            unsafe
            {
                var nteContext = 0;
                var nteInstance = 0;
                IntPtr ptrNteContext;
                var ptrNteInstance = new IntPtr(nteInstance);
                return AddIPAddress((uint)BitConverter.ToInt32(ipAdd.GetAddressBytes(), 0), (uint)BitConverter.ToInt32(subNet.GetAddressBytes(), 0), ifIndex, out ptrNteContext,
                                    out ptrNteInstance);
            }
        }

It seems to be working, but I noticed if I reboot the machine, the IPs are removed. Also, I can see them if I perform an ipconfig from a command line, but I do not see them in the Advanced TCP/IP settings dialog. So, are the IPS really added or do I need do something else to ensure the IPs are bound to the nic card?

IP Addresses via command line

enter image description here

Était-ce utile?

La solution

the IPs are, in fact, added, but AddIPAddress is non-persistent:

The IPv4 address added by the AddIPAddress function is not persistent. The IPv4 address exists only as long as the adapter object exists. Restarting the computer destroys the IPv4 address, as does manually resetting the network interface card (NIC). Also, certain PnP events may destroy the address.

To create an IPv4 address that persists, the EnableStatic method of the Win32_NetworkAdapterConfiguration Class in the Windows Management Instrumentation (WMI) controls may be used. The netsh commands can also be used to create a persistent IPv4 address.

Source: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365801%28v=vs.85%29.aspx

You can execute the EnableStatic method using WMI.NET (System.Management Namespace) like:

var q = new ObjectQuery("select * from Win32_NetworkAdapterConfiguration where InterfaceIndex=25");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(q);

foreach (ManagementObject nic in searcher.Get()) 
{
    ManagementBaseObject newIP = nic.GetMethodParameters("EnableStatic");
    newIP["IPAddress"] = new string[]{"192.168.0.1"};
    newIP["SubnetMask"] = new string[]{"255.255.255.0"};
    nic.InvokeMethod("EnableStatic", newIP, null); 
}

Autres conseils

As noted, AddIPAddress and all of iphlpapi.dll show and control the dynamic configuration, which doesn't get persisted.

You can set the static persisted configuration, which would show up in the TCP/IP settings dialogs, using netsh - run netsh interface ipv4 set /? to see how. It's programmatically accessible via the INetCfg interface, but I think some of it is undocumented.

The WMI interfaces are wrappers which mix stuff from both sources, which is why I recommend against using them (as you've noticed, they won't configure disconnected NICs).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top