Dial the Net Automatically
Does your application need to connect to the Internet?
Most code snippets simply show you how to display a typical connection dialog box. This method has three basic flaws; it doesn't force a dial-up, often requires the name of the connection and doesn't let you know when a connection has been made.
Thankfully this code snippet solves all those problems by using Internet Explorer's own automatic dial-up settings.
If you double-click the Internet Options icon in the Control Panel, you'll notice the Connections tab hosts a list of possible dial-up connections.
This snippet utilises a couple of little-known API calls that can automatically dial-up and disconnect from the default connection in that list just like Internet Explorer does if it needs to connect to the Net.
Note: If the 'Never Dial a Connection' option is selected, this code will not be able to connect. It does as Internet Explorer does, and Internet Explorer simply wouldn't connect if that option was selected.
It's worth noting that this code pauses until the connection is actually made - or not, as the case may be. And that's definitely a good thing.
A project demonstrating this code is available for download here.
Code
This code is designed to be placed inside a Form with a Command Button.
Private Declare Function InternetAutodial Lib "wininet.dll" _ (ByVal dwFlags As Long, ByVal dwReserved As Long) As Long Private Const INTERNET_AUTODIAL_FORCE_ONLINE = 1 Private Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2 Private Declare Function InternetAutodialHangup Lib "wininet.dll" _ (ByVal dwReserved As Long) As Long Private Sub Command1_Click() 'To prompt the user to connect to the Net If InternetAutodial(INTERNET_AUTODIAL_FORCE_ONLINE, 0) Then MsgBox "You're Connected!", vbInformation End If 'To automatically start dialling If InternetAutodial(INTERNET_AUTODIAL_FORCE_UNATTENDED, 0) Then MsgBox "You're Connected!", vbInformation End If 'To disconnect an automatically dialled connection If InternetAutodialHangup(0) Then MsgBox "You're Disconnected!", vbInformation End If End Sub