c# - Using the iphlpapi.dll in .Net to Add new IP Addresses on x64 machines -


i using code add ip addresses 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 working, noticed if reboot machine, ips removed. also, can see them if perform ipconfig command line, not see them in advanced tcp/ip settings dialog. so, ips added or need else ensure ips bound nic card?

ip addresses via command line

enter image description here

the ips are, in fact, added, addipaddress non-persistent:

the ipv4 address added addipaddress function not persistent. ipv4 address exists long adapter object exists. restarting computer destroys ipv4 address, manually resetting network interface card (nic). also, pnp events may destroy address.

to create ipv4 address persists, enablestatic method of win32_networkadapterconfiguration class in windows management instrumentation (wmi) controls may used. netsh commands can used create persistent ipv4 address.

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

you can execute enablestatic method using wmi.net (system.management namespace) like:

var q = new objectquery("select * win32_networkadapterconfiguration 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);  } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -