VB:如何启用/禁用本地连接

        其实没什么说的,无非就是先找到“网络连接”这个虚拟文件夹,然后找到要控制的本地连接对应的folderitem,然后枚举verb,找到需要的verb后,调用verb的DoIt方法,在winxp sp2 ,vb6 sp6下测试通过,代码如下:
Option Explicit
'首先引用Microsoft Shell Controls And Automation
Private Function ExcNetLinkMenu(ByVal AdapterName As String, ByVal MenuName As String) As Boolean
    On Error Resume Next
    Dim mShell As New Shell32.Shell
    Dim NetConnection As Shell32.Folder
    Dim FolderItem As Shell32.FolderItem
    Dim NetConnectionItem As ShellFolderItem
    Dim verb As Shell32.FolderItemVerb
    Set NetConnection = mShell.NameSpace(49) '这个49是我找出来的,有了它就可以避免遍历控制面板
    If ObjPtr(NetConnection) = 0 Then
        ExcNetLinkMenu = False
        GoTo exitfunction
    End If
    Dim flag As Boolean
    flag = False
    For Each FolderItem In NetConnection.Items
        If FolderItem.Name = AdapterName Then
            Set NetConnectionItem = FolderItem
            flag = True
            Exit For
        End If
    Next
    If flag = False Then
        ExcNetLinkMenu = False
        GoTo exitfunction
    End If
    For Each verb In NetConnectionItem.Verbs
        If verb.Name = MenuName Then
            flag = True
            verb.DoIt
            ExcNetLinkMenu = True
            GoTo exitfunction
         End If
    Next
    If flag = False Then
        ExcNetLinkMenu = False
        GoTo exitfunction
    End If
exitfunction:
    Set mShell = Nothing
    Set NetConnection = Nothing
    Set FolderItem = Nothing
    Set NetConnectionItem = Nothing
    Set verb = Nothing
End Function

Private Sub Command1_Click()
        Dim flag As Boolean
        '把 本地连接 2 换成你要控制的本地连接的名字
        flag = ExcNetLinkMenu("本地连接 2", "停用(&B)") '这个在2000下对应的是禁用,具体是什么,点右键,自己看吧
End Sub

Private Sub Command2_Click()
    '把 本地连接 2 换成你要控制的本地连接的名字
    Dim flag As Boolean
    flag = ExcNetLinkMenu("本地连接 2", "启用(&A)")
End Sub

转载于:https://www.cnblogs.com/rainstormmaster/archive/2006/02/11/328943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值