
Getting User name & IP address
Quote:
> Does anyone know where to find current user login name & more
>important, the current IP address of the current machine?
In a LAN, Internet, what? If you want to find Internet address:
Type HostEntType
h_name As Long 'LPstr
h_aliases As Long 'LPstr to *h_aliases
h_addrtype As Integer 'Plain int
h_length As Integer 'Plain int
h_addr As Long 'LPstr
End Type
Declare Function gethostname Lib "WinSock.DLL" (ByVal host As String, ByVal
hostlen As Integer) As Integer
Declare Function gethostbyname Lib "WinSock.DLL" (ByVal host As String) As
Long
Declare Sub hmemcpy Lib "Kernel" (hpvDest As Any, ByVal hpvSource As Any,
ByVal cbCopy&)
Function IPFromName& (ByVal ip_name$)
Dim addr As Long
Dim anIP As Long
Dim aHost As hostenttype
'* Using the gethostbyname API,
anIP = gethostbyname(Trim$(ip_name$))
'* we get back a pointer to a hostent structure.
'* If the pointer is not NULL, we were able to resolve the name into an
'* address.
If anIP <> 0 Then
'* But we're not done yet. Since the result (anIP) is a LONG POINTER
'* to a data
'* structure, we have to force Visual Basic to move the data pointed
'* to by the pointer
'* into a valid hostenttype data structure, by using the Windows
'* hmemcpy API.
hmemcpy aHost, anIP, Len(aHost)
'* Now the hostenttype structure (aHost) is filled in, but we need to
'* get the
'* address out of the h_addr field. The h_addr field is a POINTER to
'* a POINTER,
'* so we use the same hmemcpy trick to get the address of the first
'* pointer...
hmemcpy addr, aHost.h_addr, 4
'* and then use it again to get to the Real Data (of length h_length)
hmemcpy addr, addr, aHost.h_length
'* Whew. Now we have the address stored in the variable addr.
IPFromName& = addr
End If
End Function
Use like this:
hostname$ = Space$(255)
retval% = gethostname(hostname$, len(hostname$))
hostname$ = Left$(hostname$, retval%)
MyIP = IpFromName(hostname$)
--
VB Info: http://www.sn.no/~balchen/vb/visual.htm
FAQ: http://www.sn.no/~balchen/vb/faq.htm
Knowledge Base: http://www.sn.no/~balchen/vb/kb.htm