Winsock1.LocalIP Simple question

Posted on 2005-04-14
Last Modified: 2013-11-13
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..
Question by:nffvrxqgrcfqvvc
    LVL 44

    Accepted Solution

    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
    LVL 1

    Expert Comment


    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
    LVL 29

    Author Comment

    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.
    LVL 29

    Author Comment

    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.
    LVL 44

    Expert Comment


    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now