📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "获取所有DHCP的所有IP地址"
ClientHeight = 1755
ClientLeft = 45
ClientTop = 330
ClientWidth = 4125
Icon = "Form1.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 1755
ScaleWidth = 4125
StartUpPosition = 2 '屏幕中心
Begin VB.TextBox Text1
Height = 375
Left = 240
TabIndex = 1
Top = 600
Width = 3375
End
Begin VB.CommandButton Command1
Caption = "运行(&R)"
Height = 375
Left = 2400
TabIndex = 0
Top = 1080
Width = 1215
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "所有安装了NICs的DHCP服务器的地址"
Height = 180
Left = 240
TabIndex = 2
Top = 240
Width = 2880
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
'传递地址列表中分隔各IP地址的界定字符"+"号
Text1.Text = DhcpServerAddresses("+")
End Sub
Private Function DhcpServerAddresses(ByVal sDelim As String) As String
'api vars
Dim buff() As Byte
Dim cbRequired As Long
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
'working vars
Dim ptr1 As Long
Dim ptr2 As Long
Dim sAllAddr As String
Dim sIPAddr As String
Call GetAdaptersInfo(ByVal 0&, cbRequired)
If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte
If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'获取存放数据的缓存buff()的指针
ptr1 = VarPtr(buff(0))
Do While (ptr1 <> 0)
'复制缓存中的第一个网卡的数据到IP_ADAPTER_INFO结构中
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
With Adapter
If .uDhcpEnabled = 1 Then
'DHCP信息存放在IP_ADAPTER_INFO结构的DhcpServer成员中,它
'按IP_ADDR_STRING格式存放,所以,需要将数据复制为IP_ADDR_STRING类型
ptr2 = VarPtr(.DhcpServer)
'另外,IP_ADDR_STRING类型中的成员dwNext指明了存在更多的DHCP服务器,
'因此,需进行另一个循环。
Do While (ptr2 <> 0)
CopyMemory AdapterStr, ByVal ptr2, LenB(AdapterStr)
With AdapterStr
'网卡的DHCP服务器的IP地址
sIPAddr = TrimNull(StrConv(.IpAddress.IpAddr, vbUnicode))
'将IP地址、界定符添加到结果字符串,并继续循环
sAllAddr = sAllAddr & sIPAddr & sDelim
'查找下一个DHCP服务器
ptr2 = .dwNext
End With 'With AdapterStr
Loop 'Do While (ptr2 <> 0)
End If 'If .uDhcpEnabled
'查找下一个网卡
ptr1 = .dwNext
End With 'With Adapter
'直到ptr1返回0(即说明不存在其它的网卡了)时,才停止循环
Loop 'Do While (ptr1 <> 0)
'删除结果字符串中最后的界定字符
If Len(sAllAddr) > 0 Then
sAllAddr = Left$(sAllAddr, Len(sAllAddr) - 1)
End If
End If 'If GetAdaptersInfo
End If 'If cbRequired > 0
'返回结果字符串
DhcpServerAddresses = sAllAddr
End Function
Private Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -