Winsock1.LocalIP Simple question

Okay I am using winsock control to retrieve an ip address.  However there is a slight problem.

If a user is logged onto the internet and I call_ Winsock1.LocalIP it will return me the IP address, however If a user is not logged it returns

I tried adding Winsock1.Localip to a timer but when a user signs on the winsock.localip does not refresh to the new ip instead it stays at, how can I refresh the winsock control to get the IP??

I am looking for a simple way other than to detect if a user is online or not and then call winsock1.localip..
LVL 29
Who is Participating?
TimCotteeHead of Software ServicesCommented:
Hi egl1044,

Have a look at this solution, it doesn't use the winsock control and is by far a better solution that doing so.

Tim Cottee

Private Sub Command1_Click()
If ActiveConnection = True Then
  MsgBox "You have an active connection.", vbInformation
  MsgBox "You have no active connections.", vbInformation
End If
End Sub

Next, add the following code to a module

Option Explicit

Public Const ERROR_SUCCESS = 0&
Public Const APINULL = 0&
Public Const HKEY_LOCAL_MACHINE = &H80000002

Public ReturnCode As Long

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

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 = "SystemCurrentControlSetServicesRemoteAccess"
    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
nffvrxqgrcfqvvcAuthor Commented:
Sorry virendra_malushte , But that code tells me I'm offline when I am connected.

I don't really want to check if a user is connected or not, I was looking for a way to maybe refresh the winsock control to update the IP address when there is an active connections if its even possible. But thanks anyway.
nffvrxqgrcfqvvcAuthor Commented:
TimCottee , Thank You for pointing me to that link, that seems to work perfect.

Can anyone tell me why the winsock control doesn't refresh? Just curious. Thanks anyway.
TimCotteeHead of Software ServicesCommented:

I don't know for certain why it doesn't refresh, it may be that it is designed only to retrieve the localip when it is initialised.

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.

All Courses

From novice to tech pro — start learning today.