VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 启动拨号网络中的连接
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:9388, 日期:2001-06-15



  启动拨号网络中的连接

  由于拨号网络不是一个可执行文件, 所以要启动拨号网络,需要借助 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