Method of acquiring this machine IP address

xiaoxiao2021-03-06  121

Method 1: (Control method)

New project, join the Winsock control and a button control in the project

Code:

Option expedition

Private submmand1_click () msgbox "Current Computer NIC IP:" & Winsock1.localipend Sub

Method 2: (API)

New project, add a button control in the project

Code:

Option expedition

Private Type Wsadata WVERSION AS INTEGER WHIGHVERSION AS INTEGER SZDESCRIPTION (0 to 256) SzsystemStatus (0 to 128) WMAXSOCKETS AS INTEGER DWVENDORINFO AS LONGEND TYPE

Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSAData As WSAData) As LongPrivate Declare Function WSACleanup Lib "WSOCK32.DLL" () As LongPrivate Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHost As String, ByVal dwHostLen As Long) As LongPrivate Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHost As String) As LongPrivate Declare Sub CopyMemoryIP Lib "kernel32" Alias ​​"RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy 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 endiff =" "" "End Function

PRIVATE SUB Command1_Click () DIM RET AS STRING RET = Getipaddress if Ret <> "" TEN MSGBOX "Current Computer NIC IP:" & Ret End IFEND SUB '----------------- -------------------------- 'please indicate the source' author: Don just fine 'E-mail: tanaya@163.com' ----- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

转载请注明原文地址:https://www.9cbs.com/read-126453.html

New Post(0)