我最近做了一个网络切换软件,在VB6.0里在调试时正常,生成EXE文件就不能运行了
请大家帮我看看是什么原因
Option Explicit
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)
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 FunctionPrivate Sub Command1_Click()
Dim flag As Boolean
flag = ExcNetLinkMenu("外网", "启用(&A)")
flag = ExcNetLinkMenu("内网", "停用(&B)")
Unload Form1
End SubPrivate Sub Command2_Click()
Dim flag As Boolean
flag = ExcNetLinkMenu("内网", "启用(&A)")
flag = ExcNetLinkMenu("外网", "停用(&B)")
Unload Form1
End SubPrivate Sub Form_Load()
If ExcNetLinkMenu("外网", "停用(&B)") = True Then
Label1.Caption = "当前网络处于外网中"
Image3.Picture = LoadPicture(App.Path & "\img\1.gif")
Command1.Visible = False
Command2.Visible = True
Else
If ExcNetLinkMenu("内网", "停用(&B)") = True Then
Label1.Caption = "当前网络处于内网中"
Image3.Picture = LoadPicture(App.Path & "\img\2.gif")
Command1.Visible = True
Command2.Visible = False
End If
End If
End SubPrivate Sub Label2_Click()
Unload Form1
End Sub
请大家帮我看看是什么原因
Option Explicit
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)
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 FunctionPrivate Sub Command1_Click()
Dim flag As Boolean
flag = ExcNetLinkMenu("外网", "启用(&A)")
flag = ExcNetLinkMenu("内网", "停用(&B)")
Unload Form1
End SubPrivate Sub Command2_Click()
Dim flag As Boolean
flag = ExcNetLinkMenu("内网", "启用(&A)")
flag = ExcNetLinkMenu("外网", "停用(&B)")
Unload Form1
End SubPrivate Sub Form_Load()
If ExcNetLinkMenu("外网", "停用(&B)") = True Then
Label1.Caption = "当前网络处于外网中"
Image3.Picture = LoadPicture(App.Path & "\img\1.gif")
Command1.Visible = False
Command2.Visible = True
Else
If ExcNetLinkMenu("内网", "停用(&B)") = True Then
Label1.Caption = "当前网络处于内网中"
Image3.Picture = LoadPicture(App.Path & "\img\2.gif")
Command1.Visible = True
Command2.Visible = False
End If
End If
End SubPrivate Sub Label2_Click()
Unload Form1
End Sub
还是拿到别的机器上不能运行的?
因为你的代码里面用到了Shell32.dll
因此若安装到别的机器上,最好带着该DLL一起部署
另外,我不同意楼上所说的带Shell32.dll部署的说法,
这个DLL在windows系统中是包括的。
Dim NetConnection As Shell32.Folder
Dim FolderItem As Shell32.FolderItem
Dim NetConnectionItem As ShellFolderItem
Dim verb As Shell32.FolderItemVerb