Tip: Automatic connect/disconnect to the Internet

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Tip: Automatic connect/disconnect to the Internet

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by PB.

This tip (for Windows only) assumes that you have a default connection
setting for the Internet in your Dial-Up Networking folder, and that
your username/password is automatic and doesn't require typed entry.

Code: Select all

; Connect/Disconnect from the Internet via default connection.
; By PB -- please feel free to use in any way you wish.  :)
; Note 1: Requires Win98 or higher, or Win95 with Internet Explorer 4.
; Note 2: Username and password must be "remembered" for this to work.
;
If InternetGetConnectedState_(0,0)=#True
  Debug "Already connected by another process!"
Else
  isp$="D-Link iPrimus DSL Connection" ; Default connection name.
  ConnectionID=1 ; 1 = Any number you want to identify this connection.
  If InternetDial_(0,isp$,2,@ConnectionID,0) <> #ERROR_SUCCESS
    Debug "Connection was unsuccessful..."
  Else
    Debug "Connected successfully!"
    Debug "Press SPACE when ready to disconnect..."
    GetAsyncKeyState_(#VK_SPACE) ; Clear space buffer before testing.
    Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_SPACE)=-32767
    Debug "Disconnecting... (May take a few seconds)."
    InternetHangUp_(ConnectionID,0)
    Debug "Disconnected."
  EndIf
EndIf