Visual Basic para Aplicaciones - obtener ip

Life is soft - evento anual de software empresarial
 
Vista:

obtener ip

Publicado por fran (2 intervenciones) el 03/06/2008 10:16:10
Hola, necesito saber la ip de un ordenador que elimine un registro desde una base de datos access, la base de datos esta en el servidor, y necesito saber la dirección IP del que elimina el registro. He estado indagando y he encontrado este codigo de visual basic para aplicaciones:

Const MAX_IP = 5 'To make a buffer... i dont think you have more than 5 ip on your pc..

Type IPINFO
dwAddr As Long ' IP address
dwIndex As Long ' interface index
dwMask As Long ' subnet mask
dwBCastAddr As Long ' broadcast address
dwReasmSize As Long ' assembly size
unused1 As Integer ' not currently used
unused2 As Integer '; not currently used
End Type

Type MIB_IPADDRTABLE
dEntrys As Long 'number of entries in the table
mIPInfo(MAX_IP) As IPINFO 'array of IP address entries
End Type

Type IP_Array
mBuffer As MIB_IPADDRTABLE
BufferLen As Long
End Type

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long

'converts a Long to a string
Private Function ConvertAddressToString(longAddr As Long) As String
Dim myByte(3) As Byte
Dim Cnt As Long
CopyMemory myByte(0), longAddr, 4
For Cnt = 0 To 3
ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
Next Cnt
ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
End Function

Private Sub Comando0_Click()
Dim Ret As Long, Tel As Long
Dim bBytes() As Byte
Dim Listing As MIB_IPADDRTABLE
Dim cad As String

cad = ""

On Error GoTo END1
GetIpAddrTable ByVal 0&, Ret, True

If Ret <= 0 Then Exit Sub
ReDim bBytes(0 To Ret - 1) As Byte
'retrieve the data
GetIpAddrTable bBytes(0), Ret, False

'Get the first 4 bytes to get the entry's.. ip installed
CopyMemory Listing.dEntrys, bBytes(0), 4
'MsgBox "IP's found : " & Listing.dEntrys => Founded ip installed on your PC..
cad = Listing.dEntrys & " IP addresses found on your PC !!" & vbCrLf
cad = cad & "----------------------------------------" & vbCrLf
For Tel = 0 To Listing.dEntrys - 1
'Copy whole structure to Listing..
' MsgBox bBytes(tel) & "."
CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
Debug.Print cad & "IP address : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
Debug.Print cad & "IP Subnetmask : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
Debug.Print cad & "BroadCast IP address : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
Debug.Print cad & "**************************************" & vbCrLf
Next

'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr)
Exit Sub
END1:
MsgBox "ERROR"
End Sub

Esto en teroria me devolvería la direccion IP, pero el problema es que he creado un formulario en acces y al pulsar el botón q deberia ejecutar este código me da el siguiente mensaje:

La Expresion 'Al hacer click' que introdujo como valor de la propiedad produjo un error: No se puede defirnir un tipo definido por el usuario Public dentro de un modulo de objeto.

Haber si alguien me pudiera echar una mano, estoy atrancado aqui y no se como solucionar el problema
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 ip

Publicado por JuanC (243 intervenciones) el 03/06/2008 16:01:44
hasta la línea
Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long
inclusive debe ir en un módulo estándar y el resto del código en el módulo de objeto (correspondiente al UserForm)

Saludos desde Baires, JuanC
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

RE:obtener ip

Publicado por fran (2 intervenciones) el 03/06/2008 16:09:54
juanC muxas gracias por responder y + mas aun por tu brevedad, pero lo cierto es que soy poco experto en el tema y he estado probando distintas opciones, pero no obtenido solucion. Perdona, pero el editor de visual basic de access ¿como genero un modulo estándar o lo hago desde access directamente desde algun formulario o algo parecido?,la otra parte por ejemplo la puedo asociar a un boton en un formulario, pero esta parte de declaracion de tipos no se como resolverla, no estoy muy puesto en el tema, muxas gracias de nuevo
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

RE:obtener ip

Publicado por JuanC (243 intervenciones) el 03/06/2008 21:11:47
El módulo estándar se crea desde el menú Insertar >> Módulo

Saludos desde Baires, JuanC
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

RE:obtener ip

Publicado por Felipe (1 intervención) el 21/07/2009 18:21:46
E encontrado una opcion pero solo nesesitas saber el host ejemplo "www.gmail.com" o culaquier cosa asi

Crea un modulo y pon esto:

Option Explicit
Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type

Public Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long

Public Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long

Public Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)

Ahora crea un formulario y coloca este codigo:

Option Explicit
Private Function DireccionDelNombredelHost(ByVal hostname As String) As String
Dim hostent_addr As Long

Dim host As HOSTENT

Dim hostip_addr As Long
Dim temp_ip_address() As Byte

Dim i As Integer

Dim ip_address As String
Dim result As String
On Error GoTo err
hostent_addr = gethostbyname(hostname)

If hostent_addr = 0 Then
DireccionDelNombredelHost = "<Posiblemte, la dirección de Host que has colocado no es correcta,( o puede no existir). Intenta colocar una nueva dirección de Serviodor SMPT. Si no quieres colocar la te tú Administrador habitual , puedes encontrar en Internet, gratuitamente , alguna dirección, (en algunos casos), con carácter Anónimo...,....aunque no se recomienda este tipo de servidores>"
Exit Function
End If

RtlMoveMemory host, hostent_addr, LenB(host)

RtlMoveMemory hostip_addr, host.hAddrList, 4#
Do

ReDim temp_ip_address(1 To host.hLength)

RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

For i = 1 To host.hLength
ip_address = ip_address & temp_ip_address(i) & "."
Next
Text3 = ip_address
ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
result = result & ip_address
ip_address = ""

host.hAddrList = host.hAddrList + LenB(host.hAddrList)
RtlMoveMemory hostip_addr, host.hAddrList, 4
Loop While (hostip_addr <> 0)

If Len(result) > 0 Then result = Left$(result, Len(result) - Len(vbCrLf))

DireccionDelNombredelHost = result

Exit Function
err:

End Function

Private Sub Command1_Click()
On Error GoTo er
Me.Winsock.Connect Me.Text1.Text, 25
Text2 = DireccionDelNombredelHost(Text1.Text)
Me.Winsock.Close
Exit Sub
er:
Text2 = DireccionDelNombredelHost(Text1.Text)
End Sub

Crea 2 textbox y un boton

Bien ese es el codigo pero te dara 4 IP algunas son temporales pero la mayoria funcionan muy bien en el primer textbox coloca el host y en el segundo apareceran las 4 IP

Chao y espero que te sirva
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