
Para obtener el nombre de máquina de un ordenador del que conocemos la dirección IP emplearemos la función GetHostNameFromIP que se describe más abajo.
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
Private Const WS_VERSION_REQD As Long = &H101
Private Const IP_SUCCESS As Long = 0
Private Const SOCKET_ERROR As Long = -1
Private Const AF_INET = 2
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To WSADescription_Len) As Byte
szSystemStatus(0 To WSASYS_Status_Len) As Byte
imaxsockets As Integer
imaxudp As Integer
lpszvenderinfo As Long
End Type
Private Declare Function WSAStartup Lib "wsock32.dll" _
(ByVal VersionReq As Long, _
WSADataReturn As WSADATA) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Function inet_addr Lib "wsock32.dll" _
(ByVal s As String) As Long
Private Declare Function gethostbyaddr Lib "wsock32.dll" _
(haddr As Long, _
ByVal hnlen As Long, _
ByVal addrtype As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(xDest As Any, _
xSource As Any, _
ByVal nbytes As Long)
Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenA" _
(lpString As Any) As Long
Public Sub SocketsCleanup()
If WSACleanup() <> 0 Then
MsgBox "Ha ocurrido un error en los sockets.", vbExclamation
End If
End Sub
Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
End Function
Public Function GetHostNameFromIP(ByVal sAddress As String) As String
Dim ptrHosent As Long
Dim hAddress As Long
Dim nbytes As Long
If SocketsInitialize() Then
'convertir la cadena a un long
hAddress = inet_addr(sAddress)
If hAddress <> SOCKET_ERROR Then
'obtener un puntero a la estructura HOSTENT que contiene el nombre
'y la dirección correspondiente a la dirección dada
ptrHosent = gethostbyaddr(hAddress, 4, AF_INET)
If ptrHosent <> 0 Then
CopyMemory ptrHosent, ByVal ptrHosent, 4
nbytes = lstrlen(ByVal ptrHosent)
If nbytes > 0 Then
sAddress = Space$(nbytes)
CopyMemory ByVal sAddress, ByVal ptrHosent, nbytes
GetHostNameFromIP = sAddress
End If
Else
MsgBox "Falló la llamada a gethostbyaddr."
End If
SocketsCleanup
Else
MsgBox "Dirección IP inválida."
End If
Else
MsgBox "Fallo en la inicialización de los sockets."
End If
End Function

