vb中连接网络

vb中连接网络,第1张

如果你知道连接的名称,例如“连接到163”或“ADSL”,那么这段程序就可以运行了,不过你可不能保证你的用户机器上有同样的连接名称,所以还必须要使用一段API函数来枚举所有的可用拨号连接名称才行。

新建一个VB

Project,在Form1中做以下声明:

Private

Const

RAS_MaxDeviceType

=

16

Private

Const

RAS95_MaxDeviceName

=

128

Private

Const

RAS95_MaxEntryName

=

256

Private

Type

RASCONN95

dwSize

As

Long

hRasConn

As

Long

szEntryName(RAS95_MaxEntryName)

As

Byte

szDeviceType(RAS_MaxDeviceType)

As

Byte

szDeviceName(RAS95_MaxDeviceName)

As

Byte

End

Type

Private

Type

RASENTRYNAME95

dwSize

As

Long

szEntryName(RAS95_MaxEntryName)

As

Byte

End

Type

Private

Declare

Function

RasEnumConnections

Lib

"RasApi32.DLL"

Alias

"RasEnumConnectionsA"

(lprasconn

As

Any,

lpcb

As

Long,

lpcConnections

As

Long)

As

Long

Private

Declare

Function

RasEnumEntries

Lib

"RasApi32.DLL"

Alias

"RasEnumEntriesA"

(ByVal

reserved

As

String,

ByVal

lpszPhonebook

As

String,

lprasentryname

As

Any,

lpcb

As

Long,

lpcEntries

As

Long)

As

Long

Private

Declare

Function

RasHangUp

Lib

"rasapi32.dll"

Alias

_

"RasHangUpA"

(ByVal

hRasConn

As

Long)

As

Long

在Form上放一个ListBox或者ComboBox,然后在Form_Load中写入以下代码把当前可用的拨号连接名称加进去,这里我们使用ListBox。

Private

Sub

Form_Load()

Dim

s

As

Long,

l

As

Long,

ln

As

Long,

a$

ReDim

R(255)

As

RASENTRYNAME95

R(0).dwSize

=

264

s

=

256

*

R(0).dwSize

l

=

RasEnumEntries(vbNullString,

vbNullString,

R(0),

s,

ln)

For

l

=

0

To

ln

-

1

a$

=

StrConv(R(l).szEntryName(),

vbUnicode)

List1.AddItem

Left$(a$,

InStr(a$,

Chr$(0))

-

1)

Next

List1.ListIndex

=

0

End

Sub

好,现在你就可以运行一下了,ListBox中应该把当前所有的拨号连接名称都显示出来了。

下面再往Form上加一个命令按钮,Caption设为“Connect”。输入以下代码:

Private

Sub

Command1_Click()

Dim

cmd

As

String

cmd

=

"rundll

rnaui.dll,RnaDial

"

&

List1.List(List1.ListIndex)

Shell

cmd$,

vbNormalFocus

End

Sub

这段程序从ListBox获得选择的拨号连接名称,然后调用RunDLL建立拨号连接。如果已经建立连接,它将显示拨号连接的状态。如果你不想让对话框出现,再添一行代码上去:

DoEvents:SendKeys

"{enter}",

True

它将发送一个回车键到当前窗口,相当于用户按了“确认”按钮。

如何挂断当前的连接

我们继续在上面的基础上讲解,挂断连接要用到RasHangUp函数,我们在上面声明部分已经写过了。

再向Form1添加一个按钮Command2,Caption设为Disconnect,输入如下的代码:

Private

Sub

Command2_Click()

Dim

s

As

Long,

l

As

Long,

ln

As

Long,

a$,

RasConn

As

Long,

Ret

As

Long

b$

=

List1.List(List1.ListIndex)

ReDim

R(255)

As

RASCONN95

R(0).dwSize

=

412

s

=

256

*

R(0).dwSize

l

=

RasEnumConnections(R(0),

s,

ln)

For

l

=

0

To

ln

-

1

a$

=

StrConv(R(l).szEntryName(),

vbUnicode)

a$

=

Left$(a$,

InStr(a$,

Chr$(0))

-

1)

RasConn

=

R(l).hRasConn

'这里将挂断连接

Ret

=

RasHangUp(ByVal

RasConn)

Next

End

Sub

这段代码枚举当前所有的连接,并且把它挂断,如果你有双猫,三猫(没有这么好的条件吧),它将把所有连接都挂断。

如何判断当前是否连接到Internet

以前部分网站介绍过读取Registry来判断是否连接到Internet的方法,不过笔者认为这种方法不是最健壮,这里还是在上面的基础上用RasEnumConnections来判断某个连接是否已经连上线了。

再向Form1添加一个按钮Command3,Caption设为Status,输入如下的代码:

Private

Sub

Command2_Click()

Dim

s

As

Long,

l

As

Long,

ln

As

Long,

a$,

b$

b$

=

List1.List(List1.ListIndex)

ReDim

R(255)

As

RASCONN95

R(0).dwSize

=

412

s

=

256

*

R(0).dwSize

l

=

RasEnumConnections(R(0),

s,

ln)

For

l

=

0

To

ln

-

1

a$

=

StrConv(R(l).szEntryName(),

vbUnicode)

a$

=

Left$(a$,

InStr(a$,

Chr$(0))

-

1)

If

a$

=

b$

Then

MsgBox

"Connected

(or

connecting)!":

Exit

Sub

Next

MsgBox

"Not

Connected!"

End

Sub

本段代码判断ListBox当前选中的连接是否已经连接成功或者正在连接之中,并给出相应的信息。

如果你知道连接的名称,例如“连接到163”或“ADSL”,那么这段程序就可以运行了,不过你可不能保证你的用户机器上有同样的连接名称,所以还必须要使用一段API函数来枚举所有的可用拨号连接名称才行。

新建一个VB Project,在Form1中做以下声明:

Private Const RAS_MaxDeviceType = 16

Private Const RAS95_MaxDeviceName = 128

Private Const RAS95_MaxEntryName = 256

Private Type RASCONN95

dwSize As Long

hRasConn As Long

szEntryName(RAS95_MaxEntryName) As Byte

szDeviceType(RAS_MaxDeviceType) As Byte

szDeviceName(RAS95_MaxDeviceName) As Byte

End Type

Private Type RASENTRYNAME95

dwSize As Long

szEntryName(RAS95_MaxEntryName) As Byte

End Type

Private Declare Function RasEnumConnections Lib "RasApi32.DLL" Alias "RasEnumConnectionsA" (lprasconn As Any, lpcb As Long, lpcConnections As Long) As Long

Private Declare Function RasEnumEntries Lib "RasApi32.DLL" Alias "RasEnumEntriesA" (ByVal reserved As String, ByVal lpszPhonebook As String, lprasentryname As Any, lpcb As Long, lpcEntries As Long) As Long

Private Declare Function RasHangUp Lib "rasapi32.dll" Alias _

"RasHangUpA" (ByVal hRasConn As Long) As Long

在Form上放一个ListBox或者ComboBox,然后在Form_Load中写入以下代码把当前可用的拨号连接名称加进去,这里我们使用ListBox。

Private Sub Form_Load()

Dim s As Long, l As Long, ln As Long, a$

ReDim R(255) As RASENTRYNAME95

R(0).dwSize = 264

s = 256 * R(0).dwSize

l = RasEnumEntries(vbNullString, vbNullString, R(0), s, ln)

For l = 0 To ln - 1

a$ = StrConv(R(l).szEntryName(), vbUnicode)

List1.AddItem Left$(a$, InStr(a$, Chr$(0)) - 1)

Next

List1.ListIndex = 0

End Sub

好,现在你就可以运行一下了,ListBox中应该把当前所有的拨号连接名称都显示出来了。

下面再往Form上加一个命令按钮,Caption设为“Connect”。输入以下代码:

Private Sub Command1_Click()

Dim cmd As String

cmd = "rundll rnaui.dll,RnaDial " &List1.List(List1.ListIndex)

Shell cmd$, vbNormalFocus

End Sub

这段程序从ListBox获得选择的拨号连接名称,然后调用RunDLL建立拨号连接。如果已经建立连接,它将显示拨号连接的状态。如果你不想让对话框出现,再添一行代码上去:

DoEvents:SendKeys "{enter}", True

它将发送一个回车键到当前窗口,相当于用户按了“确认”按钮。

如何挂断当前的连接

我们继续在上面的基础上讲解,挂断连接要用到RasHangUp函数,我们在上面声明部分已经写过了。

再向Form1添加一个按钮Command2,Caption设为Disconnect,输入如下的代码:

Private Sub Command2_Click()

Dim s As Long, l As Long, ln As Long, a$, RasConn As Long, Ret As Long

b$ = List1.List(List1.ListIndex)

ReDim R(255) As RASCONN95

R(0).dwSize = 412

s = 256 * R(0).dwSize

l = RasEnumConnections(R(0), s, ln)

For l = 0 To ln - 1

a$ = StrConv(R(l).szEntryName(), vbUnicode)

a$ = Left$(a$, InStr(a$, Chr$(0)) - 1)

RasConn = R(l).hRasConn

'这里将挂断连接

Ret = RasHangUp(ByVal RasConn)

Next

End Sub

这段代码枚举当前所有的连接,并且把它挂断,如果你有双猫,三猫(没有这么好的条件吧),它将把所有连接都挂断。

如何判断当前是否连接到Internet

以前部分网站介绍过读取Registry来判断是否连接到Internet的方法,不过笔者认为这种方法不是最健壮,这里还是在上面的基础上用RasEnumConnections来判断某个连接是否已经连上线了。

再向Form1添加一个按钮Command3,Caption设为Status,输入如下的代码:

Private Sub Command2_Click()

Dim s As Long, l As Long, ln As Long, a$, b$

b$ = List1.List(List1.ListIndex)

ReDim R(255) As RASCONN95

R(0).dwSize = 412

s = 256 * R(0).dwSize

l = RasEnumConnections(R(0), s, ln)

For l = 0 To ln - 1

a$ = StrConv(R(l).szEntryName(), vbUnicode)

a$ = Left$(a$, InStr(a$, Chr$(0)) - 1)

If a$ = b$ Then MsgBox "Connected (or connecting)!": Exit Sub

Next

MsgBox "Not Connected!"

End Sub

本段代码判断ListBox当前选中的连接是否已经连接成功或者正在连接之中,并给出相应的信息。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11637714.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存