MACアドレス取得(U)          <TOP>


Addres Resolution ProtocolによりIPアドレスからMACアドレスを取得します。

inet_addr 文字列を32ビットのバイナリ値に変換

CopyMemory ある位置から別の位置にメモリブロックを移動

SendARP ARPによるMACアドレス取得

 

コマンドプロンプトによるLAN接続されているPCのIPアドレスおよびMACアドレスの確認

 

'================================================================
'= MACアドレス取得(U)
'=    (SendARP.bas)
'================================================================
#include "Windows.bi"

#define NO_ERROR 0

' 文字列を32ビットのバイナリ値に変換
Declare Function Api_inet_addr& Lib "wsock32" Alias "inet_addr" (ByVal s$)

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

' ARP(Address Resolution Protocol)によるMACアドレス取得
Declare Function Api_SendARP& Lib "iphlpapi" Alias "SendARP" (ByVal DestIP&, ByVal SrcIP&, MacAddr&, PhyAddrLen&)

Var Shared Text(1) As Object
Var Shared Edit(1) As Object
Var Shared Button1 As Object

For i = 0 To 1
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 14
    Edit(i).Attach GetDlgItem("Edit" & Trim$(Str$(i + 1))) : Edit(i).SetFontSize 14
Next
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Function GetRemoteMacAddress(sRemoteIP As String, sRemoteMacAddress As String) As Integer
Function GetRemoteMacAddress(sRemoteIP As String, sRemoteMacAddress As String) As Integer
    Var RemoteIP As Long
    Var MacAddr As Long
    Var PhyAddrLen As Long
    Var cnt As Long
    Var tmp As String
   
    RemoteIP = Api_inet_addr(sRemoteIP)
    
    If RemoteIP <> 0 Then
        PhyAddrLen = 6
      
        If Api_SendARP(RemoteIP, 0, MacAddr, PhyAddrLen) = NO_ERROR Then
            If MacAddr <> 0 And PhyAddrLen <> 0 Then
                Var bMacAddr(PhyAddrLen - 1) As Byte
                CopyMemory bMacAddr(0), MacAddr, ByVal PhyAddrLen
            
                For cnt = 0 To PhyAddrLen - 1
                    If bMacAddr(cnt) = 0 Then
                        tmp = tmp & "00-"
                    Else
                        tmp = tmp & Right$("00" & Hex$(bMacAddr(cnt)), 2) & "-"
                    End If
                Next
                
                If Len(tmp) > 0 Then
                    sRemoteMacAddress = Left$(tmp, Len(tmp) - 1)
                    GetRemoteMacAddress = True
                End If
                Exit Function
            Else
                GetRemoteMacAddress = False
            End If
        Else
            GetRemoteMacAddress = False
        End If
    Else
        GetRemoteMacAddress = False
    End If
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var sRemoteMacAddress As String
   
    If Len(Edit(0).GetWindowText) > 0 Then
        If GetRemoteMacAddress(Edit(0).GetWindowText, sRemoteMacAddress) Then
            Edit(1).SetWindowText sRemoteMacAddress
        Else
            Edit(1).SetWindowText "(取得失敗)"
        End If
    End If
End Sub

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