Private const socket_error as long =-1
Private const max_wsade133 = 256
Private const max_wsasysstatus = 128
Private const error_success as long = 0
Private const ws_version_reqd as long = & H101
Private const min_sockets_reqd as long = 1
Private const ws_version_major as long = ws_version_reqd \ & h100 and & HFF &
Private const ws_version_minor as long = ws_version_reqd and & HFF &
Private type hostent
Hname as long
Haliases as long
Haddrtype as integer
Hlen as integer
Haddrlist as long
End type
private type wsadata
wversion as integer
whighversion as integer
szdescription (0 to max_wsadeworkflow) as byte
szsystemstatus (0 to max_wsasysstatus) as byte
wmaxsockets as integer
wmaxudpdg as integer
dwvendorinfo as long
end type
private declare function gethostname lib "wsock32.dll" (byval szhost as string, byval dwhostlen as long) as long
private declare function gethostbyname lib "wsock32.dll" (byval szhost as string) As long
private declare function wsastartup lib "wsock32.dll" (byval wversionrequired as long, lpwsadata as wsadata) As long
private declare function wsagetlasterror lib "wsock32.dll" () as long
private declare function wsacleanup lib "wsock32.dll "() as long
private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (hpvdest as any, byval hpvsource as long, byval cbcopy as long)
private function getipaddress (optional shost as string) as string
'returns the IP address of the given machine name, if the machine name is null, the local IP address is returned.
dim shostname as string * 256
dim lphost as long
dim host as hostent
dim dwipaddr as long
dim tmpipaddr () as byte
dim I as integer
dim sipaddr as string
dim werr as long
if not socketsinitialize () then
getipaddress = ""
exit function
end if
If shost = "" Then
If gethostname (shostname, 256) = socket_error then
werr = wsagetlasterror ()
getipaddress = ""
socketscleanup
exit function
end if
Shostname = trim $ (shostname)
Else
Shostname = trim $ (shost) & CHR $ (0)
End if
Lphost = gethostbyname (shostname)
If lphost = 0 then
Werr = wsagetlasterror ()
Getipaddress = ""
Socketscleanup
Exit Function
End if
Copymemory host, lphost, Len (host)
Copymemory dwipaddr, host. haddrlist, 4
Redim tmpipaddr (1 to host. hlen)
Copymemory tmpipaddr (1), dwipaddr, host. hlen
For I = 1 to host. hlen
Sipaddr = sipaddr & tmpipaddr (I )&"."
Next
Getipaddress = mid $ (sipaddr, 1, Len (sipaddr)-1)
Socketscleanup
End Function
Private function socketsinitialize (optional serr as string) as Boolean
Dim wsad as wsadata, slobyte as string, shibyte as string
If wsastartup (ws_version_reqd, wsad) <> error_success then
Serr = "the 32-bit windows socket is not responding ."
Socketsinitialize = false
Exit Function
End if
If wsad. wmaxsockets <min_sockets_reqd then
Serr = "this application requires a minimum "&_
CSTR (min_sockets_reqd) & "supported sockets ."
Socketsinitialize = false
Exit Function
End if
If lobyte (wsad. wversion) <ws_version_major or _
(Lobyte (wsad. wversion) = ws_version_major and _
Hibyte (wsad. wversion) <ws_version_minor) then
Shibyte = CSTR (hibyte (wsad. wversion ))
Slobyte = CSTR (lobyte (wsad. wversion ))
Serr = "Sockets version" & slobyte & "." & shibyte &_
"Is not supported by 32-bit Windows Sockets ."
Socketsinitialize = false
Exit Function
End if
Socketsinitialize = true
End Function
Private sub socketscleanup ()
If wsacleanup () <> error_success then
App. logevent "socket error occurred in cleanup.", vblogeventtypeerror
End if
End sub
Private function hibyte (byval wparam as integer)
Hibyte = wparam \ & H1 and & HFF &
End Function
Private function lobyte (byval wparam as integer)
Lobyte = wparam and & HFF &
End Function
Private sub commandementclick ()
On Error resume next
Screen. mousepointer = vbhourglass
Txtip. Text = getipaddress (txtcmpname. Text)
Screen. mousepointer = vbdefault
End sub