| |
Obtener nombre del host con su IP
Obtener el nombre del HOST con su IP utilizando libreria API wsock32.dll.
Este es el código necesario:
Option Explicit
'UDT
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 256) As Byte
szSystemStatus(0 To 128) As Byte
imaxsockets As Integer
imaxudp As Integer
lpszvenderinfo As Long
End Type
'Declaración de las Api Winsock
'******************************************************
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 nb As Long)
Private Declare Function lstrlen _
Lib "kernel32" _
Alias "lstrlenA" ( _
lpString As Any) As Long
Public Sub SocketsCleanup()
If WSACleanup() <> 0 Then
MsgBox "Error.", vbExclamation
End If
End Sub
'Iniciliza
Public Function Socket() As Boolean
Dim W As WSADATA
Socket = WSAStartup(&H101, W) = 0
End Function
'Función que devuelve el ombre del Host a partir de su IP
Public Function recuperar_Nombre_Host(ByVal direccion_IP As String) As String
Dim PH As Long, hDir As Long, nb As Long
If Socket() Then
hDir = inet_addr(direccion_IP)
'Si devuelve -1 dió error
If hDir <> -1 Then
PH = gethostbyaddr(hDir, 4, 2)
If PH <> 0 Then
CopyMemory PH, ByVal PH, 4
nb = lstrlen(ByVal PH)
If nb > 0 Then
direccion_IP = Space$(nb)
CopyMemory ByVal direccion_IP, ByVal PH, nb
recuperar_Nombre_Host = direccion_IP
End If
Else
MsgBox "Error."
End If
SocketsCleanup
Else
MsgBox "La dirección Ip no es válida."
End If
Else
MsgBox "Error"
End If
End Function
Private Sub Command1_Click()
'Le enviamos la dirección IP
MsgBox recuperar_Nombre_Host(Text1)
End Sub
Private Sub Form_Load()
Text1 = " Ingrese una dirección IP válida "
End Sub
autor: AMC email o web: webmaster@tecnocodigo.com Fecha articulo: 03/05/2009
|
|