Changing NIC IP and Gateway

Posted on 2004-11-09
Last Modified: 2011-04-14
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,
Question by:aboka
    1 Comment
    LVL 76

    Accepted Solution

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now