Option Explicit
Public Type hostent
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Public Const WS_VERSION_REQD As Long = &H101
Public Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD As Long = 1
Public Const MAX_WSADescription = 256
Public Const ERROR_SUCCESS = 0
Public Const MAX_WSASYSStatus = 128
Public Type WSAData
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type
Public Const INADDR_NONE = &HFFFF
Declare Function WSAStartup Lib "WSOCK32" _
(ByVal wVersionRequired As Long, lpWSADATA As WSAData) As Long
Public Declare Function WSACleanup Lib "WSOCK32" () As Long
Public Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long
Public Declare Function gethostbyname Lib "ws2_32.dll" _
(ByVal host_name As String) As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
(lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Public Function vbIPFromHostName(ByVal strHostName As String) As String
Dim udtHost As hostent
Dim lngIPAddress As Long
Dim lngPointer As Long
Dim bytIPAddress(0 To 3) As Byte
Dim strBuffer As String
Dim i As Long
If SocketsInitialize = False Then
vbIPFromHostName = "init failure"
Exit Function
End If
lngIPAddress = vbInetAddr(strHostName)
If (lngIPAddress <> INADDR_NONE) Then
vbIPFromHostName = strHostName
Exit Function
End If
lngPointer = gethostbyname(strHostName)
If (lngPointer) Then
Call CopyMemory(udtHost, ByVal lngPointer, LenB(udtHost))
Call CopyMemory(lngPointer, ByVal udtHost.hAddrList, udtHost.hLength)
Call CopyMemory(bytIPAddress(0), ByVal lngPointer, udtHost.hLength)
For i = 0 To 3
strBuffer = strBuffer & CStr(bytIPAddress(i)) & "."
Next i
vbIPFromHostName = Mid$(strBuffer, 1, Len(strBuffer) - 1)
Else
vbIPFromHostName = "Lookup Failed!"
End If
SocketsCleanup
End Function
Public Function vbInetAddr(ByVal strIPAddress As String) As Long
' Convert a dotted IP address into a network byte integer.
vbInetAddr = inet_addr(strIPAddress)
End Function
Public Sub SocketsCleanup()
If WSACleanup() <> ERROR_SUCCESS Then
MsgBox "Socket error occurred in Cleanup."
End If
End Sub
Public Function SocketsInitialize() As Boolean
Dim WSAD As WSAData
Dim sLoByte As String
Dim sHiByte As String
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
MsgBox "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End If
If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
MsgBox "This application requires a minimum of " & 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))
MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
" is not supported by 32-bit Windows Sockets."
SocketsInitialize = False
Exit Function
End If
'must be OK, so lets do it
SocketsInitialize = True
End Function
Public Function HiByte(ByVal wParam As Integer)
HiByte = wParam \ &H100 And &HFF&
End Function
Public Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End Function