Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

Encontrar la IP de red local

Estas en el tema de Encontrar la IP de red local en el foro de Visual Basic clásico en Foros del Web. Hola a todos, estoy buscando la forma de obtener la IP local mediante un Script de VB, expongo mi problema. Tengo un software, en el ...
  #1 (permalink)  
Antiguo 30/04/2009, 08:43
 
Fecha de Ingreso: agosto-2007
Mensajes: 268
Antigüedad: 16 años, 8 meses
Puntos: 2
Encontrar la IP de red local

Hola a todos,

estoy buscando la forma de obtener la IP local mediante un Script de VB, expongo mi problema. Tengo un software, en el cual no puedo modificar el codigo duro, sin embargo tengo la opcion de trabajar con Scripts. Ahora, necesito obtener la direccion IP de red local ya que el software no trae alguna funcion que me lo permita.

Ya lo intente con un control WINSOCK, pero no funciona en todas las maquinas, en algunas me marca error y no puedo usarlo. ¿Alguien conoce otra forma?

Saludos...
  #2 (permalink)  
Antiguo 30/04/2009, 08:59
Avatar de Sergestux  
Fecha de Ingreso: agosto-2007
Ubicación: Tapachula
Mensajes: 1.218
Antigüedad: 16 años, 8 meses
Puntos: 20
Respuesta: Encontrar la IP de red local

Podria ser ejecutando un ipconfig (con shell) y guardando el resultado en un archivo temporal para despues analizar ese archivo
  #3 (permalink)  
Antiguo 30/04/2009, 09:04
 
Fecha de Ingreso: agosto-2007
Mensajes: 268
Antigüedad: 16 años, 8 meses
Puntos: 2
Respuesta: Encontrar la IP de red local

Ok...esa es una buena opcion, lo voy a probar haber que tal sale
  #4 (permalink)  
Antiguo 30/04/2009, 18:12
Avatar de seba123neo  
Fecha de Ingreso: febrero-2007
Ubicación: Esperanza, Santa Fe
Mensajes: 1.046
Antigüedad: 17 años, 2 meses
Puntos: 19
Respuesta: Encontrar la IP de red local

Cita:
Iniciado por yera2002 Ver Mensaje
Hola a todos,

estoy buscando la forma de obtener la IP local mediante un Script de VB, expongo mi problema. Tengo un software, en el cual no puedo modificar el codigo duro, sin embargo tengo la opcion de trabajar con Scripts. Ahora, necesito obtener la direccion IP de red local ya que el software no trae alguna funcion que me lo permita.

Ya lo intente con un control WINSOCK, pero no funciona en todas las maquinas, en algunas me marca error y no puedo usarlo. ¿Alguien conoce otra forma?

Saludos...
hay un monton de formas para averiguar la IP, con el winsock con una linea de codigo la tenes...te debe tirar error porque el winsock no es un control estandar que esta en todas las maquinas, tenes que llevarlo y registrarlo porque viene con el visual basic...

Código vb:
Ver original
  1. Winsock1.localIP

otras formas:

Código vb:
Ver original
  1. Private Declare Function GetIpAddrTable_API Lib "IpHlpApi" Alias "GetIpAddrTable" (pIPAddrTable As Any, pdwSize As Long, ByVal bOrder As Long) As Long
  2.  
  3. Public Function GetIpAddrTable()
  4.    Dim Buf(0 To 511) As Byte
  5.    Dim BufSize As Long: BufSize = UBound(Buf) + 1
  6.    Dim rc As Long
  7.    rc = GetIpAddrTable_API(Buf(0), BufSize, 1)
  8.    If rc <> 0 Then Err.Raise vbObjectError, , "GetIpAddrTable failed with return value " & rc
  9.    Dim NrOfEntries As Integer: NrOfEntries = Buf(1) * 256 + Buf(0)
  10.    If NrOfEntries = 0 Then GetIpAddrTable = Array(): Exit Function
  11.    ReDim IpAddrs(0 To NrOfEntries - 1) As String
  12.    Dim i As Integer
  13.    For i = 0 To NrOfEntries - 1
  14.       Dim j As Integer, s As String: s = ""
  15.       For j = 0 To 3: s = s & IIf(j > 0, ".", "") & Buf(4 + i * 24 + j): Next
  16.       IpAddrs(i) = s
  17.       Next
  18.    GetIpAddrTable = IpAddrs
  19. End Function
  20.  
  21. Public Sub Test()
  22.    Dim IpAddrs
  23.    IpAddrs = GetIpAddrTable
  24.    Debug.Print "Nr of IP addresses: " & UBound(IpAddrs) - LBound(IpAddrs) + 1
  25.    Dim i As Integer
  26.    For i = LBound(IpAddrs) To UBound(IpAddrs)
  27.       Debug.Print IpAddrs(i)
  28.    Next
  29. End Sub
  30.  
  31. Private Sub Form_Load()
  32.     Call Test
  33. End Sub

Código vb:
Ver original
  1. Option Explicit
  2.  
  3. Private Const WS_VERSION_REQD = &H101
  4. Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
  5. Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
  6. Private Const MIN_SOCKETS_REQD = 1
  7. Private Const SOCKET_ERROR = -1
  8. Private Const WSADescription_Len = 256
  9. Private Const WSASYS_Status_Len = 128
  10.  
  11. Private Type HOSTENT
  12.     hName As Long
  13.     hAliases As Long
  14.     hAddrType As Integer
  15.     hLength As Integer
  16.     hAddrList As Long
  17. End Type
  18.  
  19. Private Type WSADATA
  20.     wversion As Integer
  21.     wHighVersion As Integer
  22.     szDescription(0 To WSADescription_Len) As Byte
  23.     szSystemStatus(0 To WSASYS_Status_Len) As Byte
  24.     iMaxSockets As Integer
  25.     iMaxUdpDg As Integer
  26.     lpszVendorInfo As Long
  27. End Type
  28.  
  29. Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
  30. Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, lpWSAData As WSADATA) As Long
  31. Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
  32.  
  33. Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
  34. Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
  35. Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
  36.  
  37. Function hibyte(ByVal wParam As Integer)
  38.     hibyte = wParam \ &H100 And &HFF&
  39. End Function
  40.  
  41. Function lobyte(ByVal wParam As Integer)
  42.     lobyte = wParam And &HFF&
  43. End Function
  44.  
  45.  
  46. Private Function LocalHostName() As String
  47.     Dim hostname As String * 256
  48.  
  49.     If gethostname(hostname, 256) = SOCKET_ERROR Then
  50.         LocalHostName = "<Error>"
  51.     Else
  52.         LocalHostName = Trim$(hostname)
  53.     End If
  54. End Function
  55.  
  56. Private Sub InitializeSockets()
  57.     Dim WSAD As WSADATA
  58.     Dim iReturn As Integer
  59.     Dim sLowByte As String, sHighByte As String, sMsg As String
  60.  
  61.     iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
  62.  
  63.     If iReturn <> 0 Then
  64.         MsgBox "Winsock.dll is not responding."
  65.         End
  66.     End If
  67.  
  68.     If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = _
  69.         WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
  70.  
  71.         sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
  72.         sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
  73.         sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
  74.         sMsg = sMsg & " is not supported by winsock.dll "
  75.         MsgBox sMsg
  76.         End
  77.     End If
  78.  
  79.     If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
  80.         sMsg = "This application requires a minimum of "
  81.         sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
  82.         MsgBox sMsg
  83.         End
  84.     End If
  85. End Sub
  86.  
  87. Private Sub CleanupSockets()
  88.     Dim lReturn As Long
  89.  
  90.     lReturn = WSACleanup()
  91.  
  92.     If lReturn <> 0 Then
  93.         MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
  94.         End
  95.     End If
  96. End Sub
  97.  
  98. Private Sub cmdGetIPAddress_Click()
  99.     txtIPAddress.Text = IPAddressFromHostName(LocalHostName())
  100. End Sub
  101.  
  102. Private Sub Form_Load()
  103.     InitializeSockets
  104. End Sub
  105.  
  106. Private Sub Form_Unload(Cancel As Integer)
  107.     CleanupSockets
  108. End Sub
  109.  
  110. Private Function IPAddressFromHostName(ByVal hostname As String) As String
  111.     Dim hostent_addr As Long
  112.     Dim host As HOSTENT
  113.     Dim hostip_addr As Long
  114.     Dim temp_ip_address() As Byte
  115.     Dim i As Integer
  116.     Dim ip_address As String
  117.     Dim result As String
  118.  
  119.     hostent_addr = gethostbyname(hostname)
  120.     If hostent_addr = 0 Then
  121.         IPAddressFromHostName = "<error>"
  122.         Exit Function
  123.     End If
  124.  
  125.     RtlMoveMemory host, hostent_addr, LenB(host)
  126.     RtlMoveMemory hostip_addr, host.hAddrList, 4
  127.     Do
  128.         ReDim temp_ip_address(1 To host.hLength)
  129.         RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength
  130.  
  131.         For i = 1 To host.hLength
  132.             ip_address = ip_address & temp_ip_address(i) & "."
  133.         Next
  134.  
  135.         ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
  136.         result = result & ip_address & vbCrLf
  137.         ip_address = ""
  138.  
  139.         host.hAddrList = host.hAddrList + LenB(host.hAddrList)
  140.         RtlMoveMemory hostip_addr, host.hAddrList, 4
  141.     Loop While (hostip_addr <> 0)
  142.  
  143.     If Len(result) > 0 Then result = Left$(result, Len(result) - Len(vbCrLf))
  144.  
  145.     IPAddressFromHostName = result
  146. End Function

saludos.
__________________
" Todos Somos Ignorantes; lo que pasa es que no todos ignoramos las mismas cosas " - Albert Einstein
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:49.