• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Is my PC connected to the Internet?

I want to know how can i know (via VB) if my computer is connected to the Internet.

Thanks in advance.
  • 2
1 Solution
This should really have been a 100 point question..

'Detecting and Internet connection

Public Const HKEY_LOCAL_MACHINE = &H80000002
Public ReturnCode As Long
Public Const ERROR_SUCCESS = 0&
Public Const APINULL = 0&

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
hKey As Long) As Long

Declare Function RegOpenKey Lib "advapi32.dll" Alias _
"RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long

Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
As String, ByVal lpReserved As Long, lpType As Long, _
lpData As Any, lpcbData As Long) As Long
'Form/Module Code
Public 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, _

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
            ActiveConnection = True
        End If
    End If
RegCloseKey (hKey)
End If

End Function
'Function Code:

If ActiveConnection = True then
    Call MsgBox("You have an active connection.",vbInformation)
    Call MsgBox("You have no active connections.", vbInformation)
End If  

Downloading a Web Page

You must have a Winsock control loaded into the project. This code assumes the control is called Winsock1. There also must be a text box on the form which is called txtWebPage.
First, connect to the remote computer using the following code:
Winsock1.RemoteHost = "jelsoft.com"
Winsock1.RemotePort = 80
In the Winsock1.Connect event, place the following code to download the web page.
Dim strCommand as String
Dim strWebPage as String
strWebPage = "http://www.jelsoft.com/index.html"
strCommand = "GET " + strWebPage + " HTTP/1.0" + vbCrLf
strCommand = strCommand + "Accept: */*" + vbCrLf
strCommand = strCommand + "Accept: text/html" + vbCrLf
strCommand = strCommand + vbCrLf
Winsock1.SendData strCommand      
The Winsock control will attempt to download the page. When is receives some data from the server, it will trigger the DataArrival event.
Dim webData As String
Winsock1.GetData webData, vbString
TxtWebPage.Text = TxtWebPage.Text + webData      
pin_plunderAuthor Commented:
yeah... i know.. sorry for the points, but as i have just registered i've got few points.

gonna check code. then i'll tell you if you get the points or not.
pin_plunderAuthor Commented:
good answer.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now