VB 程序设计

10个成员

Visual Basic6.0网络编程的五大散手

发表于 2016-12-25 3472 次查看
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

启动拨号网络中的连接

   由于拨号网络不是一个可执行文件, 所以要启动拨号网络,需要借助 explorer.exe 。但若是要启动拨号网络中的某一个连接,则要借助rundll.exe 和 rnaui.dll两个文件。启动方法如下(假定此连接名称为163):

Shell "rundll rnaui.dll,RnaDial 163",vbNormalFocus

   上面假定了连接名称,但在实际编程中我们是不知道连接名称的。在窗体上放置一个命令按钮(cmdCallConnect),在其单击事件中进行连接处理。下面的代码介绍如何取得默认的连接名称并启动它:

Option Explicit
/*有关的API声明*/
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal szData As String, ByRef lpcbData As Long) As Long
  Private Declare Function RegCloseKey Lib "advapi32"(ByVal hKey As Long) As Long
  /*常数的设定*/
Const HKEY_CURRENT_USER = &H80000001
Const ERROR_SUCCESS = 0
在命令按钮(cmdCallConnect)中加入如下代码:
Private Sub cmdCallConnect_Click()
/*启动默认拨号连接*/
Shell "rundll rnaui.dll,RnaDial" + GetConnect, vbNormalFocus
End Sub

/*取得连接的函数(GetConnect)*/
Public Function GetConnect() As String
  Dim hKey As Long
  Dim SubKey As String
  /*主键*/
  hKey = HKEY_CURRENT_USER
  /*子键*/
  SubKey = "RemoteAccess"
  /*取得默认连接名*/
  GetConnect=GetRegValue(hKey,SubKey, "Default")
End Function

/*取得注册的函数(GetRegValue)*/
Public Function GetRegValue(hKey As Long,lpszSubKey As String,szKey As String) As Variant
  On Error GoTo ErrorRoutineErr:
  Dim phkResult As Long
  Dim lResult As Long
  Dim szBuffer As String
  Dim lBuffSize As Long
  /*创建缓冲区*/
  szBuffer = Space(255)
  lBuffSize = Len(szBuffer)
  /*打开注册键*/
  RegOpenKeyEx hKey, lpszSubKey, 0, 1,phkResult
  /*取得查询结果*/
  lResult = RegQueryValueEx(phkResult, szKey, 0, 0, szBuffer, lBuffSize)
  /*关闭注册键*/
  RegCloseKey phkResult
  /*返回结果*/
  If lResult = ERROR_SUCCESS Then
   GetRegValue = Left(szBuffer, lBuffSize - 1)
  Else
   GetRegValue =""
  End If
Exit Function

/*意外处理*/
ErrorRoutineErr:
GetRegValue =""
End Function


 

发表回复
你还没有登录,请先登录注册