Browse Network and Return IP Address

I'm looking for code to browse the network neighborhood and return the IP of the machine that was selected.

Any ideas??
Thanks.
skretschAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
chabaudConnect With a Mentor Commented:
Here is a complete code.
Create a new exe project with a form and one CommandButton.
Add a reference to "Microsoft Internet Control" (shdocvw.dll).


Option Explicit

Private Const BIF_BROWSEFORCOMPUTER = &H1000
   
Const WSADESCRIPTION_LEN = 256 + 1
Const WSASYS_STATUS_LEN = 128 + 1
Const AF_INET = 2
Const INADDR_NONE = &HFFFFFFFF

Private Type WSADATA
    wHighVersion As Long
    szDescription As String * WSADESCRIPTION_LEN
    szSystemStatus As String * WSASYS_STATUS_LEN
    iMaxSockets As Long
    iMaxUdpDg As Long
    lpVendorInfo As Long
End Type

Private Type HOSTENT
    h_name As Long 'LPSTR
    h_aliases As Long 'LPLPSTR
    h_addrtype As Integer
    h_length As Integer
    h_addr_list As Long 'char FAR * FAR * h_addr_list
End Type
       
Private Type IN_ADDR
    b1 As Byte
    b2 As Byte
    b3 As Byte
    b4 As Byte
End Type
               
Private Declare Function WSAStartup Lib "Wsock32.dll" (ByVal wVersionRequested As Integer, ByRef lpWSAData As WSADATA) As Long
Private Declare Sub WSACleanup Lib "Wsock32.dll" ()
Private Declare Function gethostbyaddr Lib "Wsock32.dll" (addr As Any, ByVal ilen As Long, ByVal itype As Long) As Long
Private Declare Function inet_addr Lib "Wsock32.dll" (ByVal cp As String) As Long
Private Declare Function gethostbyname Lib "Wsock32.dll" (ByVal name As String) As Long 'HOSTENT

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, lpString2 As Any) As Long

Public Function GetName(cpAddr As String) As String
   
    Dim Adata As WSADATA
    Dim addr As Long
    Dim p As Long
    Dim host As HOSTENT
    Dim buf As String
   
    GetName = ""
   
    If WSAStartup(&H101, Adata) <> 0 Then Exit Function
   
    addr = inet_addr(cpAddr)
    If addr <> INADDR_NONE Then
   
        'switch address
        addr = ((addr And &HFF&) * &H1000000) _
            + ((addr And &HFF000000) / &H1000000) _
            + ((addr And &HFF00&) * &H100) _
            + ((addr And &HFF0000) / &H100)
       
        p = gethostbyaddr(ByVal VarPtr(addr), 4, AF_INET)
       
        If p Then
            CopyMemory ByVal VarPtr(host), ByVal p, LenB(host)
            buf = Space(lstrlen(host.h_name))
            lstrcpy buf, ByVal host.h_name
            GetName = Trim(Left(buf, lstrlen(host.h_name)))
        End If
    End If
   
    WSACleanup

End Function

Public Function GetIPaddress(sName As String) As String
   
    Dim Adata As WSADATA
    Dim a As IN_ADDR
    Dim host As HOSTENT
    Dim p As Long
   
    GetIPaddress = ""
   
    If WSAStartup(&H101, Adata) <> 0 Then Exit Function
   
    p = gethostbyname(sName)
   
    If p Then
        CopyMemory ByVal VarPtr(host), ByVal p, LenB(host)
       
        'p = host.h_addr_list[0]
        CopyMemory ByVal VarPtr(p), ByVal host.h_addr_list, LenB(p)
       
        If p Then
            'a = *p
            CopyMemory ByVal VarPtr(a), ByVal p, LenB(a)
           
            GetIPaddress = a.b1 & "." & a.b2 & "." & a.b3 & "." & a.b4
        End If
    End If
   
    WSACleanup

End Function

Private Sub Command1_Click()
   
    Dim sh As New SHDocVw.Shell
    Dim fd As SHDocVw.Folder
   
    Set fd = sh.BrowseForFolder(Me.hWnd, "Select a computer", BIF_BROWSEFORCOMPUTER, ssfNETWORK)
   
    MsgBox fd.Title & "=" & GetIPaddress(fd.Title)
   
End Sub

0
 
watyCommented:
You could use the ping command to retreive it.
0
 
chabaudCommented:
You can use the SHBrowseForFolder function from shell32.dll
0
 
MirkwoodCommented:
Bought This Question.
0
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.