MACアドレスを取得          <TOP>


MAC(Media Access Control)アドレスを取得します。 ではありません・・

Netbios 指定されたネットワークコントロールブロック(NCB)を解釈実行

GetProcessHeap 呼び出し側プロセスのヒープのハンドルを取得

RtlMoveMemory メモリーのブロックのコピー

HeapAlloc 確保したメモリー領域を割り当て

HeapFree HeapReAlloc機能により割り当てられたメモリーブロックを解放

 

Dell(Windows XP Pro)での確認

 

VersaPro(Windows XP Pro)での確認

 

Hp(Windows 7 HP)での確認(VB6でも同じ結果でした)


ネットワーク接続→ローカルエリア接続→ローカルエリア接続の状態→サポート→詳細と進んでいくと見られます。

 

'================================================================
'= MACアドレスを取得
'=    (Netbios.bas)
'=    VB6 からネットワーク アダプタ アドレスを取得する方法
'================================================================
#include "Windows.bi"

#define NCBASTAT &H33                   '
#define NCBNAMSZ 16                     '
#define NCBRESET &H32                   '
#define HEAP_ZERO_MEMORY &H8            '
#define HEAP_GENERATE_EXCEPTIONS &H4    '

Type NET_CONTROL_BLOCK  'NCB
    ncb_command    As Byte
    ncb_retcode    As Byte
    ncb_lsn        As Byte
    ncb_num        As Byte
    ncb_buffer     As Long
    ncb_length     As Integer
    ncb_callname   As String * NCBNAMSZ
    ncb_name       As String * NCBNAMSZ
    ncb_rto        As Byte
    ncb_sto        As Byte
    ncb_post       As Long
    ncb_lana_num   As Byte
    ncb_cmd_cplt   As Byte
    ncb_reserve(9) As Byte
    ncb_event      As Long
End Type

Type ADAPTER_STATUS
    adapter_address(5) As Byte
    rev_major          As Byte
    reserved0          As Byte
    adapter_Type       As Byte
    rev_minor          As Byte
    duration           As Integer
    frmr_recv          As Integer
    frmr_xmit          As Integer
    iframe_recv_err    As Integer
    xmit_aborts        As Integer
    xmit_success       As Long
    recv_success       As Long
    iframe_xmit_err    As Integer
    recv_buff_unavail  As Integer
    t1_timeouts        As Integer
    ti_timeouts        As Integer
    Reserved1          As Long
    free_ncbs          As Integer
    max_cfg_ncbs       As Integer
    max_ncbs           As Integer
    xmit_buf_unavail   As Integer
    max_dgram_size     As Integer
    pEnding_sess       As Integer
    max_cfg_sess       As Integer
    max_sess           As Integer
    max_sess_pkt_size  As Integer
    name_count         As Integer
End Type

Type NAME_BUFFER
    bname      As String * NCBNAMSZ
    name_num   As Integer
    name_flags As Integer
End Type

Type ASTAT
    adapt        As ADAPTER_STATUS
    NameBuff(30) As NAME_BUFFER
End Type

' 指定されたネットワークコントロールブロック(NCB)を解釈実行
Declare Function Api_Netbios& Lib "netapi32" Alias "Netbios" (pncb As NET_CONTROL_BLOCK)

' ある位置から別の位置にメモリブロックを移動する関数の宣言
Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)

' 呼び出し側プロセスのヒープのハンドルを取得
Declare Function Api_GetProcessHeap& Lib "Kernel32" Alias "GetProcessHeap" ()

' 確保したメモリー領域を割り当て
Declare Function Api_HeapAlloc& Lib "Kernel32" Alias "HeapAlloc" (ByVal hHeap&, ByVal dwFlags&, ByVal dwBytes&)

' HeapReAlloc機能により割り当てられたメモリーブロックを解放
Declare Function Api_HeapFree& Lib "Kernel32" Alias "HeapFree" (ByVal hHeap&, ByVal dwFlags&, lpMem As Any)

Var Shared Text1 As Object
Var Shared Text2 As Object
Var Shared Button1 As Object

Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Text2.Attach GetDlgItem("Text2") : Text2.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Function GetMACAddress$ ()
Function GetMACAddress$()
    Var tmp As String
    Var pASTAT As Long
    Var NCB As NET_CONTROL_BLOCK
    Var AST As ASTAT
    Var Ret As Long

    NCB.ncb_command = NCBRESET

    Ret = Api_Netbios(NCB)

    NCB.ncb_callname = "*               "
    NCB.ncb_command = NCBASTAT
    NCB.ncb_lana_num = 0
    NCB.ncb_length = Len(AST)

    pASTAT = Api_HeapAlloc(Api_GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, NCB.ncb_length)

    If pASTAT = 0 Then
        A% = MessageBox("", "メモリ取得に失敗しました", 0, 2)
        Exit Function
    End If

    NCB.ncb_buffer = pASTAT
    Ret = Api_Netbios(NCB)

    CopyMemory AST, NCB.ncb_buffer, Len(AST)

    tmp = Right$("0" & Hex$(AST.adapt.adapter_address(0)), 2) & " " & Right$("0" & Hex$(AST.adapt.adapter_address(1)), 2) & " " & Right$("0" & Hex$(AST.adapt.adapter_address(2)),2)
    tmp = tmp & " " & Right$("0" & Hex$(AST.adapt.adapter_address(3)), 2) & " " & Right$("0" & Hex$(AST.adapt.adapter_address(4)), 2) & " " & Right$("0" & Hex$(AST.adapt.adapter_address(5)), 2)

    Ret = Api_HeapFree(Api_GetProcessHeap(), 0, pASTAT)
    GetMACAddress = tmp
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Text2.SetWindowText GetMACAddress()
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End