I wonder if there's an easier way?
In addition, what is the simple way to obtain the Internet public IP address
In addition, what is the simple way to obtain the Internet public IP address
Code:
Private Sub Form_Load()
MsgBox Winsock1.LocalIP
End Sub
Code:
Option Explicit
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)
"Socket error constant and version constant
Private Const SOCKET_ERROR As Long = -1
Private Const MAX_WSADescription = 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&
"" Structure for storing host information
Private Type HOSTENT
hName As Long "Official name of the host
hAliases As Long "List of host aliases
hAddrType As Integer "" Host address type
hLen As Integer "" The length of the host address
hAddrList As Long "" Host IP address list
End Type
A structure that stores information such as the Winsock version
Private 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
'' Initializes the Socket
Private Function SocketsInitialize(Optional sErr As String) As Boolean
Dim WSAD As WSADATA
Dim sLoByte As String
Dim sHiByte As String
Initializes the Winsock DLL and determines if the version meets the requirements
If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
sErr = "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
Else
SocketsInitialize = True
End If
End Function
Private Sub SocketsCleanup()
If WSACleanup() <> 0 Then
MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
End If
End Sub
Private Sub Command1_Click()
MsgBox GetIPAddress
End Sub
Function GetIPAddress(Optional sHost As String) As String
"Return the Ip address of the given machine name, or the local Ip address if the machine name is empty
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
'' Exits function if Socket cannot be initialized
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
If no host name is specified, get the local host name and get its IP
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
'' Gets a pointer to the host information structure
lpHost = gethostbyname(sHostName)
'' If the pointer is zero, an error exits
If lpHost = 0 Then
werr = WSAGetLastError()
GetIPAddress = ""
SocketsCleanup
Exit Function
End If
'' Gets data from the specified memory
CopyMemory HOST, lpHost, Len(HOST)
CopyMemory dwIPAddr, HOST.hAddrList, 4
'' Reallocate variable memory dynamically
ReDim tmpIPAddr(1 To HOST.hLen)
Store the host address in tmpIPAddr
CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
'' Gets the final host IP address string
For i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next
'' return
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
"Release the system resources occupied by the Socket library
SocketsCleanup
End Function