问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

获取本机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的字符串函数很丰富了。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
为什么来大姨妈胸会胀 少儿学什么舞蹈 青年学什么舞蹈好 成年人学什么舞蹈 福州企业最低工资标准 2013年厦门的底薪是多少 生产要素的需求有哪些性质 生产要素的需求有何特点? 什么是生产要素需求 微观经济学要素需求什么是条件要素需求?它和要素需求有什么不同?_百度... 快手直播心愿单幸运魔盒怎么设置- 问一问 快手上龙是怎么抽的? 快手幸运魔盒开出金龙怎么提现- 问一问 工厂宿舍区没有wlan无线网络,如果需要搭建的话选用基站还是无线路由+ap呢,造价大概需要多少? 马自达3改装OZ轮圈和AP刹车,大约要多少钱 单个的吸顶式无线AP需要多少钱? lol里面3级符文AD.AP俩套需要多少金币!!? 70米*8米的5层办公楼,一层设置一个无线AP能否覆盖,大约需要多少钱? 现在购买一个纯ap要多少钱呢? AP信号转换器要多少钱一台 ESl可视化标签专用AP多少钱? 我想做一个商场的无线AP网络覆盖,我想问一下地下室7000平的超市做下来设备大概需要多少钱 如何把普通歌曲做成DJ舞曲 怎样把歌曲改成舞曲? 想把普通的歌曲做成舞曲 怎么样能把普通的歌曲做成DJ舞曲MP3格式?详细点,谢谢。 如何用电脑把普通的歌曲制作成DJ舞曲。 50分求解:如何用自己喜欢的歌曲制作各种舞曲? 怎么样才能把歌曲弄成DJ舞曲,谁有这个软件发来一下,谢谢 怎样把普通歌曲变成舞曲?像慢三,探戈等 转发给好友的微信怎么写留言 VC++获取本机IP 给好友发微信 想发句谁也看不懂的语言 怎么写 如何用c或者c++代码获取默认DNS服务器的ip 通过微信好友给朋友捎点东西答谢幽默的语言怎么写? 手机上怎么写微信? 吸引人加微信的一句话怎么写? 早上5点多,做梦哭醒预示什么?求解 我做了个哭着醒来 有什么寓意吗? 刚刚做了一个梦,把自己哭醒了。醒来才发现满眼泪水,梦里的我一定很难过吧?这是不是什么预兆 君度的经典配方 君度的诠释,正确的解释 卡玫尔的种类 想开一间小小的酒吧.但是酒吧的菜单我不会制定!请大家帮帮忙 我想当一个调香师,可不知道该怎么办 巴黎浪漫吗 酒吧文化的中国酒吧文化 香榭丽舍大街旁的商店穿正装不能进吗? 苹果手机删除的app怎么恢复 酒驾撞人私聊保险公司理赔完反悔了怎么办