Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Enumerate the subkeys in: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\NetworkCards
Each subkey represents a network card in the machine (including virtual network cards, such as dial up adapters). In this subkey we can find useful information about the network card, such as its name, description, driver or manufacturer. The entry we are looking for is ServiceName. It indicates the name of the service associated to this network card in the service database.
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\{ServiceName}\Parameters\
If the network card is bound to TCP/IP, TCP/IP parameters for this card are under subkey TcpIP. TCP/IP parameters are well documented in MSDN. If you just want to change the IP address, we should look at:
IPAddress: REG_MULTI_SZ: This is the list of IP Addresses for this adapter. Notice that DHCPEnabled must be set to FALSE for these addresses to be used. DHCPEnabled: REG_DWORD: This is wether this adapter uses DHCP to get configuration information or not. If we are setting the IP Address, we must set this to FALSE (0).
; Right way to change the IP address of an adapter. ; The disadvantage is that you have to reboot your machine. ipaddr = AskLine ("Ip Address Changer", "Enter the new IP address", "172.16.") keypath = "Software\Microsoft\Windows NT\CurrentVersion\NetworkCards" key=RegOpenkey(@RegMachine, keypath) keylist = RegQueryKeys(key) keycount = ItemCount(keylist,@tab) for xx = 1 to keycount subkey = ItemExtract(xx,keylist,@tab) subkeypath = StrCat(keypath,"\",subkey,"[ServiceName]") if RegExistValue(@RegMachine,subkeypath) servicename=RegQueryValue(@regmachine,subkeypath) subkey = StrCat("SYSTEM\CurrentControlSet\Services\",ServiceName,"\Parameters\TcpIp") if RegExistKey(@regmachine,subkey) ;RegSetDword(@regmachine,StrCat(subkey,"[DHCPEnabled]"),0);DHCPEnabled must be set to FALSE for these addresses to be used. ;RegSetMulSz(@regmachine,StrCat(subkey,"[IpAddress]"),ipaddr," ") endif endif nextThe above is the right way to change the IP address of an adapter. The disadvantage is that you have to reboot your machine. TCP/IP reads these parameters every time it binds to an adapter (usually, at system startup). The only way to make IP to read those parameters again is forcing it to unbind from the adapter and bind to it again. It happens when you unload the network card driver and load it again. Unfortunately, not all the network card adapter drivers can unload and load themselves (only NDIS 4.0 drivers can and, usually, only PCMCIA drivers do). You can try stopping your device driver and trying to start it again from Control Panel\Devices. It doesn't work for the network cards in my computer.
The change of IP address of an adpater is not a trivial operation, anyway. You can lose data, because you force all the sockets to close, all the services to restart on that adapter, the route table must be updated, etc., etc.
To programmatically launch NetSh.exe with a command line, you can use the Run function.
I found a sample at http://cwashington.netreach.net/depo/view.asp?Index=628&ScriptType=vbscript. Though it is VBScript, you may get hints from it.
WMI:
Win32_NetworkAdapterConfiguration:
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_6oq6.asp
Methods: EnableStatic + EnableDHCP
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_0ujy.asp
http://msdn.microsoft.com/library/en-us/wmisdk/r_32hard4_27u6.asp
Change ip VB-Scripts:
http://desktopengineer.com/index.php?topic=0080WMI
http://cwashington.netreach.net/depo/view.asp?Index=628
VERY simplified WMI sample: // =============================================================================== using System; using System.Management; using System.Threading; namespace WmiIpChanger { class IpChanger { [MTAThread] static void Main(string[] args) { ReportIP(); // SwitchToDHCP(); SwitchToStatic(); Thread.Sleep( 5000 ); ReportIP(); Console.WriteLine( "end." ); } static void SwitchToDHCP() { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach( ManagementObject mo in moc ) { if( ! (bool) mo["IPEnabled"] ) continue; inPar = mo.GetMethodParameters("EnableDHCP"); outPar = mo.InvokeMethod( "EnableDHCP", inPar, null ); break; } } static void SwitchToStatic() { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach( ManagementObject mo in moc ) { if( ! (bool) mo[ "IPEnabled" ] ) continue; inPar = mo.GetMethodParameters( "EnableStatic" ); inPar["IPAddress"] = new string[] { "192.168.1.1" }; inPar["SubnetMask"] = new string[] { "255.255.255.0" }; outPar = mo.InvokeMethod( "EnableStatic", inPar, null ); break; } } static void ReportIP() { Console.WriteLine( "****** Current IP addresses:" ); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach( ManagementObject mo in moc ) { if( ! (bool) mo[ "IPEnabled" ] ) continue; Console.WriteLine( "{0}\n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"], (string) mo["ServiceName"], (string) mo["MACAddress"] ); string[] addresses = (string[]) mo[ "IPAddress" ]; string[] subnets = (string[]) mo[ "IPSubnet" ]; Console.WriteLine( " Addresses :" ); foreach(string sad in addresses) Console.WriteLine( "\t'{0}'", sad ); Console.WriteLine( " Subnets :" ); foreach(string sub in subnets ) Console.WriteLine( "\t'{0}'", sub ); } } } } // ===============================================================================WARNING: do MUCH more error checking, multiple NIC tests, timing!... use all at at your own risk!
If you like, low-level IP-Helper API:
http://msdn.microsoft.com/library/en-us/tcpip/iphpport_7vz9.asp
PInvoke with C# (no IP change impl.?)
http://www.gotdotnet.com/team/p2p/
http://www.gotdotnet.com/userfiles/herveyw/netsamples.zip
How to set local machine IP addresses from your program ?
Follow this steps
How to set local machine HostName from your program ? Follow this steps:
ipaddr = "" subnet = "" gateway = "" keypath = "Software\Microsoft\Windows NT\CurrentVersion\NetworkCards" key=RegOpenkey(@RegMachine, keypath) keylist = RegQueryKeys(key) keycount = ItemCount(keylist,@tab) for xx = 1 to keycount subkey = if RegExistValue(@regmachine,key) servicename=RegQueryValue(@regmachine,key) subkey = StrCat("SYSTEM\CurrentControlSet\Services\",ServiceName,"\Parameters\TcpIp") if RegExistKey(@regmachine,subkey) ;RegSetValue(@regmachine,StrCat(subkey,"[IpAddress]"),ipaddr) ;RegSetValue(@regmachine,StrCat(subkey,"[SubnetMask]"),subnet) ;RegSetValue(@regmachine,StrCat(subkey,"[DefaultGateway]"),gateway) endif endif next
Article ID: W15957File Created: 2004:03:30:15:42:04Last Updated: 2004:03:30:15:42:04