📄 154.htm
字号:
<p>获得网卡的MAC地址</p>
<p></p>
<p> </p>
<p>Private Const NCBASTAT As Long = &H33</p>
<p>Private Const NCBNAMSZ As Long = 16</p>
<p>Private Const HEAP_ZERO_MEMORY As Long = &H8</p>
<p>Private Const HEAP_GENERATE_EXCEPTIONS As Long = &H4</p>
<p>Private Const NCBRESET As Long = &H32</p>
<p>Private Type NET_CONTROL_BLOCK `NCB</p>
<p> ncb_command As Byte</p>
<p> ncb_retcode As Byte</p>
<p> ncb_lsn As Byte</p>
<p> ncb_num As Byte</p>
<p> ncb_buffer As Long</p>
<p> ncb_length As Integer</p>
<p> ncb_callname As String * NCBNAMSZ</p>
<p> ncb_name As String * NCBNAMSZ</p>
<p> ncb_rto As Byte</p>
<p> ncb_sto As Byte</p>
<p> ncb_post As Long</p>
<p> ncb_lana_num As Byte</p>
<p> ncb_cmd_cplt As Byte</p>
<p> ncb_reserve(9) As Byte ` Reserved, must be 0</p>
<p> ncb_event As Long</p>
<p>End Type</p>
<p>Private Type ADAPTER_STATUS</p>
<p> adapter_address(5) As Byte</p>
<p> rev_major As Byte</p>
<p> reserved0 As Byte</p>
<p> adapter_type As Byte</p>
<p> rev_minor As Byte</p>
<p> duration As Integer</p>
<p> frmr_recv As Integer</p>
<p> frmr_xmit As Integer</p>
<p> iframe_recv_err As Integer</p>
<p> xmit_aborts As Integer</p>
<p> xmit_success As Long</p>
<p> recv_success As Long</p>
<p> iframe_xmit_err As Integer</p>
<p> recv_buff_unavail As Integer</p>
<p> t1_timeouts As Integer</p>
<p> ti_timeouts As Integer</p>
<p> Reserved1 As Long</p>
<p> free_ncbs As Integer</p>
<p> max_cfg_ncbs As Integer</p>
<p> max_ncbs As Integer</p>
<p> xmit_buf_unavail As Integer</p>
<p> max_dgram_size As Integer</p>
<p> pending_sess As Integer</p>
<p> max_cfg_sess As Integer</p>
<p> max_sess As Integer</p>
<p> max_sess_pkt_size As Integer</p>
<p> name_count As Integer</p>
<p>End Type</p>
<p>Private Type NAME_BUFFER</p>
<p> name As String * NCBNAMSZ</p>
<p> name_num As Integer</p>
<p> name_flags As Integer</p>
<p>End Type</p>
<p>Private Type ASTAT</p>
<p> adapt As ADAPTER_STATUS</p>
<p> NameBuff(30) As NAME_BUFFER</p>
<p>End Type</p>
<p>Private Declare Function Netbios Lib "netapi32.dll" (pncb As NET_CONTROL_BLOCK) As Byte</p>
<p>Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)</p>
<p>Private Declare Function GetProcessHeap Lib "kernel32" () As Long</p>
<p>Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long</p>
<p>Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long</p>
<p>Function GetMACAddress() As String</p>
<p> `retrieve the MAC Address for the network controller</p>
<p> `installed, returning a formatted string</p>
<p> Dim tmp As String</p>
<p> Dim pASTAT As Long</p>
<p> Dim NCB As NET_CONTROL_BLOCK</p>
<p> Dim AST As ASTAT</p>
<p> `The IBM NetBIOS 3.0 specifications defines four basic</p>
<p> `NetBIOS environments under the NCBRESET command. Win32</p>
<p> `follows the OS/2 Dynamic Link Routine (DLR) environment.</p>
<p> `This means that the first NCB issued by an application</p>
<p> `must be a NCBRESET, with the exception of NCBENUM.</p>
<p> `The Windows NT implementation differs from the IBM</p>
<p> `NetBIOS 3.0 specifications in the NCB_CALLNAME field.</p>
<p> NCB.ncb_command = NCBRESET</p>
<p> Call Netbios(NCB)</p>
<p> `To get the Media Access Control (MAC) address for an</p>
<p> `ethernet adapter programmatically, use the Netbios()</p>
<p> `NCBASTAT command and provide a "*" as the name in the</p>
<p> `NCB.ncb_CallName field (in a 16-chr string).</p>
<p> NCB.ncb_callname = "* "</p>
<p> NCB.ncb_command = NCBASTAT</p>
<p> `For machines with multiple network adapters you need to</p>
<p> `enumerate the LANA numbers and perform the NCBASTAT</p>
<p> `command on each. Even when you have a single network</p>
<p> `adapter, it is a good idea to enumerate valid LANA numbers</p>
<p> `first and perform the NCBASTAT on one of the valid LANA</p>
<p> `numbers. It is considered bad programming to hardcode the</p>
<p> `LANA number to 0 (see the comments section below).</p>
<p> NCB.ncb_lana_num = 0</p>
<p> NCB.ncb_length = Len(AST)</p>
<p> pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, NCB.ncb_length)</p>
<p> If pASTAT = 0 Then</p>
<p> Debug.Print "memory allocation failed!"</p>
<p> Exit Function</p>
<p> End If</p>
<p> NCB.ncb_buffer = pASTAT</p>
<p> Call Netbios(NCB)</p>
<p> CopyMemory AST, NCB.ncb_buffer, Len(AST)</p>
<p> tmp = Format$(Hex(AST.adapt.adapter_address(0)), "00") & " " & Format$(Hex(AST.adapt.adapter_address(1)), "00") & " " & Format$(Hex(AST.adapt.adapter_address(2)), "00") & " " & Format$(Hex(AST.adapt.adapter_address(3)), "00") & " " & Format$(Hex(AST.adapt.adapter_address(4)), "00") & " " & Format$(Hex(AST.adapt.adapter_address(5)), "00")</p>
<p> HeapFree GetProcessHeap(), 0, pASTAT</p>
<p> GetMACAddress = tmp</p>
<p>End Function</p>
<p>Private Sub Form_Load()</p>
<p> MsgBox "Network adapter address: " + GetMACAddress()</p>
<p>End Sub</p>
<p> </p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -