Solved

Browse Network and Return IP Address

Posted on 1999-01-19
4
143 Views
Last Modified: 2010-05-03
I'm looking for code to browse the network neighborhood and return the IP of the machine that was selected.

Any ideas??
Thanks.
0
Comment
Question by:skretsch
  • 2
4 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1469448
You could use the ping command to retreive it.
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1469449
You can use the SHBrowseForFolder function from shell32.dll
0
 
LVL 4

Accepted Solution

by:
chabaud earned 200 total points
ID: 1469450
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
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1469451
Bought This Question.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

813 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

19 Experts available now in Live!

Get 1:1 Help Now