获取本机IP和网卡地址
发布网友
发布时间:2022-04-29 00:09
我来回答
共4个回答
热心网友
时间:2022-06-26 02:58
现在的计算机以及网络组成十分复杂。例如系统硬件方面就有主板、硬盘、网卡... 。
软件方面有操作系统、系统中安装的软件、正在运行的进程等等。网络方面有域、工作组
等等。利用WMI可以访问上面的全部信息,但是如果向上面一样的利用分项来访问的话会很
麻烦。为此,WMI提供了一种类似SQL语句的查询语句,可以通过查询语句获得WMI对象下的子项。
下面是一个遍历系统中安装的网卡并返回网卡MAC地址的代码:
Private Function MACAddress() As String
Set objs = GetObject("winmgmts:").ExecQuery( _
"SELECT MACAddress " & _
"FROM Win32_NetworkAdapter " & _
"WHERE " & _
"((MACAddress Is Not NULL) " & _
"AND (Manufacturer <> " & _
"‘Microsoft‘))")
For Each obj In objs
MACAddress = obj.MACAddress
Exit For
Next obj
End Function
上面的代码获得WMI对象,然后运行ExecQuery执行一个WMI查询语句获得安装的网卡并返回
网卡的MAC地址。
热心网友
时间:2022-06-26 02:59
●●●●●●●●●获取IP●●●●●●●●●●●
Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128
Private Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type
Private Type WSADATA
wversion As Integer
wHighVersion As Integer
szDescription(0 To WSADescription_Len) As Byte
szSystemStatus(0 To WSASYS_Status_Len) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpszVendorInfo As Long
End Type
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
Function hibyte(ByVal wParam As Integer)
hibyte = wParam \ &H100 And &HFF&
End Function
Function lobyte(ByVal wParam As Integer)
lobyte = wParam And &HFF&
End Function
Sub SocketsInitialize()
Dim WSAD As WSADATA
Dim iReturn As Integer
Dim sLowByte As String, sHighByte As String, sMsg As String
iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
If iReturn <> 0 Then
MsgBox "Winsock.dll is not responding."
End
End If
If lobyte(WSAD.wversion) < WS_VERSION_MAJOR Or (lobyte(WSAD.wversion) = WS_VERSION_MAJOR And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
sMsg = sMsg & " is not supported by winsock.dll "
MsgBox sMsg
End
End If
If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
sMsg = "This application requires a minimum of "
sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) & " supported sockets."
MsgBox sMsg
End
End If
End Sub
Sub SocketsCleanup()
Dim lReturn As Long
lReturn = WSACleanup()
If lReturn <> 0 Then
MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
End
End If
End Sub
Private Sub Command1_Click()
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
hostent_addr = gethostbyname(Text1)
If hostent_addr = 0 Then
MsgBox "Can't resolve name."
Exit Sub
End If
RtlMoveMemory host, hostent_addr, LenB(host)
RtlMoveMemory hostip_addr, host.hAddrList, 4
ReDim temp_ip_address(1 To host.hLength)
RtlMoveMemory temp_ip_address(1), 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)
'MsgBox ip_address
Text1.Text = ip_address
End Sub
Sub Form_Load()
SocketsInitialize
End Sub
Private Sub Form_Unload(Cancel As Integer)
SocketsCleanup
End Sub
●●●●●●●●●●获取mac地址●●●●●●●Private Sub Command1_Click()
Text1.Text = Replace(MACAddress, ":", "-")
End Sub
Private Function MACAddress() As String
Set objs = GetObject("winmgmts:").ExecQuery("SELECT MACAddress " & "FROM Win32_NetworkAdapter " & "WHERE " & "((MACAddress Is Not NULL) " & "AND (Manufacturer <> " & "'Microsoft'))")
For Each obj In objs
MACAddress = obj.MACAddress
Exit For
Next obj
End Function
热心网友
时间:2022-06-26 02:59
Option Explicit
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
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
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 = ""
If hostIpStr <> "" Then
ldest = inet_addr(IP)
lhost = inet_addr(hostIpStr)
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
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.1." '假设本局域网的网段为192.168.0.1到192.168.0.254
For i = 1 To 254
MACStr = GetMac(IPStr & i)
If MACStr <> "" Then
Text1.SelStart = Len(Text1.Text)
Text1.SelText = "IP地址:" & IPStr & i & " MAC号:" & MACStr & vbCrLf
End If
Next i
End Sub
Private Sub Command2_Click()
MsgBox GetMyIp
MsgBox GetMac("202.101.118.81")
End Sub
热心网友
时间:2022-06-26 03:00
说个思路吧
那些用API的方法我没尝试。告诉你个简单的
shell ipconfig /all>>"c:\ip.txt"
这样就将ipconfig的回显输出到了C盘下的ip.txt文本文档中
然后就open,readline。。。。你会了吧。
很简单啦,用instr找到先关的字眼,mid截取。。。vb的字符串函数很丰富了。