?
Solved

Winsock1.LocalIP Simple question

Posted on 2005-04-14
5
Medium Priority
?
988 Views
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 127.0.0.1.

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 127.0.0.1, 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..
0
Comment
Question by:nffvrxqgrcfqvvc
  • 2
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 1000 total points
ID: 13782479
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.

http://vbnet.mvps.org/code/network/ipaddress.htm

Tim Cottee
0
 
LVL 1

Expert Comment

by:virendra_malushte
ID: 13782659
TRY FOLLOWING


Private Sub Command1_Click()
If ActiveConnection = True Then
  MsgBox "You have an active connection.", vbInformation
Else
  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, _
    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
0
 
LVL 29

Author Comment

by:nffvrxqgrcfqvvc
ID: 13782871
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.
0
 
LVL 29

Author Comment

by:nffvrxqgrcfqvvc
ID: 13785104
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.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 13788695
egl1044,

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.

Tim
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This video teaches viewers about errors in exception handling.
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.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

840 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