Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Const ssfCONTROLS = 3
sConnectionName = "我的连接"
sEnableVerb = "连接(&O)"
sDisableVerb = "断开(&O)"
Set shellApp = CreateObject("shell.application")
Set oControlPanel = shellApp.Namespace(ssfCONTROLS)
Set oNetConnections = Nothing
For Each folderitem In oControlPanel.items
If folderitem.Name = "网络和拨号连接" Then
Set oNetConnections = folderitem.getfolder: Exit For
End If
Next
If oNetConnections Is Nothing Then
wscript.Quit
End If
Set oLanConnection = Nothing
For Each folderitem In oNetConnections.items
If LCase(folderitem.Name) = LCase(sConnectionName) Then
Set oLanConnection = folderitem: Exit For
End If
Next
If oLanConnection Is Nothing Then
wscript.Quit
End If
bEnabled = True
Set oEnableVerb = Nothing
Set oDisableVerb = Nothing
s = "Verbs: " & vbCrLf
For Each Verb In oLanConnection.verbs
s = s & vbCrLf & Verb.Name
If Verb.Name = sEnableVerb Then
Set oEnableVerb = Verb
bEnabled = False
End If
If Verb.Name = sDisableVerb Then
Set oDisableVerb = Verb
End If
Next
If bEnabled Then
oDisableVerb.DoIt
Else
' oEnableVerb.DoIt
End If
Sleep 3000
Unload Me
End Sub
------解决方案--------------------
//代码在xp sp3+pb11.5下测试通过,pb9下的代码也是一样的,
//代码有些地方还可以精减的,楼主自己弄吧,测试效果不错,有问题找我
constant integer ssfCONTROLS = 3
string sConnectionName,ls_cmd,ls_name
string sEnableVerb,sDisableVerb
string vbCrLf=char(10) + char(13)
Boolean bEnabled
int li_rtn,li_i
oleobject shellApp,oControlPanel,mylist,mytmp,mynet,myExit,myVerb,myWork
sConnectionName = "宽带连接"
sEnableVerb = "连接(&O)"
sDisableVerb = "断开(&O)"
shellApp = Create oleobject
li_rtn = shellApp.connecttonewobject( "shell.application")
if li_rtn=0 then
//遍历所有控制面板项,得到网络连接对象
oControlPanel = shellApp.Namespace(ssfCONTROLS)
for li_i = 0 to oControlPanel.items.Count - 1
mylist = oControlPanel.items.Item[li_i]
if mylist.name='网络连接' then//和操作系统有关,我的XP是这个
mytmp = mylist.getfolder()
exit;
end if
next
end if
if isvalid(mytmp) then
mynet = mytmp.items
//遍历本地所有网络连接,得到要中断的连接
for li_i = 0 to mynet.count - 1
if Lower(string(mynet.item[li_i].name))=sConnectionName then
myExit = mynet.item[li_i]
exit;
end if
next
end if
//ls_cmd不需要定义,应该是作者用来测试的
ls_cmd = "Verbs: " + vbCrLf
if Isvalid(myExit) then
for li_i = 0 to myExit.verbs.count - 1
myVerb = myExit.verbs.item[li_i]
messagebox(string(li_i),string(myVerb.name))
ls_name = string(myVerb.name)
//根据当前网络状态判断是断开还是启用,实质是得到上面的控件,执行代码
if ls_name=sEnableVerb then
bEnabled = false
myWork = myVerb
exit
elseif ls_name= sDisableVerb then
bEnabled = true
myWork = myVerb
exit
end if
next
end if
if IsValid(myWork) then
myWork.DoIt()
messagebox('','正在停用/启用,可以看到效果的!')
Sleep(3000)//pb自带了sleep函数,可以不用了
end if
shellApp.disconnectobject( )
Destroy shellApp