Changing NIC IP and Gateway

Hi! I have read a QA here( 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,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.