|
-
Nov 7th, 2007, 09:33 AM
#1
Thread Starter
Hyperactive Member
Connecting/disconnecting from the internet by code
Hi gurus,
I got a DSL connection on my WinXP machine. When I boot/power on, I double-click my internet connection icon on my desktop to connect to the internet. I know my router could handle this automatically. I also know that I could put it on the startup folder so I don't have to manually connect all the time - but right now I like that way.
I would like to know if it's possible, by code, to terminate the connection, and re-establish it.
Thanks for any advice.
-
Nov 7th, 2007, 12:41 PM
#2
Frenzied Member
Re: Connecting/disconnecting from the internet by code
OK found somthing.
This worked only when the connetion is made and closed from the progrma. If its opened before the progrma starts, it didnt work.
http://www.freevbcode.com/ShowCode.Asp?ID=2089
This is same functions but its .Net.
I converted to VB6.
http://support.microsoft.com/kb/821770/EN-US/
Code:
Option Explicit
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, _
ByVal dwReserved As Long) As Boolean
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" (ByRef lpdwFlags As Long, _
ByVal lpszConnectionName As String, ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long
Private Declare Function InternetHangUp Lib "wininet.dll" (ByVal _
dwConnection As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetDial Lib "wininet.dll" (ByVal hWnd As _
Long, ByVal sConnectoid As String, ByVal dwFlags As Long, _
lpdwConnection As Long, ByVal dwReserved As Long) As Long
Dim ConnectionNumber As Long
Private Enum Flags
'Local system uses a LAN to connect to the Internet.
INTERNET_CONNECTION_LAN = &H2
'Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM = &H1
'Local system uses a proxy server to connect to the Internet.
INTERNET_CONNECTION_PROXY = &H4
'Local system has RAS installed.
INTERNET_RAS_INSTALLED = &H10
End Enum
'Declaration Used For InternetDialUp.
Private Enum DialUpOptions
INTERNET_DIAL_UNATTENDED = &H8000
INTERNET_DIAL_SHOW_OFFLINE = &H4000
INTERNET_DIAL_FORCE_PROMPT = &H2000
End Enum
Private Const ERROR_SUCCESS = &H0
Private Const ERROR_INVALID_PARAMETER = &H87
Private mlConnection As Long
Public Function CheckInternetConnection() As Boolean
Dim aux As String * 255
Dim r As Long
r = InternetGetConnectedStateEx(r, aux, 254, 0)
If r = 1 Then
CheckInternetConnection = True
Else
CheckInternetConnection = False
End If
End Function
Private Sub cmdTestConnection_Click()
Dim lngFlags As Long
If InternetGetConnectedState(lngFlags, 0) Then
'connected.
If lngFlags And Flags.INTERNET_CONNECTION_LAN Then
'LAN connection.
MsgBox ("LAN connection.")
ElseIf lngFlags And Flags.INTERNET_CONNECTION_MODEM Then
'Modem connection.
MsgBox ("Modem connection.")
ElseIf lngFlags And Flags.INTERNET_CONNECTION_PROXY Then
'Proxy connection.
MsgBox ("Proxy connection.")
End If
Else
'not connected.
MsgBox ("Not connected.")
End If
CheckInternetConnection
End Sub
Private Sub cmdDial_Click()
Dim DResult As Long
DResult = InternetDial(Me.hWnd, "My Connection Name", DialUpOptions.INTERNET_DIAL_UNATTENDED, mlConnection, 0) ' give your connection name
If (DResult = ERROR_SUCCESS) Then
MsgBox "Dial Up Successful", , "Dial-Up Connection"
Else
MsgBox "UnSuccessFull Error Code" & DResult, , "Dial-Up Connection"
End If
End Sub
Private Sub cmdHangUp_Click()
Dim Result As Long
If Not (mlConnection = 0) Then
Result = InternetHangUp(mlConnection, 0&)
If Result = 0 Then
MsgBox "Hang up successful", , "Hang Up Connection"
Else
MsgBox "Hang up NOT successful", , "Hang Up Connection"
End If
Else
MsgBox "You must dial a connection first!", , "Hang Up Connection"
End If
End Sub
This is using RAS.
http://www.developerfusion.co.uk/show/247/
http://allapi.mentalis.org/apilist/RasDial.shtml
http://allapi.mentalis.org/apilist/RasHangUp.shtml
http://allapi.mentalis.org/apilist/RasEnumEntries.shtml
All these had one problem. If the connection is already made, before the program runs, then you have no control over it. 
Another two examples
http://www.freevbcode.com/ShowCode.asp?ID=5844
http://www.vbforums.com/showthread.php?t=296384 ' this works 
Try and see.
Last edited by zeezee; Nov 7th, 2007 at 12:44 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|