Is there any way to input an IP address and have VB return that IP's hostname?
Who is Participating?
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
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.

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.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I have written a small DLL that exposes a few functions like this.  You can download it from http://wtg.wharton.upenn.edu/~shchuka/Software/Hostname.zip
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.

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

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...
xlunaxAuthor Commented:
thanks alot..i also tryed to reference it...no luck
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.

xlunaxAuthor Commented:
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.
xlunaxAuthor Commented:
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

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++.
xlunaxAuthor Commented:
Well I found out why it crashed...in 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 xlunax@rocketmail.com, maybe you can show me why its screwing up.
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.

xlunaxAuthor Commented:
thanks alot it worked great
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.