Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting IP address with code to dynamic

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
0
0
AU
Way back in 08 I received good advice from this forum on how to set IP addresses in code using WMI

This works well setting a static address but I can't see how to easily set the computer back to a dynamic condition.

Once a static address has been set, IPConfig/release in the command window brings up an error "The operation failed as no adapter is in the state permissible for this operation.
It only sets IPs to 0.0.0.0 without error if the computer is already in dynamic. (using XP)

Rather than use a shell anyway, is there a way to set back to dynamic using WMI?
 
If you are actually using WMI illustrated in thread222-1454008 - given more recent reports from you seem to suggest that this is not the case (for example your own posts from March 2008 and Jan 2012) - then the EnableDHCP method would seem to be what you are looking for.
 
I moved on from that and used the following -

Code:
Sub ChangeIP (NewAddress as String, NewSubnet as String, NewGateway as String)
Dim objWMIService, colNetAdapters, objNetAdapter
Dim strIPAddress, strSubnetMask, strGateway, strGatewaymetric
Dim errEnable, errGateways
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration " & "where IPEnabled=TRUE")
       
strIPAddress = Array(NewAddress)
strSubnetMask = Array(NewSubnet)
strGateway = Array(NewGateway)
strGatewaymetric = Array(1)
 
For Each objNetAdapter In colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
Next
End Sub

Private Sub Command2_Click()

Which works very well in XP.

It would be nice if I could use similar methods to return it to dynamic.

 
In which case, as I suggested, EnableDHCP should do the trick

 
Thanks. It worked the first time but didnt seem to work when I set it to static with the above routine and then back to dynamic again. I have to investigate further.
 
You might want to issue RenewDHCPlease after switching the NIC to DHCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top