How to disable the NIC?
-
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:
-
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:
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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