Posted on 1998-10-26
Last Modified: 2010-04-30
Is there any way to input an IP address and have VB return that IP's hostname?
Question by:xlunax
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
  • 8
  • 6

Expert Comment

ID: 1441690
There is a function GetHostByAddr() function in Winsock.dll, but I'm not sure what kind of parameters it takes and what it returns.  I can experiment with it a little, and if I succeed, I'll post the code.


Expert Comment

ID: 1441691
There is a function GetHostByAddr() function in Winsock.dll, but I'm not sure what kind of parameters it takes and what it returns.  I can experiment with it a little, and if I succeed, I'll post the code.


Accepted Solution

shchuka earned 50 total points
ID: 1441692
I'm afraid you cannot use this function directly from a VB program, since it returns a pointer to a structure containing the information.  You'll need to write a C/C++ DLL that would perform the call and return the information in a VB accessible form.

The following is the description of this function:

The Windows Sockets gethostbyaddr function gets host information corresponding to an address.

struct hostent FAR * gethostbyaddr (

    const char FAR * addr,      
    int len,      
    int type      



[in] A pointer to an address in network byte order.


[in] The length of the address.


[in] The type of the address.



gethostbyaddr returns a pointer to the following hostent structure which contains the name(s) and address which correspond to the given address. All strings are null terminated.

Return Values

If no error occurs, gethostbyaddr returns a pointer to the hostent structure described above. Otherwise, it returns a NULL pointer and a specific error number can be retrieved by calling WSAGetLastError.

Error Codes

WSANOTINITIALISED      A successful WSAStartup must occur before using this function.
WSAENETDOWN      The network subsystem has failed.
WSAHOST_NOT_FOUND      Authoritative Answer Host not found.
WSATRY_AGAIN      Non-Authoritative Host not found, or server failed.
WSANO_RECOVERY      Nonrecoverable error occurred.
WSANO_DATA      Valid name, no data record of requested type.
WSAEINPROGRESS      A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEAFNOSUPPORT      The type specified is not supported by the Windows Sockets implementation.
WSAEFAULT      The addr argument is not a valid part of the user address space, or the len argument is too small.
WSAEINTR      The (blocking) call was canceled through WSACancelBlockingCall.

See Also
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1441693
I have written a small DLL that exposes a few functions like this.  You can download it from
It does this function and some other ones (like the other way around: Hostname to IP, etc)

It includes enough documentation to get it to work in a few minutes.  I tested all the functions with VB and they work fine.


Author Comment

ID: 1441694
My VB5 wont find hostname.dll from yer site.  I tryed to reg it but It wont work.  Any ides?


Expert Comment

ID: 1441695
Sounds strange... I will recheck it tomorrow morning, when I'm back at work, that's where I have the code.  Probably, I didn't link something statically and it's looking for some kind of run-time or something...

Again, I'll check it tomorrow...

Author Comment

ID: 1441696
thanks alot..i also tryed to reference luck

Expert Comment

ID: 1441697
Hi Xlunax,

Thanks for your patience.  I just uploaded a new version of that DLL to the same place.  Apparently, just what I said was the case.  I linked borland's runtime tynamically, thus, when you tried to use the dll, it was trying to find VCL35.bpl file and did not succeed.  I recompiled it with static linking.  It should be OK now.  Give it a try and let me know the outcome.


Author Comment

ID: 1441698
Well no it only finds the dll if its in windows\system dir but thats ok...but It crashes the second i call a function.

Author Comment

ID: 1441699
This is exact message.  I tryed to use MSVC++ to debug but no luck.
VB5 caused an invalid page fault in
module HOSTNAME.DLL at 015f:01b1a6ad.
EAX=00000000 CS=015f EIP=01b1a6ad EFLGS=00010246
EBX=0061eaa8 SS=0167 ESP=007ff1ec EBP=007ff1ec
ECX=007ff1f4 DS=0167 ESI=00000000 FS=3d5f
EDX=00000072 ES=0167 EDI=015c0eb0 GS=0e1e
Bytes at CS:EIP:
c6 00 00 8d 45 10 50 8b 55 0c 52 51 68 7c a6 b1
Stack dump:
007ff21c 01b11419 00000000 01b231d8 000000a8 000000bf 00000072 000000b3 007ff230 0063a95a 0063a9c4 bf72b3a0 007ff344 0fbdd74e 0061ea14 00000000


Expert Comment

ID: 1441700
Strange... I created the dll with Borland C++ Builder 3.0 Pro.  I tested it with C++ and with VB - and everything wend smoth...  If you want to, give me your email address, and I'll send you the c++ source code.  You'll need to modify it a little bit to compile with visual c++.

Author Comment

ID: 1441701
Well I found out why it this line of code:
Public Declare Function IpToHostname Lib "Hostname.dll" (ByVal ip As String, ByVal host As String, ByVal hlen As Long) As Long

it says ByVal host as string, but if i dim a variable as a string it crashes.  If i done dim it a string it wont crash, but It still wont resolve the hostname at all.  If you can, write a simple program in VB5.0 that wil resolve an IP using your dll and send it to, maybe you can show me why its screwing up.

Expert Comment

ID: 1441702
Actually - I did get it to crash - and the problem was in NOT preallocating the memory for the return string.  Here's how my procedure looked in my VB test project:

Private Sub cmdAddressToIp_Click()
    Dim host As String
    Dim res As Long
    host = String$(128, 0)
    res = IpToHostname(Text1.Text, host, 128)
    If res = 0 Then
        MsgBox "Error occurred", 16
        host = Left$(host, res)
        MsgBox "Hostname: " + host, 64
    End If
End Sub

In the meantime, I'll send you my zipped VB test project.


Author Comment

ID: 1441703
thanks alot it worked great

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

631 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