[VB]获取本机的IP地址

来源:互联网 发布:淘宝p授权书查得出来吗 编辑:程序博客网 时间:2024/04/30 15:58

Option Explicit

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long

Const Max_IP = 5

Type IPINFO
dwAddr As Long
dwIndex As Long
dwMask As Long
dwBCastAddr As Long
dwReasmSize As Long
UnUsed1 As Integer
UnUsed2 As Integer
End Type

Type MIB_IPADDRTABLE
dEntrys As Long
mIPInfo(Max_IP) As IPINFO
End Type

Type IP_Array
mBuffer As MIB_IPADDRTABLE
BufferLen As Long
End Type

Sub Main()
Start
End Sub

Public Function ConvertAddressToString(longAddr As Long) As String
Dim MyByte(3) As Byte
Dim Cnt As Long
CopyMemory MyByte(0), longAddr, 4
For Cnt = 0 To 3
ConvertAddressToString = ConvertAddressToString + CStr(MyByte(Cnt)) + "."
Next Cnt
ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
End Function

Public Sub Start()
Dim Ret As Long, Tel As Long
Dim bBytes() As Byte
Dim Listing As MIB_IPADDRTABLE
On Error GoTo End1
GetIpAddrTable ByVal 0&, Ret, True
If Ret <= 0 Then Exit Sub
ReDim bBytes(0 To Ret - 1) As Byte
GetIpAddrTable bBytes(0), Ret, False
CopyMemory Listing.dEntrys, bBytes(0), 4
MsgBox "找到 " & Listing.dEntrys & " 个IP地址!", 0, "提示"
For Tel = 0 To Listing.dEntrys - 1
CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel)) '拷贝整个结构到Listing
MsgBox "IP地址:" & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr), 0, "提示"
Next
End
End1:
MsgBox "出错!", 0, "提示"
End
End Sub

原创粉丝点击