📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "DHCP的IP地址"
ClientHeight = 1980
ClientLeft = 45
ClientTop = 330
ClientWidth = 2430
Icon = "Form1.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 1980
ScaleWidth = 2430
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton Command1
Caption = "运行(&R)"
Default = -1 'True
Height = 375
Left = 600
TabIndex = 2
Top = 1440
Width = 975
End
Begin VB.TextBox Text1
Height = 375
Left = 480
TabIndex = 0
Text = "Text1"
Top = 840
Width = 1575
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "DHCP服务器的IP地址"
Height = 180
Left = 480
TabIndex = 1
Top = 480
Width = 1620
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()
Text1.Text = DhcpServerAddress()
End Sub
Private Function DhcpServerAddress() As String
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
Dim ptr1 As Long
Dim ptr2 As Long
Dim sIPAddr As String
Dim found As Boolean
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) And (found = False)
'将数据转换为IP_ADAPTER_INFO数据类型
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
With Adapter
If .uDhcpEnabled 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))
'如果返回了数据,则退出循环
If Len(sIPAddr) > 0 Then
found = True
Exit Do
End If
'检测其它的服务器
ptr2 = .dwNext
End With 'With AdapterStr
Loop 'Do While (ptr2 <> 0)
'检测其它网卡
ptr1 = .dwNext
End If 'If .uDhcpEnabled
End With 'With Adapter
'如果不再有网卡,则ptr1的值为0
Loop 'Do While (ptr1 <> 0)
End If 'If GetAdaptersInfo
End If 'If cbRequired > 0
'返回结果字符串
DhcpServerAddress = sIPAddr
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 + -