Changing IP Address Programmatically

D

Don Quixote

Hello,
I use the code below to change the IP of a machine :
________________________________________________________________________
Private Sub ChangeIP(NewIP, NewMask)
Dim NetworkAdapter, AdapterConfiguration 'Objects
Dim IPAddress, SubnetMask, Gateway, DNS, strGatewayMetric 'String Arrays
Dim RetVal, x 'Integers
Dim NICname As String
Dim tmpIP As String
tmpIP = GetIPAddress()

Dim NetAdapterCFG As SWbemObjectSet
Dim naCFG As SWbemObject

NICname = ""
For x = 1 To lvwInterfaces.ListItems.Count
If lvwInterfaces.ListItems(x).Checked = True Then NICname =
lvwInterfaces.ListItems(x).SubItems(6)
Next x
If NICname = "" Then Exit Sub

Set NetAdapterCFG =
GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

For Each naCFG In NetAdapterCFG

If UCase(naCFG.ServiceName) = UCase(NICname) Then
IPAddress = Array(NewIP)
SubnetMask = Array(NewMask)
RetVal = naCFG.enableStatic(IPAddress, SubnetMask)

If Not RetVal = 0 Then
MsgBox "Failure assigning IP/Subnetmask." & vbCrLf &
"Possible duplicate IP address", vbCritical, "Warning"
IPAddress = Array(tmpIP)
RetVal = naCFG.enableStatic(IPAddress, SubnetMask)
msgBox "Failure assigning IP/Subnetmask."
Else
Debug.Print NewIP
End If
End If

Next

End Sub
__________________________________________________________________

It works fine and as far pinging goes or accessing the net, it works
alright. I do have problems accessing shares on the local network though.
Let's say my address is 192.168.10.80, and I can access other PCs on the
network, if I change thru the code above my IP to 192.168.10.81, I don't
access anymore the PCs. I can still ping them and I can go on the net. That
means my gateway and DNS still work, right ?
Needless to say, if I do the changes thru the Windows applet, everything
works fine.

Anybody has an idea of what is at play here ?

Thx a million.

Sal
 
J

Jim Carlock

The computer keeps a local cache of the DNS. Normally the way
to flush the local cache would be:

IPCONFIG /flushdns

on a win2k system or later.

Maybe someone knows how to get this done with an API call. The
following one seems to provide a means to flush the ARP cache.

'win2k or later
Declare Function FlushIpNetTable Lib "IPHLPAPI.dll" ( _
ByVal dwIfIndex As Long) As Long

Hope that helps.

--
Jim Carlock
Please post replies to newsgroup.

Hello,
I use the code below to change the IP of a machine :
________________________________________________________________________
Private Sub ChangeIP(NewIP, NewMask)
Dim NetworkAdapter, AdapterConfiguration 'Objects
Dim IPAddress, SubnetMask, Gateway, DNS, strGatewayMetric 'String Arrays
Dim RetVal, x 'Integers
Dim NICname As String
Dim tmpIP As String
tmpIP = GetIPAddress()

Dim NetAdapterCFG As SWbemObjectSet
Dim naCFG As SWbemObject

NICname = ""
For x = 1 To lvwInterfaces.ListItems.Count
If lvwInterfaces.ListItems(x).Checked = True Then NICname =
lvwInterfaces.ListItems(x).SubItems(6)
Next x
If NICname = "" Then Exit Sub

Set NetAdapterCFG =
GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

For Each naCFG In NetAdapterCFG

If UCase(naCFG.ServiceName) = UCase(NICname) Then
IPAddress = Array(NewIP)
SubnetMask = Array(NewMask)
RetVal = naCFG.enableStatic(IPAddress, SubnetMask)

If Not RetVal = 0 Then
MsgBox "Failure assigning IP/Subnetmask." & vbCrLf &
"Possible duplicate IP address", vbCritical, "Warning"
IPAddress = Array(tmpIP)
RetVal = naCFG.enableStatic(IPAddress, SubnetMask)
msgBox "Failure assigning IP/Subnetmask."
Else
Debug.Print NewIP
End If
End If

Next

End Sub
__________________________________________________________________

It works fine and as far pinging goes or accessing the net, it works
alright. I do have problems accessing shares on the local network though.
Let's say my address is 192.168.10.80, and I can access other PCs on the
network, if I change thru the code above my IP to 192.168.10.81, I don't
access anymore the PCs. I can still ping them and I can go on the net. That
means my gateway and DNS still work, right ?
Needless to say, if I do the changes thru the Windows applet, everything
works fine.

Anybody has an idea of what is at play here ?

Thx a million.

Sal
 
P

Pegasus \(MVP\)

Don Quixote said:
Hello,
I use the code below to change the IP of a machine :
________________________________________________________________________
Private Sub ChangeIP(NewIP, NewMask)
Dim NetworkAdapter, AdapterConfiguration 'Objects
Dim IPAddress, SubnetMask, Gateway, DNS, strGatewayMetric 'String Arrays
Dim RetVal, x 'Integers
Dim NICname As String
Dim tmpIP As String
tmpIP = GetIPAddress()

Dim NetAdapterCFG As SWbemObjectSet
Dim naCFG As SWbemObject

NICname = ""
For x = 1 To lvwInterfaces.ListItems.Count
If lvwInterfaces.ListItems(x).Checked = True Then NICname =
lvwInterfaces.ListItems(x).SubItems(6)
Next x
If NICname = "" Then Exit Sub

Set NetAdapterCFG =
GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")

For Each naCFG In NetAdapterCFG

If UCase(naCFG.ServiceName) = UCase(NICname) Then
IPAddress = Array(NewIP)
SubnetMask = Array(NewMask)
RetVal = naCFG.enableStatic(IPAddress, SubnetMask)

If Not RetVal = 0 Then
MsgBox "Failure assigning IP/Subnetmask." & vbCrLf &
"Possible duplicate IP address", vbCritical, "Warning"
IPAddress = Array(tmpIP)
RetVal = naCFG.enableStatic(IPAddress, SubnetMask)
msgBox "Failure assigning IP/Subnetmask."
Else
Debug.Print NewIP
End If
End If

Next

End Sub
__________________________________________________________________

It works fine and as far pinging goes or accessing the net, it works
alright. I do have problems accessing shares on the local network though.
Let's say my address is 192.168.10.80, and I can access other PCs on the
network, if I change thru the code above my IP to 192.168.10.81, I don't
access anymore the PCs. I can still ping them and I can go on the net. That
means my gateway and DNS still work, right ?
Needless to say, if I do the changes thru the Windows applet, everything
works fine.

Anybody has an idea of what is at play here ?

Thx a million.

Sal

You could do the same with a single line of code in a batch file:
netsh interface ip set address "Local Area Connection" static 125.187.7.56
255.0.0.0
It appears to take effect immediately.
 
D

Don Quixote

:
IPCONFIG /flushdns

I had tried that already, but no luck. Since disabling and re-enabling the
NIC seems to fix the problems, I would settle for a way to reset the network
card from code (although I think it might be a kludge). Did not find
anything yet


Thx
 
D

Don Quixote

Pegasus (MVP) said:
You could do the same with a single line of code in a batch file:
netsh interface ip set address "Local Area Connection" static 125.187.7.56
255.0.0.0
It appears to take effect immediately.

Thanks, but that was my way of doing it before, then I wanted to eliminate
the shelling out to an external command. Of course, if I do not find a
solution for my problem, I may be obliged to go back to the netsh method.

Sal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top