VB doubt

Posted on 2004-11-26
Last Modified: 2013-11-13

Does someone know how this can be done?

i want to enter a host name  through a input edit box

then i want to ping the host name using a command line so that i can retrieve the ip adress of that machine.

so that

vv.xx.yy.zz  hostname in the 'hosts' file at


basically the problem is that is there some way so that  

i can accept the host name from the user.
then find the ip address of that machine using say


is there any other way i can obtain the ip of the machine.

and add the entry to hosts file  as above.

i have the code that writes to the hosts file .

i want some way to obtain the ip of the machine, given the host name in my application .

i know this seems tough. Can you help me with this.

Question by:Vlearns
    LVL 48

    Accepted Solution


    How to Ping an IP Address By Host Name
    LVL 8

    Expert Comment

    Well, what are you trying to get the IP of?
    A domain? If it is a domain you are trying
    to track, you can simply connect to the
    domain in place of IP, and then check
    which IP you are connected to.

    Author Comment

    no actually i know machines by their names.

    i want the user to type in this name and be able to retrieve their ip address. i do not know how to do that using vb

    pinging might be a primitive solution.
    do you know a better way?

    LVL 1

    Expert Comment

    If you are using then this is the best way:


    This will return the IP Address of
    You just input a hostname and it returns the ipaddress.
    LVL 1

    Expert Comment

    If you are using VB6 then you have to use about a million lines of code but it can be done:

    This website contains all the code:
    LVL 76

    Assisted Solution

    by:David Lee
    Here's a way, that doesn't require much code, to do it using WMI.  This is from Microsoft's MSDN web site (

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ")
     For Each IPConfig in IPConfigSet
        If Not IsNull(IPConfig.IPAddress) Then
            For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
                    WScript.Echo IPConfig.IPAddress(i)
        End If

    LVL 1

    Assisted Solution

    Put the following code into a form. It uses the Winsock API to get the IP address of a host. Let me know if you have any questions.

    Option Explicit

    Private Declare Function gethostbyname Lib "ws2_32.dll" (ByVal host_name As String) As Long
    Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVR As Long, lpWSAD As WSAData) As Long
    Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long
    Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

    Private Type WSAData
        wVersion As Integer
        wHighVersion As Integer
        szDescription As String * 257
        szSystemStatus As String * 129
        iMaxSockets As Integer
        iMaxUdpDg As Integer
        lpVendorInfo As Long
    End Type

    Private Type HOSTENT
        hName     As Long
        hAliases  As Long
        hAddrType As Integer
        hLength   As Integer
        hAddrList As Long
    End Type

    Private Function sGetIPAddressOfHost(sHost As String) As String
        Dim udtHostent As HOSTENT
        Dim udtWinsockData As WSAData
        Dim lpIP As Long
        Dim lpHNet As Long
        Dim lRet As Long
        Dim abIP() As Byte
        Dim strIpAddress As String
        Dim i As Integer
        lRet = WSAStartup(&H101, udtWinsockData)
        If lRet = 0 Then
            lpHNet = gethostbyname(sHost)
            If lpHNet <> 0 Then
                RtlMoveMemory udtHostent, lpHNet, LenB(udtHostent)
                RtlMoveMemory lpIP, udtHostent.hAddrList, 4
                ReDim abIP(1 To udtHostent.hLength)
                RtlMoveMemory abIP(1), lpIP, udtHostent.hLength
                For i = 1 To udtHostent.hLength
                    strIpAddress = strIpAddress & abIP(i) & "."
                strIpAddress = Left$(strIpAddress, Len(strIpAddress) - 1)
                sGetIPAddressOfHost = strIpAddress
                Exit Function
            End If
        End If
    End Function

    Private Sub Form_Load()
        MsgBox sGetIPAddressOfHost("")
    End Sub

    Author Comment

    i figured it out
    thanks all
    LVL 48

    Expert Comment

    by:Ryan Chong
    Hi Vlearns,

    What's your solution, who make the helps here? ;-)

    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

    This article will show, step by step, how to integrate R code into a R Sweave document
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now