Solved

Browse Network and Return IP Address

Posted on 1999-01-19
4
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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