Controlling Dial Up Networking using the WinInet API

Introduction

This article explains the basics of controling your DUN([D]ial [U]p [N]etworking) via the WinInet API Function to see if theres an active connection, dial a connection if there isnt or hangup if there is.If your working on a browser based or something to do with the internet then this article may help you.

I've seen many people using the following to get the status of the internet connection:

Function ActiveConnection() As Boolean
 Dim hKey As Long
 Dim lpSubKey As String
 Dim phkResult As Long
 Dim lpValueName As String
 Dim lpReserved As Long
 Dim lpType As Long
 Dim lpData As Long
 Dim lpcbData As Long
   ActiveConnection = False
   lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
   ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, phkResult)

   If ReturnCode = ERROR_SUCCESS Then
       hKey = phkResult
       lpValueName = "Remote Connection"
       lpReserved = APINULL
       lpType = APINULL
       lpData = APINULL
       lpcbData = APINULL
       ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, ByVal lpData, lpcbData)
       lpcbData = Len(lpData)
       ReturnCode = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData)
                   
       If ReturnCode = ERROR_SUCCESS Then
           If lpData = 0 Then
               ActiveConnection = False
             Else
               ActiveConnection = True
           End If
       End If
                               
       RegCloseKey (hKey)
   End If

End Function

The above code was taken from a project by someone else, and I DON'T TAKE ANY CREDIT FOR IT. The code is alright if you arr prepared to add the API functions for the Windows Registry. But if your like me, I like to do things and keep the code to the minimal. I usually code my functions into a handy DLL (ActiveX DLL-[D]ynamic [L]ink [L]ibrary) which I use it mostly all my projects. Its easier and more efficent in some ways. If you were to add the above code to a DLL then you would add the registry code or add dependencies to one.

You might also like...

Comments

About the author

Thushan Fernando

Thushan Fernando Australia

Senior Developer working at Readify on cool new technology and platforms.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone” - Bjarne Stroustrup