DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Visual Basic 4 / 5 / 6 (http://www.daniweb.com/forums/forum4.html)
-   -   Network LAN Connection to be Enabled/Disabled (http://www.daniweb.com/forums/thread50255.html)

maheshsayani Jul 17th, 2006 1:20 am
Network LAN Connection to be Enabled/Disabled
 
Hi To All,

How can the Network LAN Connection to be Enabled/Disabled
through Vb Code

Comatose Jul 17th, 2006 8:06 am
Re: Network LAN Connection to be Enabled/Disabled
 
This code actually "toggles" the connection, by retrieving it's current state, and then setting the opposite state..... but, in vb6 you'll need to add a reference to "Microsoft Shell Controls and Automation" before it will work for you, plus, you may need to change some variables to fit the name of your connection....
Dim objCP, objEnable, objDisable, colNetwork
Dim clsConn, clsLANConn, clsVerb
Dim strNetConn, strConn, strEnable, strDisable
Dim bEnabled, bDisabled

strNetConn = "Network Connections"
strConn = "Local Area Connection"

strEnable = "En&able"
strDisable = "Disa&ble"

Set objshell = CreateObject("Shell.Application")

If objshell Is Nothing Then
    MsgBox "Fooey"
    Exit Sub
End If


Set objCP = objshell.NameSpace(3) 'Control Panel

Set colNetwork = Nothing
For Each clsConn In objCP.Items
    If clsConn.Name = strNetConn Then
        Set colNetwork = clsConn.GetFolder
        Exit For
    End If
Next

If colNetwork Is Nothing Then
    MsgBox "Network folder not found"
    Exit Sub
End If

Set clsLANConn = Nothing
For Each clsConn In colNetwork.Items
    If InStr(LCase(clsConn.Name), LCase(strConn)) Then
        Set clsLANConn = clsConn
        Exit For
    End If
Next

If clsLANConn Is Nothing Then
    MsgBox "Network Connection not found"
    Exit Sub
End If

bEnabled = True
Set objEnable = Nothing
Set objDisable = Nothing

For Each clsVerb In clsLANConn.Verbs
    If clsVerb.Name = strEnable Then
        Set objEnable = clsVerb
        bEnabled = False
    End If
    If clsVerb.Name = strDisable Then
        Set objDisable = clsVerb
    End If
Next

If bEnabled Then
    objDisable.DoIt
Else
    objEnable.DoIt
End If

maheshsayani Jul 17th, 2006 8:16 am
Re: Network LAN Connection to be Enabled/Disabled
 
Thanks For your help Mr.Comatose.


All times are GMT -4. The time now is 12:14 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC