Click here to Skip to main content
15,905,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have this code to enable and disable internet access:

Private Sub Button1_Click() Handles Button1.Click
    Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Dim colAdapters = objWMIService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled=True")
        For Each adapter In colAdapters
        MsgBox(adapter.DeviceId & "  " & adapter.Name)
        Next
        Dim errReturn = colAdapters.ItemIndex(0).Enable()
        If errReturn <> 0 Then
        MsgBox("Enable Network adapter failed for adapter= " & colAdapters.ItemIndex(0).DeviceId)
        Else
        MsgBox("Enable Network adapter succeeded for adapter= " & colAdapters.ItemIndex(0).DeviceId)
        End If
    MsgBox("NetEnabled= " & colAdapters.ItemIndex(0).NetEnabled)
End Sub
Private Sub Button2_Click() Handles Button2.Click
    Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Dim colAdapters = objWMIService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled=True")
    For Each adapter In colAdapters
        MsgBox(adapter.DeviceId & "  " & adapter.Name & vbCrLf)
    Next
    Dim errReturn = colAdapters.ItemIndex(0).Disable()
    If errReturn <> 0 Then
        msgbox("Enable Network adapter failed for adapter= " & colAdapters.ItemIndex(0).DeviceId & vbCrLf)
    Else
        MsgBox("Enable Network adapter succeeded for adapter= " & colAdapters.ItemIndex(0).DeviceId & vbCrLf)
    End If
    MsgBox("NetEnabled= " & colAdapters.ItemIndex(0).NetEnabled & vbCrLf)
End Sub

Things you may want to know:

* When I click button2, my internet gets disconnected, so that's a success.
* But when I click button1, my internet will not reconnect and gives an error on the line:
Dim errReturn = colAdapters.ItemIndex(0).Enable()
* My IDE highlights the Enable() as being the error and it says "Invalid parameter".
* I have to run this program as an administrator as nothing works otherwise

Does anyone know how to fix this?
Posted
Comments
Richard C Bishop 12-Nov-13 14:16pm    
If you set "Enabled" to false and disconnect the internet connection, wouldn't your query in the button2_click want to query search for "false" or "disabled" connections?

1 solution

Have you seen that you are using the same query condition in both cases?
You should try to enable only what is not enabled ("...Where NetEnabled=False"). See the NetEnabled property here: http://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx[^]. I know that you have used the code from MSDN (http://msdn.microsoft.com/en-us/library/aa390385(v=vs.85).aspx[^]), but I think it is wrong.
Be aware, that the system has several adapters. Use WmiExplorer[^] for example to check all adapter status. You should store somewhere the id of the device you disable to know which to enable.
 
Share this answer
 
Comments
[no name] 12-Nov-13 16:17pm    
I used the lower link and made this, but it doesn't even disable internet now. Therefore, i don't know whether it enables or not either :O

Private Sub Button1_Click() Handles Button1.Click
Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim colAdapters = objWMIService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled=True")
For i As Integer = 0 To colAdapters.count - 1 Step +1
colAdapters.ItemIndex(i).Enable()
Next
MsgBox("NetEnabled= " & colAdapters.ItemIndex(0).NetEnabled)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim colAdapters = objWMIService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled=False")
For i As Integer = 0 To colAdapters.count - 1 Step +1
colAdapters.ItemIndex(i).Disable()
Next
MsgBox("NetEnabled= " & colAdapters.ItemIndex(0).NetEnabled)
End Sub
Zoltán Zörgő 12-Nov-13 16:51pm    
You can/should disable what is enabled, and enable what is disabled. Swap the conditions. But don't be blind: why do you think that always the connection with index 0 is the right one?
[no name] 13-Nov-13 12:24pm    
i didn't- that's why i put in the For loop to go through all adapters :)

but changing the conditions worked perfectly, thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900