Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to disable the NIC?

How to disable the NIC?

Scheduled Pinned Locked Moved Visual Basic
sysadmintutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    younghand
    wrote on last edited by
    #1

    I mean to disable the NIC(Network Interface Card) by running a VB program. And then enabling the card. This will make the changes of parameters in network take effect without reboot the system. I want to get it work through a program. Thanks for reading and replying.:omg:

    D 1 Reply Last reply
    0
    • Y younghand

      I mean to disable the NIC(Network Interface Card) by running a VB program. And then enabling the card. This will make the changes of parameters in network take effect without reboot the system. I want to get it work through a program. Thanks for reading and replying.:omg:

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Hi, That is best done thru WMI (Windows Management Instrumentation). But, not all changes made will be applied when you disable and then re-enable the card. You'll know this when you call a function to make a change and the return code is something like REBOOT_REQUIRED. You can start with the System.Management and System.Management.Instrumentation namespace in VB.NET or the WMI SDK on MSDN[^]. RageInTheMachine9532

      V 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Hi, That is best done thru WMI (Windows Management Instrumentation). But, not all changes made will be applied when you disable and then re-enable the card. You'll know this when you call a function to make a change and the return code is something like REBOOT_REQUIRED. You can start with the System.Management and System.Management.Instrumentation namespace in VB.NET or the WMI SDK on MSDN[^]. RageInTheMachine9532

        V Offline
        V Offline
        vbTom
        wrote on last edited by
        #3

        Hi I have been trying to find a way to do this thru WMI but do not see any methods and all properties are read only. Can you point me in the right direction I am using Win32_NetworkAdapter I searched others and am unable to find a way to do it. Any help is appreciated. Thanks Tom

        D 1 Reply Last reply
        0
        • V vbTom

          Hi I have been trying to find a way to do this thru WMI but do not see any methods and all properties are read only. Can you point me in the right direction I am using Win32_NetworkAdapter I searched others and am unable to find a way to do it. Any help is appreciated. Thanks Tom

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Unfortunately, the WMI functionality that is exposed doesn't work. The idea is to get the ServiceName from the NetworkAdapter object. Then use the StopService method of the ServiceName object to stop the driver. But, alas, the driver won't stop or pause. Apparently it can't be done through WMI. But it could be done thru calls to the PnP Device Manager. You would have to call IoGetDeviceInterfaces with the class GUID of the network adapter class to get a linked list of SymbolicLinks. Once you find the Symbolic Link name, you can pass that to IoSetDeviceInterfaceState with the Enabled param set to False. I haven't tried to do this, and frankly, don't have to the time to write any code for it. But after about an hour of research, this is the only method I can come up with to disable/enable a NIC through code. I THINK it's the same method used by the Device Manager when you disable/enable a device through there. RageInTheMachine9532

          V 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Unfortunately, the WMI functionality that is exposed doesn't work. The idea is to get the ServiceName from the NetworkAdapter object. Then use the StopService method of the ServiceName object to stop the driver. But, alas, the driver won't stop or pause. Apparently it can't be done through WMI. But it could be done thru calls to the PnP Device Manager. You would have to call IoGetDeviceInterfaces with the class GUID of the network adapter class to get a linked list of SymbolicLinks. Once you find the Symbolic Link name, you can pass that to IoSetDeviceInterfaceState with the Enabled param set to False. I haven't tried to do this, and frankly, don't have to the time to write any code for it. But after about an hour of research, this is the only method I can come up with to disable/enable a NIC through code. I THINK it's the same method used by the Device Manager when you disable/enable a device through there. RageInTheMachine9532

            V Offline
            V Offline
            vbTom
            wrote on last edited by
            #5

            Thanks for the quick response. I will look into the solution you mentioned. Also I found this KB http://support.microsoft.com/?kbid=311272 it is a command line utility of the device manager which I may use as a last resort. Once again thanks. Tom

            D 1 Reply Last reply
            0
            • V vbTom

              Thanks for the quick response. I will look into the solution you mentioned. Also I found this KB http://support.microsoft.com/?kbid=311272 it is a command line utility of the device manager which I may use as a last resort. Once again thanks. Tom

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              DevCon is a pretty neat little tool. The source code for it can be found in the Windows XP DDK. You can find how to disable and enable devices in there. Although it's written in C++, you can use the technique in a VB.NET app. But it will take a bit of effort to get the structures defined properly and P/Invoke the calls required. RageInTheMachine9532

              V 1 Reply Last reply
              0
              • D Dave Kreskowiak

                DevCon is a pretty neat little tool. The source code for it can be found in the Windows XP DDK. You can find how to disable and enable devices in there. Although it's written in C++, you can use the technique in a VB.NET app. But it will take a bit of effort to get the structures defined properly and P/Invoke the calls required. RageInTheMachine9532

                V Offline
                V Offline
                vbTom
                wrote on last edited by
                #7

                Thanks I order the XP DDK on CD to take a look and possibly generate a better solution than using the Devcon.exe program. In the meantime I did get this to work, its not complete but it seems to work. Of course it relies on WMI and the Devcon executable. Thanks for your help Tom Private Sub ChangeAdapterStatus(ByVal mAdapter As String, ByVal mStatus As String) Dim oMngt As New ManagementScope("\\.") ' local machine Dim oMs As New System.Management.ObjectQuery Dim oSearch As New ManagementObjectSearcher Dim oColl As ManagementObjectCollection Dim oRcd As New ManagementObject Dim oNic As PropertyData Dim oState As String Dim ret As Integer ' Try oMs.QueryString() = "SELECT NetConnectionID, NetConnectionStatus, PNPDeviceID" & _ " FROM Win32_NetworkAdapter" & _ " Where NetConnectionID = '" & mAdapter & "'" ' " Where NetConnectionID like '%'" This returns all configed adpt oSearch.Scope = oMngt oSearch.Query = oMs oColl = oSearch.Get For Each oRcd In oColl oNic = oRcd.Properties.Item("PNPDeviceID") Dim oDevHld As String = oNic.Value Dim oDevID() As String = oDevHld.Split("&") Dim oCmd As String = "\devcon.exe " & mStatus & " *" & oDevID(1) & "* updateni" ret = Shell(Application.StartupPath & oCmd) Next Catch ex As ManagementException MsgBox(ex.Message) End Try End Sub Tom

                D 1 Reply Last reply
                0
                • V vbTom

                  Thanks I order the XP DDK on CD to take a look and possibly generate a better solution than using the Devcon.exe program. In the meantime I did get this to work, its not complete but it seems to work. Of course it relies on WMI and the Devcon executable. Thanks for your help Tom Private Sub ChangeAdapterStatus(ByVal mAdapter As String, ByVal mStatus As String) Dim oMngt As New ManagementScope("\\.") ' local machine Dim oMs As New System.Management.ObjectQuery Dim oSearch As New ManagementObjectSearcher Dim oColl As ManagementObjectCollection Dim oRcd As New ManagementObject Dim oNic As PropertyData Dim oState As String Dim ret As Integer ' Try oMs.QueryString() = "SELECT NetConnectionID, NetConnectionStatus, PNPDeviceID" & _ " FROM Win32_NetworkAdapter" & _ " Where NetConnectionID = '" & mAdapter & "'" ' " Where NetConnectionID like '%'" This returns all configed adpt oSearch.Scope = oMngt oSearch.Query = oMs oColl = oSearch.Get For Each oRcd In oColl oNic = oRcd.Properties.Item("PNPDeviceID") Dim oDevHld As String = oNic.Value Dim oDevID() As String = oDevHld.Split("&") Dim oCmd As String = "\devcon.exe " & mStatus & " *" & oDevID(1) & "* updateni" ret = Shell(Application.StartupPath & oCmd) Next Catch ex As ManagementException MsgBox(ex.Message) End Try End Sub Tom

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  That'll work. The only down side is DevCon is NOT redistributable. Microsoft is a bit picky about sending developer tools out with licensed products. RageInTheMachine9532

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups