• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Changing NIC IP and Gateway

Hi! I have read a QA here(http://www.experts-exchange.com/Networking/WinNT_Networking/Q_20719720.html) but just can't find ways to successfully make them work for my VB6 - Change the Gateway without rebooting the PC

I am wondering if anyone could make it work for me. FYI - I am using XP Home/Pro

p/s - the QA only mention about Gateway, it would be great if we could make it change the IP address as well

thank you,
1 Solution
David LeeCommented:
Hi, Aboka.

The code posted in the QA you cited works fine in VB for me.  I made three changes, neither of which should have anything to do with whether the code works or not.  The first change I made was to change both instances of Wscript.Echo to Debug.Print.  The second change I made was to restrict the change to a single NIC rather than changing the settings of all NICs in the system.  I found the index number of one NIC and used it to restrict the change to that NIC.  You don't have to do that if you want to change all the NICs.  Last of all, I changed the message being displayed if changing the address fails to show the error number.  The code is below.  Try it out and post the error number if it doesn't work.

Hope this helps.

Private Sub Command1_Click()
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE And Index=25")
    strIPAddress = Array("")
    strSubnetMask = Array("")
    strGateway = Array("")
    strGatewayMetric = Array(1)
    For Each objNetAdapter In colNetAdapters
        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
        errGateways = objNetAdapter.SetGateways(strGateway, strGatewayMetric)
        If errEnable = 0 Then
            Debug.Print "The IP address has been changed."
            Debug.Print "The IP address could not be changed." & vbCrLf & "Error #" & errEnable
        End If
End Sub

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now