明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

使用RAS調(diào)用在VB6.0中完成撥號上網(wǎng)

[摘要]我們常用的軟件如:JETCAR(現(xiàn)在為FLASHGET),GO!ZILLA等都具有撥號上網(wǎng)功能。那么,如何在自己編寫的應(yīng)用程序中也實(shí)現(xiàn)上述的功能呢?本文就利用RAS調(diào)用介紹在自己編寫的應(yīng)用程序如何實(shí)...
我們常用的軟件如:JETCAR(現(xiàn)在為FLASHGET),GO!ZILLA等都具有撥號上網(wǎng)功能。那么,如何在自己編寫的應(yīng)用程序中也實(shí)現(xiàn)上述的功能呢?本文就利用RAS調(diào)用介紹在自己編寫的應(yīng)用程序如何實(shí)現(xiàn)撥號上網(wǎng)。

  一.與撥號網(wǎng)絡(luò)有關(guān)的結(jié)構(gòu)和函數(shù)

  1.RASDIALPARAMS95結(jié)構(gòu).其定義如下:

Public Type RASDIALPARAMS95
 dwSize As Long
 szEntryName(RAS95_MaxEntryName) As Byte
 szPhoneNumber(RAS95_MaxPhoneNumber) As Byte
 szCallbackNumber(RAS95_MaxCallbackNumber) As Byte
 szUserName(UNLEN) As Byte
 szPassword(PWLEN) As Byte
 szDomain(DNLEN) As Byte
End Type

  其中,dwSize表示該結(jié)構(gòu)的大小(以字節(jié)為單位)。szEntryName表示一個遠(yuǎn)程訪問的入口名,也就是WINDOWS下的撥號網(wǎng)絡(luò)中的連接圖標(biāo)小的文字;szEntryName參數(shù)可是是空串,表示僅與第一個有效的MODEM建立遠(yuǎn)程連接。szPhoneNumber為表示呼叫的號碼。當(dāng)szEntryName是空時,zPhoneNumber不能為空。szCallbackNumber表示回叫號碼,一般都設(shè)為空。szUserName為建立遠(yuǎn)程訪問的用戶名。szPassword為建立遠(yuǎn)程訪問的密碼。szDomain為進(jìn)行遠(yuǎn)程訪問用戶名和密碼認(rèn)證的域名。我們將szDomain設(shè)為空,表示遠(yuǎn)程訪問的服務(wù)器也是該域名下的成員。

  2.RASDIAL函數(shù).其API聲明為:

Public Declare Function RasDial Lib "RasApi32.DLL" Alias "RasDialA" (lpRasDialExtensions As Any, ByVal lpszPhonebook As String, lprasdialparams As Any, ByVal dwNotifierType As Long, lpvNotifier As Long, lphRasConn As Long) As Long

  RASDIAL函數(shù)用于建立RAS客戶機(jī)和RAS服務(wù)器的連接。

  各參數(shù)解釋如下:

   lpRasDialExtensions參數(shù)在WINDOWS95/98下被忽略。

   lpszPhonebook參數(shù)在WINDOWS95/98下也被忽略。

   lprasdialparams參數(shù)是指向一個含有RAS連接參數(shù)值的RASDIALPARAMS結(jié)構(gòu)變量。

  dwNotifierType參數(shù)用于指出參數(shù)lpvNotifier的性質(zhì)。若lpvNotifier 為NULL,則dwNotifierType的值被忽略;若lpvNotifier不為NULL,則dwNotifierType應(yīng)該為下列值之一:

 。1).0XFFFFFFFF------指出lpvNotifier參數(shù)是一個接收進(jìn)程通知消息的窗口句柄;

  (2).0----------------指出lpvNotifier指向一個RASDIALFUNC回調(diào)函數(shù);

 。3).1----------------指出lpvNotifier指向下一個RASDIALFUNC1回調(diào)函數(shù)。

  lpvNotifier參數(shù)用于指向一個接收RASDIAL事件通知的窗口或者回調(diào)函數(shù),其性質(zhì)由dwNotifierType參數(shù)決定:

 。1).若lpvNotifier不NULL,表示是異步通信。即RASDIAL會對每個事件發(fā)送一個窗口消息或者調(diào)用回調(diào)函數(shù)。RASDIAL函數(shù)會立即返回,它通過窗口或者調(diào)用回調(diào)函數(shù)建立連接,與進(jìn)程通信。

 。2).若lpvNotifier是NULL,表示同步通信。即RASDIAL函數(shù)在連接建立完成或者失敗后才會返回。

  lphRasConn參數(shù)指向HRASCONN變量,必須先將lphRasConn設(shè)置為NULL,然后再調(diào)用RASDIAL函數(shù)。如果RASDIAL函數(shù)調(diào)用成功,lphRasconn會放置一個RAS連接句柄。

  3.RASHANGUP函數(shù).其API聲明為:

Public Declare Function RasHangUp Lib "RasApi32.DLL" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long

  RASHANGUP函數(shù)用于斷開一個RAS連接。其參數(shù)hRasConn為指向一個RAS連接的句柄。

  二.用VB6.0實(shí)現(xiàn)RAS撥號上網(wǎng)過程。

  新建一個工程,取名為RasDial;然后在該工程中加入如下項目:

  1.新建一個窗體Form1,然后在該窗體中加入如下組件:





   ("連接"按鈕取名為Ras_Dial,"掛斷"按鈕取名為:Ras_HangUp,"退出"按鈕取名為:"Cancel".

   3個文本框分別取名為:PhoneNumber,UserName,PassWord.)
2.雙擊連接按鈕,加入如下代碼:

Private Sub Ras_Dial_Click()
Dim temp As Long
If PhoneNumber.Text = "" Or UserName.Text = "" Or PassWord.Text = "" Then
temp = MsgBox("您沒有輸入有效的撥號網(wǎng)絡(luò)參數(shù)。", vbOKOnly, "錯誤")
Exit Sub
End If
temp = AddConnection("", PhoneNumber.Text, "", UserName.Text, PassWord.Text, "")
Select Case temp
Case ERROR_PORT_ALREADY_OPEN: temp = MsgBox("錯誤,端口已經(jīng)打開!", vbOKOnly, "Error")
Case ERROR_UNKNOWN: temp = MsgBox("未知的錯誤!", vbOKOnly, "Error")
Case ERROR_REQUEST_TIMEOUT: temp = MsgBox("錯誤,請求超時!", vbOKOnly, "Error")
Case ERROR_PASSWD_EXPIRED: temp = MsgBox("錯誤,您沒有輸入密碼!", vbOKOnly, "Error")
Case ERROR_NO_DIALIN_PERMISSION: temp = MsgBox("錯誤,沒有撥號音!", vbOKOnly, "Error")
Case ERROR_SERVER_NOT_RESPONDING: temp = MsgBox("錯誤,撥入的遠(yuǎn)程計算機(jī)沒有響應(yīng)!", vbOKOnly, "Error")
Case ERROR_UNRECOGNIZED_RESPONSE: temp = MsgBox("錯誤,未知的響應(yīng)!", vbOKOnly, "Error")
Case ERROR_NO_RESPONSES: temp = MsgBox("錯誤,沒有響應(yīng)!", vbOKOnly, "Error")
Case ERROR_DEVICE_NOT_READY: temp = MsgBox("錯誤,設(shè)備沒有準(zhǔn)備好!", vbOKOnly, "Error")
Case ERROR_LINE_BUSY: temp = MsgBox("錯誤,占線!", vbOKOnly, "Error")
Case ERROR_NO_ANSWER: temp = MsgBox("錯誤,沒有應(yīng)答信號!", vbOKOnly, "Error")
Case ERROR_NO_CARRIER: temp = MsgBox("錯誤,沒有載波信號!", vbOKOnly, "Error")
Case ERROR_NO_DIALTONE: temp = MsgBox("錯誤,沒有撥號音!", vbOKOnly, "Error")
Case ERROR_AUTHENTICATION_FAILURE: temp = MsgBox("用戶名密碼出錯!", vbOKOnly, "Error")
Case ERROR_PPP_TIMEOUT: temp = MsgBox("PPP接入超時。", vbOKOnly, "Error")
End Select
End Sub
3.雙擊掛斷按鈕,加入如下代碼:
Private Sub Ras_HangUp_Click()
Dim temp As Long
temp = RasHangUp(hRasConn)
End Sub
4.雙擊退出按鈕,加入如下代碼:
Private Sub Cancel_Click()
Unload Me
End Sub
5.最后,新建一個模塊,取名為Ras_Dial,加入如下代碼:
Option Explicit
Public hRasConn As Long '定義一個指向RAS調(diào)用的全局句柄
Public Const APINULL = 0&
Public Const UNLEN = 256
Public Const DNLEN = 15
Public Const PWLEN = 256
Public Const RAS95_MaxPhoneNumber = 128
Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxCallbackNumber = RAS95_MaxPhoneNumber
Public Type RASDIALPARAMS95
dwSize As Long
szEntryName(RAS95_MaxEntryName) As Byte
szPhoneNumber(RAS95_MaxPhoneNumber) As Byte
szCallbackNumber(RAS95_MaxCallbackNumber) As Byte
szUserName(UNLEN) As Byte
szPassword(PWLEN) As Byte
szDomain(DNLEN) As Byte
End Type
'**********************************
'* RAS調(diào)用錯誤代號 *
'**********************************
Public Const NOT_SUPPORTED = 120&
Public Const RASBASEERROR = 600&
Public Const SUCCESS = 0&
Public Const ERROR_PORT_ALREADY_OPEN = (RASBASEERROR + 2)
Public Const ERROR_UNKNOWN = (RASBASEERROR + 35)
Public Const ERROR_REQUEST_TIMEOUT = (RASBASEERROR + 38)
Public Const ERROR_PASSWD_EXPIRED = (RASBASEERROR + 48)
Public Const ERROR_NO_DIALIN_PERMISSION = (RASBASEERROR + 49)
Public Const ERROR_SERVER_NOT_RESPONDING = (RASBASEERROR + 50)
Public Const ERROR_UNRECOGNIZED_RESPONSE = (RASBASEERROR + 52)
Public Const ERROR_NO_RESPONSES = (RASBASEERROR + 60)
Public Const ERROR_DEVICE_NOT_READY = (RASBASEERROR + 66)
Public Const ERROR_LINE_BUSY = (RASBASEERROR + 76)
Public Const ERROR_NO_ANSWER = (RASBASEERROR + 78)
Public Const ERROR_NO_CARRIER = (RASBASEERROR + 79)
Public Const ERROR_NO_DIALTONE = (RASBASEERROR + 80)
Public Const ERROR_AUTHENTICATION_FAILURE = (RASBASEERROR + 91)
Public Const ERROR_PPP_TIMEOUT = (RASBASEERROR + 118)
'**********************************
'* RAS API 聲明 *
'**********************************
Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (lpString1 As Any, ByVal lpString2 As String) As Long
Public Declare Function RasDial Lib "RasApi32.DLL" Alias "RasDialA" (lpRasDialExtensions As Any, ByVal lpszPhonebook As String, lprasdialparams As Any, ByVal dwNotifierType As Long, lpvNotifier As Long, lphRasConn As Long) As Long
Public Declare Function RasHangUp Lib "RasApi32.DLL" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long
Public Function AddConnection(strNewEntryName As String, strNewPhoneNumber As String, strNewCallbackNumber As String, strNewUsername As String, strNewPassword As String, strNewDomain As String) As Integer

Dim lngRetCode As Long
Dim lngRetLstrcpy As Long
Dim lngRetHangUp As Long
Dim lprasdialparams As RASDIALPARAMS95
lprasdialparams.dwSize = 1052 '在WINDOWS95/98中必須將dwSize設(shè)為1052
'利用lstrcpy函數(shù)將字符串拷貝到BYTE數(shù)組
lngRetLstrcpy = lstrcpy(lprasdialparams.szEntryName(0), strNewEntryName)
lngRetLstrcpy = lstrcpy(lprasdialparams.szPhoneNumber(0), strNewPhoneNumber)
lngRetLstrcpy = lstrcpy(lprasdialparams.szCallbackNumber(0), strNewCallbackNumber)
lngRetLstrcpy = lstrcpy(lprasdialparams.szUserName(0), strNewUsername)
lngRetLstrcpy = lstrcpy(lprasdialparams.szPassword(0), strNewPassword)
lngRetLstrcpy = lstrcpy(lprasdialparams.szDomain(0), strNewDomain)
'我們使用同步通信
Screen.MousePointer = vbHourglass
hRasConn = 0 '
lngRetCode = RasDial(ByVal APINULL, vbNullString, lprasdialparams, APINULL, ByVal APINULL, hRasConn)
Screen.MousePointer = vbDefault
'測試有沒有錯誤
If lngRetCode Then
lngRetHangUp = RasHangUp(hRasConn)
End If
AddConnection = lngRetCode
End Function
Public Sub RemoveConnection(H_RasConn As Long)
Call RasHangUp(hRasConn)
End Sub

  6.最后,編譯生成應(yīng)用程序,我們就實(shí)現(xiàn)了在自己的應(yīng)用程序中撥號上網(wǎng)。本程序在PWIN98,VB6.0環(huán)境下編譯通過。