New project, adding Winsock Control and a button control to the project
Code:
Option Explicit
Private Sub Command1_Click ()
MsgBox "Current computer network card IP:" & Winsock1.localip
End Sub
Method 2: (API method)
New project, adding a button control to the project
Code:
Option Explicit
Private Type Wsadata
Wversion as Integer
Whighversion as Integer
Szdescription (0 to 256)
Szsystemstatus (0 to 128)
Wmaxsockets as Integer
Dwvendorinfo as Long
End Type
Private Declare Function wsastartup Lib "WSOCK32. DLL "(ByVal wversionrequired as Long, lpwsadata as Wsadata) as Long
Private Declare Function wsacleanup Lib "WSOCK32. DLL ' () as Long
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 Sub copymemoryip Lib "kernel32" Alias "RtlMoveMemory" (hpvdest as Any, ByVal Hpvsource as Long, ByVal Cbco PY as Long)
Private Function getipaddress () as String
On Error GoTo Z
Dim WSA as Wsadata
Dim Rval as Long
Dim Shost as String * 256
Dim LP as Long
Dim LPA as Long
Dim Ipadr (0 to 3) as Byte
Rval = WSAStartup ((2 + &h100), WSA)
If rval = 0 Then
GetHostName Shost, 256
LP = gethostbyname (Shost)
IF LP Then
Copymemoryip LPA, LP + 16, 4
Copymemoryip Ipadr (0), LPA, 4
getipaddress = CStr (Ipadr (0)) & "." & CStr (Ipadr (1)) & "& CStr (Ipadr (2)) &". & CStr (Ipadr (3))
End If
WSACleanup
End If
Exit Function
Z:
Getipaddress = ""
End Function
Private Sub Command1_Click ()
DIM ret as String
RET = getipaddress
IF ret <> "" Then
MsgBox "Current computer network card IP:" & RET
End If
End Sub
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.