【转载】VBA获取计算机网卡MAC地址的函数
时 间:2023-03-21 09:49:26
作 者:金宇 ID:43 城市:江阴
摘 要:VBA获取计算机网卡MAC地址的函数
正 文:
通过VBA获取计算机网卡MAC地址的函数,将下列函数放到新建的模块中,然后在窗体中调用函数WMI_GetMACAddresses
'--------------------------------------------------------------------------------------- ' Procedure : WMI_GetMACAddresses ' Author : Daniel Pineault, CARDA Consultants Inc. ' Website : http://www.cardaconsultants.com ' Purpose : Return a listing of MAC Addresses ' Copyright : The following is release as Attribution-ShareAlike 4.0 International ' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/ ' Req'd Refs: Late Binding -> none required ' ' Input Variables: ' ~~~~~~~~~~~~~~~~ ' bConnectedOnly : Optional - Whether to include all adapters or only currently ' Enabled ones. ' True => Only include Enabled adapters ' False => Include all adapters regardless of their state ' bExcludeVMWare : Optional - Whether to include VMWare entries, or not ' True => Omit them ' False => Include them ' sDelim : Optional - Delimiter to use as a separator in the returned string ' ' Usage: ' ~~~~~~ ' ? WMI_GetMACAddresses ' Returns -> 18:1D:EA:71:69:F2 ' ' ? WMI_GetMACAddresses(False) ' Returns -> 00:FF:FE:96:EE:B2,18:1D:EA:71:69:F2,00:D8:61:05:A0:C7,18:1D:EA:71:69:F3 ' ' ? WMI_GetMACAddresses(True, False) ' Returns -> 00:50:56:C0:00:01,00:50:56:C0:00:08,18:1D:EA:71:69:F2 ' ' Revision History: ' Rev Date(yyyy-mm-dd) Description ' ************************************************************************************** ' 1 2020-11-16 Initial Release '--------------------------------------------------------------------------------------- Public Function WMI_GetMACAddresses(Optional bConnectedOnly As Boolean = True, _ Optional bExcludeVMWare As Boolean = True, _ Optional sDelim As String = ",") As String On Error GoTo Error_Handler #Const WMI_EarlyBind = True 'True => Early Binding / False => Late Binding #If WMI_EarlyBind = True Then Dim oWMI As Object Dim oCols As Object Dim oCol As Object #Else Dim oWMI As Object Dim oCols As Object Dim oCol As Object Const wbemFlagReturnImmediately = 16 '(&H10) Const wbemFlagForwardOnly = 32 '(&H20) #End If Dim sWMIQuery As String 'WMI Query Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") sWMIQuery = "Select * FROM Win32_NetworkAdapterConfiguration" If bConnectedOnly = True Then sWMIQuery = sWMIQuery & " Where IPEnabled=TRUE" End If Set oCols = oWMI.ExecQuery(sWMIQuery, , wbemFlagReturnImmediately or wbemFlagForwardOnly) For Each oCol In oCols 'Debug.Print oCol.Description, oCol.MACAddress, oCol.IPEnabled If IsNull(oCol.MACAddress) = False Then If bExcludeVMWare = True Then If InStr(oCol.Description, "VMware") = 0 Then WMI_GetMACAddresses = WMI_GetMACAddresses & oCol.MACAddress & sDelim End If Else WMI_GetMACAddresses = WMI_GetMACAddresses & oCol.MACAddress & sDelim End If End If Next If Right(WMI_GetMACAddresses, Len(sDelim)) = sDelim Then _ WMI_GetMACAddresses = Left(WMI_GetMACAddresses, Len(WMI_GetMACAddresses) - Len(sDelim)) Error_Handler_Exit: On Error Resume Next Set oCol = Nothing Set oCols = Nothing Set oWMI = Nothing Exit Function Error_Handler: MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: WMI_GetMACAddresses" & vbCrLf & _ "Error Description: " & Err.Description & _ Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _ , vbOKOnly + vbCritical, "An Error has Occurred!" Resume Error_Handler_Exit End Function
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access快速开发平台--对上传...(11.22)
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)
- 微软Access邀测新Monaco...(11.12)
- Access列表框左右互选、列表框...(11.11)
- 高效率在导入数据前删除记录(11.10)
- Access报价单转订单示例代码(11.08)
- Access系统自带的日期选择器不...(11.08)