Winsock2 call : gethostbyname() in Visual Basic

Posted on 1998-06-11
Last Modified: 2013-11-25
the socket call gethostbyname() have the following

struct hostent FAR *
    const char FAR * name
so it involves returning a pointer to a UDT, how do I do this in Visual Basic?
I can return the value as BYTE, that doesn't seem to get
me anymore.
any code will be appreciated.
Question by:platiumstar
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

Expert Comment

ID: 1463110
Check out MSKB #Q160215 located at:

Basically here's the declares and calls:
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, HostLen&) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long

   Dim hostname As String * 256
   Dim hostent_addr As Long
  If gethostname(hostname, 256) = SOCKET_ERROR Then
    MsgBox "Windows Sockets error " & Str(WSAGetLastError())
    Exit Sub
    hostname = Trim$(hostname)
  End If
  hostent_addr = gethostbyname(hostname)


Author Comment

ID: 1463111
You're returning a long, where does that get me? I want the HOSTENT structure it returned. Maybe You misunderstood the question.


Expert Comment

ID: 1463112
He had it right, but the pasted text from that article got cut. Rather than me just repaste it here, you should go to the URL clifABB gave, it has an entire example program that does precisely what you want. hostent_addr is a long which holds a pointer to the HOSTENT structure which is then passed to  RtlMoveMemory to copy the structure returned from gethostbyname() into a UDT.

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.


Accepted Solution

yowkee earned 50 total points
ID: 1463113
You need to use API RtlMoveMemory to copy the pointer
to structure into UDT in VB:

Public Type HOSTENT
    hName As Long
    hAliases As Long
    hAddrType As Integer
    hLength As Integer
    hAddrList As Long
End Type
Public Declare Function gethostbyname Lib _
    "WSOCK32.DLL" (ByVal hostname$) As Long
Public Declare Sub RtlMoveMemory Lib _
    "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)

    Dim hostname As String * 256
    Dim hostent_addr As Long
    Dim host As HOSTENT
    Dim hostip_addr As Long
    Dim temp_ip_address() As Byte
    Dim i As Integer
    Dim ip_address As String

    :   ' Getting host name

    hostent_addr = gethostbyname(hostname)
    If hostent_addr = 0 Then
        MsgBox "Winsock.dll is not responding."
        Exit Sub
    End If

    ' Copy to HOSTENT structure
    RtlMoveMemory host, hostent_addr, LenB(host)
    RtlMoveMemory hostip_addr, host.hAddrList, 4
    ReDim temp_ip_address(1 To host.hLength)
    RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
    For i = 1 To host.hLength
        ip_address = ip_address & temp_ip_address(i) & "."
    ip_address = Left$(ip_address, Len(ip_address) - 1)
    MsgBox ip_address



Author Comment

ID: 1463114
ohhh, ok, stupid me. I guess ClifABB deserve some points. But I like alamo's comment better, explain things. oh well. more points next time.

Author Comment

ID: 1463115
ohhh, ok, stupid me. I guess ClifABB deserve some points. But I like alamo's comment better, explain things. oh well. more points next time. Thanks to all.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

623 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