学习VB.NET时,你可能会遇到VB.NET网络问题,这里将总结一个VB.NET网络技巧的案例,在这里拿出来和大家分享一下。
VB.NET网络技巧案例:启动拨号网络中的连接
由于拨号网络不是一个可执行文件, 所以要启动拨号网络,需要借助 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
以上就是总结的VB.NET网络技巧,在机器上跑试一下。