Visual Basic - Obtener hostname remoto

Life is soft - evento anual de software empresarial
 
Vista:

Obtener hostname remoto

Publicado por Gher (1 intervención) el 10/05/2004 04:41:56
hola amigos, estoy tratando de hacer un programa que me permite optener los nombres de los pc de una LAN para cargarlos a una combobox. estuve viendo que existe un control winsock , el problema que tengo con este control es que trabaja en forma de cliente servidor, ya que para que funcione tiene que haber dos programas corriendo una en el cliente otro en el servidor. PODRIAN DECIRME QUE CONTROL DE VISUAL BASIC PUEDO USAR PARA HACER ESE TIPO DE PROGRAM YA QUE ME URGE MUCHO GRACIAS...!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Obtener hostname remoto

Publicado por Agui (10 intervenciones) el 11/05/2004 14:08:15
Crea un nuevo módulo y copia el siguiente código:

-----------------------------------------------------------------------
Option Explicit

Private Declare Function WSAStartup Lib "wsock32" _
  (ByVal VersionReq As Long, _
   WSADataReturn As WSADATA) As Long

Private Declare Function WSACleanup Lib "wsock32" () As Long

Private Declare Function inet_addr Lib "wsock32" _
  (ByVal s As String) As Long

Private Declare Function gethostbyaddr Lib "wsock32" _
  (haddr As Long, _
   ByVal hnlen As Long, _
   ByVal addrtype As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (dst As Any, _
   src As Any, _
   ByVal bcount As Long)

Private Declare Function lstrlen Lib "kernel32" _
   Alias "lstrlenA" _
  (lpString As Any) As Long

Private Const SOCKET_ERROR As Long = -1
Private Const AF_INET As Long = 2
Private Const WS_VERSION_REQD As Long = &H101
Private Const IP_SUCCESS As Long = 0

Private Const WSADescription_Len As Long = 256
Private Const WSASYS_Status_Len As Long = 128

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

Public Function GetHostNameFromIP(ByVal sAddress As String) As
String
   Dim ptrHosent As Long
   Dim hAddress As Long
   Dim nbytes As Long
   If SocketsInitialize() Then
     'convert string address to long
      hAddress = inet_addr(sAddress)
      If hAddress <> SOCKET_ERROR Then
        'obtain a pointer to the HOSTENT structure
        'that contains the name and address
        'corresponding to the given network address.
         ptrHosent = gethostbyaddr(hAddress, 4, AF_INET)
         If ptrHosent <> 0 Then
           'convert address and
           'get resolved hostname
            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
         End If 'If ptrHosent
      SocketsCleanup
      End If 'If hAddress
   End If  'If SocketsInitialize
End Function

Public Function SocketsInitialize() As Boolean

   Dim WSAD As WSADATA
   
   SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
   
End Function

Public Sub SocketsCleanup()
   
   If WSACleanup() <> 0 Then
       MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
   End If
   
End Sub

''''''''''''''''''''''''
' FIN MODULO
''''''''''''''''''''''''
-----------------------------------------------------------------------

Ahora crea un nuevo Form, añade un command button y prueba con
el siguiente código:

Private Sub Command1_Click()
   
    MsgBox GetHostNameFromIP("11.12.13.14")

End Sub

Logicamente la IP con la que pruebes (11.12.13.14) deberá pertenecer
a tu LAN.

Espero haberte ayudado,
un saludo,
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar