⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 通过vb6.0实现本地网络的ip地址mac地址的扫描和显示
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   5370
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6780
   LinkTopic       =   "Form1"
   ScaleHeight     =   5370
   ScaleWidth      =   6780
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   615
      Left            =   1680
      TabIndex        =   1
      Top             =   3720
      Width           =   3015
   End
   Begin VB.TextBox Text1 
      Height          =   2775
      Left            =   600
      MultiLine       =   -1  'True
      TabIndex        =   0
      Text            =   "Form1.frx":0000
      Top             =   240
      Width           =   5535
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'===================以下定义用于获得本机IP==================
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
Private Type WSA_DATA
wVersion As Integer
wHighVersion As Integer
strDescription(WSADescription_Len + 1) As Byte
strSystemStatus(WSASYS_Status_Len + 1) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type HOSTENT
hname As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal _
wVersionRequired&, lpWSAData As WSA_DATA) As Long
Private Declare Function gethostbyname Lib "ws2_32.dll" (ByVal hostname$) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
Private Declare Function WSACleanup Lib "ws2_32.dll" () As Long
'===================以上定义用于获得本机IP==================



'=====================以下定义用于获得MAC====================
Private Declare Function SendARP Lib "iphlpapi" (ByVal dest As Long, ByVal host As Long, ByRef Mac As Any, ByRef length As Long) As Long
Private Declare Function inet_addr Lib "ws2_32.dll" (ByVal cp As String) As Long
'=====================以上定义用于获得MAC====================


'获得指定IP地址的MAC地址,用到全局变量hostIpStr为本机IP地址
'输入:IP为本网内的IP地址字符串,函数返回MAC地址,若出错返回空字符串
Function GetMac(IP As String) As String
Dim ldest As Long, lhost As Long, Mac(5) As Byte, length As Long
Dim i As Long, lR As Long, hostIpStr As String
hostIpStr = GetMyIp
GetMac = "" ' 若得不到MAC!
If hostIpStr <> "" Then
ldest = inet_addr(IP) '//目的地的IP转换为IP内码形式
lhost = inet_addr(hostIpStr) '//将本机IP转换为IP内码形式
length = 6
lR = SendARP(ldest, lhost, Mac(0), length)
If length > 0 Then
For i = 0 To length - 1
GetMac = GetMac & Right("00" & Hex(Mac(i)), 2)
Next i
End If
End If
End Function


'获得本机IP地址,函数返回值=本机IP地址,若出错返回空字符串
Function GetMyIp() As String
Dim WSAD As WSA_DATA
Dim lR As Long, MyIp As String
Dim hostent_addr As Long
Dim host As HOSTENT
Dim hostip_addr As Long
Dim temp_ip_address() As Byte
Dim i As Integer
Dim ip_address As String
lR = WSAStartup(&H202, WSAD)
If lR <> 0 Then 'WSANOERROR Then
MsgBox "启动WSAStartup失败!"
GetMyIp = ""
Exit Function
End If
hostent_addr = gethostbyname("")

If hostent_addr = 0 Then
GetMyIp = "" '注释:主机名不能被解释
Exit Function
End If

CopyMemory host, ByVal hostent_addr, LenB(host)
CopyMemory hostip_addr, ByVal host.hAddrList, 4

ReDim temp_ip_address(1 To host.hLength)
CopyMemory temp_ip_address(1), ByVal hostip_addr, host.hLength

For i = 1 To host.hLength
ip_address = ip_address & temp_ip_address(i) & "."
Next
ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
GetMyIp = ip_address
WSACleanup
End Function


Private Sub Command1_Click()
Dim i As Integer, IPStr As String, MACStr As String
IPStr = "192.168.0." '假设本局域网的网段为192.168.0.1到192.168.0.254
For i = 1 To 254
MACStr = GetMac(IPStr & i) '如果计算机开机,则MACStr为网卡的MAC号
If MACStr <> "" Then
Text1.SelStart = Len(Text1.Text)
Text1.SelText = "IP地址:" & IPStr & i & " MAC号:" & MACStr & vbCrLf
End If
Next i
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -