Results 1 to 12 of 12

Thread: [RESOLVED] Definition Confusion (Network Interface Card Enable\Disable)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    [RESOLVED] Definition Confusion (Network Interface Card Enable\Disable)

    Could someone help me convert this to VB6 please? I have tried to do it my self but I keep coming unstuck with the shell32 definitions. I have posted the original code just so none of my changes cause confusion and it would be nice if a fresh (and more experienced) pair of eyes could have a look at this.

    Code:
    ' Switch Boolean (Scope: form or module level) 
    Dim LANEnable As Boolean = True 
     
            ' ***** Either placed within a button or routine ***** 
     
            ' Control Panel Identifier 
            Const ssfCONTROLS = 3 
     
            ' Enter the name of the connection to manipulate 
            Dim ConnectionName As String = "Local Area Connection" 
            Dim EnableVerb As String = "En&able" 
            Dim DisableVerb As String = "Disa&ble" 
     
            ' Generate Shell item 
            Dim ShellApp As New Shell32.Shell() 
            ' Obtain the CP 
            Dim ControlPanel As Shell32.Folder = ShellApp.NameSpace(ssfCONTROLS) 
            Dim FolderItem As Shell32.FolderItem 
            Dim NetworkFolder As Shell32.Folder 
            Dim LANConnection As Shell32.FolderItem 
     
            ' Loop through the items in the control panel and obtain the Network Connections folder 
            For Each FolderItem In ControlPanel.Items() 
                Debug.WriteLine("Loop 1: " & FolderItem.Name) 
                If FolderItem.Name = "Network Connections" Then 
                    ' When found - exit the loop 
                    NetworkFolder = FolderItem.GetFolder 
                    Exit For 
                End If 
            Next 
     
            ' Debug check 
            If NetworkFolder Is Nothing Then 
                MessageBox.Show("Error - No network folder found") 
                Exit Sub 
            End If 
     
            ' Obtain the appropriate connection record 
            For Each FolderItem In NetworkFolder.Items() 
                Debug.WriteLine("Loop 2: " & FolderItem.Name) 
                If FolderItem.Name = ConnectionName Then 
                    ' When found - exit the loop 
                    LANConnection = FolderItem 
                    Exit For 
                End If 
            Next 
     
            ' Debug check 
            If LANConnection Is Nothing Then 
                MessageBox.Show("Error - No LAN entry was not found") 
                Exit Sub 
            End If 
     
            ' Swtich the LAN toggle 
            LANEnable = Not LANEnable 
     
            Dim EnableVerbItem, DisableVerbItem, Verb As Shell32.FolderItemVerb 
     
            ' Run through all available options and obtain the appropriate action 
            For Each Verb In LANConnection.Verbs 
                Debug.WriteLine("Loop 3: " & Verb.Name) 
                If Verb.Name = EnableVerb Then 
                    EnableVerbItem = Verb 
                End If 
                If Verb.Name = DisableVerb Then 
                    DisableVerbItem = Verb 
                End If 
            Next 
     
            ' Perform the enable / disable 
            If LANEnable Then 
                EnableVerbItem.DoIt() 
            Else 
                DisableVerbItem.DoIt() 
            End If
    Last edited by mik706; Apr 26th, 2007 at 05:23 AM.
    Mik706

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: definition confusion

    Does this help?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Re: definition confusion

    i am not really sure which language it is. i found it whilst searching through this forum

    http://www.vbforums.com/showthread.p...ol+panel+items
    Last edited by mik706; Apr 24th, 2007 at 09:50 AM.
    Mik706

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: definition confusion

    What is it that you are trying to do?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Re: definition confusion

    I am trying to find a way to programatically enable\disable the network cards installed on my PC. I have Tried WMI but unless i am using DHCP and or Vista that doesnt work. so i am now trying to replicate the manual procedure.
    Mik706

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Re: definition confusion

    i have just been looking for some more info on this code and it appears to have been created in VB.net.

    you need to reference the "Microsoft Shell Controls And Automation" object library

    I have modified it and the code i now have is:
    Code:
    Private Sub Command1_Click()
        Call NIC_Control("Local Area Connection", False)
    End Sub
    
    Public Sub NIC_Control(ConnectionName As String, LANEnable As Boolean)
        ' Enter the name of the connection to manipulate
        Dim EnableVerb As String
        Dim DisableVerb As String
        Dim EnableVerbItem, DisableVerbItem, Verb As Shell32.FolderItemVerb
        ' Generate Shell item
        Dim ShellApp As New Shell32.Shell
        ' Obtain the CP
        Dim ControlPanel As Shell32.Folder
        Dim FolderItem As Shell32.FolderItem
        Dim NetworkFolder As Shell32.Folder
        Dim LANConnection As Shell32.FolderItem
        
        ' Control Panel Identifier
        Const ssfCONTROLS = 3
        
        EnableVerb = "En&able"
        DisableVerb = "Disa&ble"
        
        Set ControlPanel = ShellApp.NameSpace(ssfCONTROLS)
        
        ' Loop through the items in the control panel and obtain the Network Connections folder
        For Each FolderItem In ControlPanel.Items()
            Debug.Print ("Loop 1: " & FolderItem.Name)
            If FolderItem.Name = "Network Connections" Then
                ' When found - exit the loop
                Set NetworkFolder = FolderItem.GetFolder
                Exit For
            End If
        Next
        
        ' Debug check
        If NetworkFolder Is Nothing Then
            MessageBox.Show ("Error - No network folder found")
            Exit Sub
        End If
        
        ' Obtain the appropriate connection record
        For Each FolderItem In NetworkFolder.Items()
            Debug.Print ("Loop 2: " & FolderItem.Name)
            If FolderItem.Name = ConnectionName Then
                ' When found - exit the loop
                Set LANConnection = FolderItem
                Exit For
            End If
        Next
        
        ' Debug check
        If LANConnection Is Nothing Then
            Debug.Print ("Error - No LAN entry was not found")
            Exit Sub
        End If
        
        ' Run through all available options and obtain the appropriate action
        For Each Verb In LANConnection.Verbs
            Debug.Print ("Loop 3: " & Verb.Name)
            If Verb.Name = EnableVerb Then
                EnableVerbItem = Verb
            End If
            If Verb.Name = DisableVerb Then
                DisableVerbItem = Verb
            End If
        Next
        
        ' Perform the enable / disable
        If LANEnable = True Then
            If EnableVerbItem <> "" Then
                EnableVerbItem.DoIt
            Else
                Debug.Print ("Already Enabled")
            End If
        Else
            If DisableVerbItem <> "" Then
                DisableVerbItem.DoIt
            Else
                Debug.Print ("Already Disabled")
            End If
        End If
    End Sub
    I still get an error when i try to execute the code though:

    Code:
    EnableVerbItem.DoIt
    or

    Code:
    DisableVerbItem.DoIt
    the error is:

    Runtime error '424' object required


    Any ideas???
    Mik706

  7. #7
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: definition confusion

    Quote Originally Posted by mik706
    i have just been looking for some more info on this code and it appears to have been created in VB.net.

    you need to reference the "Microsoft Shell Controls And Automation" object library

    I have modified it and the code i now have is:
    Code:
    Private Sub Command1_Click()
        Call NIC_Control("Local Area Connection", False)
    End Sub
    
    Public Sub NIC_Control(ConnectionName As String, LANEnable As Boolean)
        ' Enter the name of the connection to manipulate
        Dim EnableVerb As String
        Dim DisableVerb As String
        Dim EnableVerbItem, DisableVerbItem, Verb As Shell32.FolderItemVerb
        ' Generate Shell item
        Dim ShellApp As New Shell32.Shell
        ' Obtain the CP
        Dim ControlPanel As Shell32.Folder
        Dim FolderItem As Shell32.FolderItem
        Dim NetworkFolder As Shell32.Folder
        Dim LANConnection As Shell32.FolderItem
        
        ' Control Panel Identifier
        Const ssfCONTROLS = 3
        
        EnableVerb = "En&able"
        DisableVerb = "Disa&ble"
        
        Set ControlPanel = ShellApp.NameSpace(ssfCONTROLS)
        
        ' Loop through the items in the control panel and obtain the Network Connections folder
        For Each FolderItem In ControlPanel.Items()
            Debug.Print ("Loop 1: " & FolderItem.Name)
            If FolderItem.Name = "Network Connections" Then
                ' When found - exit the loop
                Set NetworkFolder = FolderItem.GetFolder
                Exit For
            End If
        Next
        
        ' Debug check
        If NetworkFolder Is Nothing Then
            MessageBox.Show ("Error - No network folder found")
            Exit Sub
        End If
        
        ' Obtain the appropriate connection record
        For Each FolderItem In NetworkFolder.Items()
            Debug.Print ("Loop 2: " & FolderItem.Name)
            If FolderItem.Name = ConnectionName Then
                ' When found - exit the loop
                Set LANConnection = FolderItem
                Exit For
            End If
        Next
        
        ' Debug check
        If LANConnection Is Nothing Then
            Debug.Print ("Error - No LAN entry was not found")
            Exit Sub
        End If
        
        ' Run through all available options and obtain the appropriate action
        For Each Verb In LANConnection.Verbs
            Debug.Print ("Loop 3: " & Verb.Name)
            If Verb.Name = EnableVerb Then
                EnableVerbItem = Verb
            End If
            If Verb.Name = DisableVerb Then
                DisableVerbItem = Verb
            End If
        Next
        
        ' Perform the enable / disable
        If LANEnable = True Then
            If EnableVerbItem <> "" Then
                EnableVerbItem.DoIt
            Else
                Debug.Print ("Already Enabled")
            End If
        Else
            If DisableVerbItem <> "" Then
                DisableVerbItem.DoIt
            Else
                Debug.Print ("Already Disabled")
            End If
        End If
    End Sub
    I still get an error when i try to execute the code though:

    Code:
    EnableVerbItem.DoIt
    or

    Code:
    DisableVerbItem.DoIt
    the error is:

    Runtime error '424' object required


    Any ideas???
    Do you actually have a program that needs control of your NIC's or do you just want to be able to click in Icon on your desktop to enable/disable them? If the latter, a simple batch file can do it.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Re: definition confusion

    I have a program which needs to be able to enable\disable the network cards
    Mik706

  9. #9
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: definition confusion

    Quote Originally Posted by mik706
    I have a program which needs to be able to enable\disable the network cards
    You could still do it by having the program run the batch files. Would that work?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Re: definition confusion

    I am effectively using a batch file to do the changeover at the moment I would just like to bring the functionality under my control i.e. perform the operation programmatically
    Mik706

  11. #11
    Lively Member
    Join Date
    Feb 2007
    Posts
    99

    Re: definition confusion

    Quote Originally Posted by mik706
    I am effectively using a batch file to do the changeover at the moment I would just like to bring the functionality under my control i.e. perform the operation programmatically
    Use the program to run the batch file by using the "Shell" statement.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    UK
    Posts
    147

    Re: definition confusion

    Just to give closure to this thread and provide my working code to anyone who needs it:

    Code:
    Public Sub NIC_Control(ConnectionName As String, LANEnable As Boolean)
        Dim EnableVerb As String
        Dim DisableVerb As String
        Dim ShellApp As Shell32.Shell
        Set ShellApp = New Shell32.Shell
        
        Dim ControlPanel As Shell32.Folder
        Set ControlPanel = ShellApp.NameSpace(ssfCONTROLS)
         
        Dim FolderItem As Shell32.FolderItem
        Dim NetworkFolder As Shell32.Folder
        Dim LANConnection As Shell32.FolderItem
        
    On Error GoTo ErrorHandler
        
        DisableVerb = "Disa&ble"
        EnableVerb = "En&able"
        
        ' Loop through the items in the control panel and obtain the Network Connections folder
        For Each FolderItem In ControlPanel.Items()
            Debug.Print ("Loop 1: " & FolderItem.Name)
            If InStr(1, FolderItem.Name, "Network", vbTextCompare) <> 0 Then
                ' When found - exit the loop
                Set NetworkFolder = FolderItem.GetFolder
                Exit For
            End If
        Next
        
        If NetworkFolder Is Nothing Then
            Exit Sub
        End If
        
        ' Obtain the appropriate connection record
        For Each FolderItem In NetworkFolder.Items()
            Debug.Print ("Loop 2: " & FolderItem.Name)
            If UCase(FolderItem.Name) = UCase(ConnectionName) Then
                ' When found - exit the loop
                Set LANConnection = FolderItem
                Exit For
            End If
        Next
         
        If LANConnection Is Nothing Then
            Exit Sub
        End If
        
        Dim EnableVerbItem, DisableVerbItem, Verb As Shell32.FolderItemVerb
        
        ' Run through all available options and obtain the appropriate action
        For Each Verb In LANConnection.Verbs
            Debug.Print ("Loop 3: " & Verb.Name)
            If Verb.Name = EnableVerb Then
                Set EnableVerbItem = Verb
            End If
            If Verb.Name = DisableVerb Then
                Set DisableVerbItem = Verb
            End If
        Next
        
        On Error Resume Next
        ' Perform the enable / disable
        If LANEnable Then
            If EnableVerbItem Is Nothing Then
                Debug.Print ("Card Already Enabled")
            Else
                EnableVerbItem.DoIt
                Sleep (2000)
            End If
        Else
            If DisableVerbItem Is Nothing Then
                Debug.Print ("Card Already Disabled")
            Else
                DisableVerbItem.DoIt
                Sleep (2000)
            End If
        End If
    
        Exit Sub
    ErrorHandler:
        LogString = Time & " " & Date & " Error in NIC_Control: " & Err.Number & ":" & Err.Description & Chr(13)
        Call LogHandler(LogString)
    End Sub
    Thanks for all the help! i hope i will be able to return the favour sometime.
    Last edited by mik706; Apr 26th, 2007 at 04:58 AM.
    Mik706

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width