VB6.0开发网络应用的5个技巧

2008-02-23 06:38:54来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

1 2 下一页

  Visual Basic 6.0(以下简称VB 6)以其强大的功能为广大软件开发人员所喜爱,被用来开发各种应用程序。随着Internet的迅猛发展,VB 6在网络方面的应用开发也越来越多。本文介绍笔者在实际编程开发中总结的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:

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:VB6.0中Dir()函数的使用方法

下一篇:VB中使用WinSock控件编写网络程序