Visual Basic 6.0(以下简称VB 6)以其强大的功能为广大软件开发人员所喜爱,被用来开发各种应用程序。随着Internet的迅猛发展,VB6在网络方面的应用开发也越来越多。本文介绍笔者在实际编程开发中总结的5个技巧,希望能为进行网络开发的朋友们提供一点参考。
连通检测
许多应用程序常常需要在程序中直接进行联网操作,以便进行一些必要的处理(如在线注册和在线帮助等),这就要求我们在程序中建立某些连接。很多软件在不知用户是否联网的情况下就启动浏览器查找网址,结果只能查出一错误网页,既浪费用户时间又没有任何效果。如果应用程序在查找网页之前能自动判断用户是否已经联网,就会节约许多时间,提高程序运行效率。
下面是实现网络连通检测的VB 6代码:
Private Sub Form_Load()
If IsConnected = TRUE Then
MsgBox ("您已经连通了Internet!")
End If
If IsConnected = FALSE Then
MsgBox ("您还没有连通 Internet!")
End If
End Sub
Option Explicit
/*有关的API声明和定义*/
Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
Public Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
/*常数和变量的设定*/
Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 32
Public Type RASCONN95
dwSize As Long
hRasCon As Long
szEntryName(RAS95_MaxEntryName) As Byte
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
Public Type RASCONNSTATUS95
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
/*函数IsConnected返回连通的状态,如果为True则表示已连通*/
Public Function IsConnected() As Boolean
Dim TRasCon(255) As RASCONN95
Dim lg As Long
Dim lpcon As Long
Dim RetVal As Long
Dim Tstatus As RASCONNSTATUS95
TRasCon(0).dwSize = 412
lg = 256 * TRasCon(0).dwSize
RetVal = RasEnumConnections(TRasCon(0), lg, lpcon)
If RetVal <> 0 Then
MsgBox "错误"
Exit Function
End If
Tstatus.dwSize = 160
RetVal = RasGetConnectStatus(TRasCon(0).hRasCon,Tstatus)
If Tstatus.RasConnState = &H2000 Then
IsConnected = TRUE
Else
IsConnected = FALSE
End If
End Function