Delphi: Get MAC of Router

前端 未结 2 1080
借酒劲吻你
借酒劲吻你 2020-12-30 11:36

I am using Delphi and I want to determinate the physical MAC address of a network device in my network, in this case the Router itself.

My code:

var
         


        
2条回答
  •  不知归路
    2020-12-30 12:17

    You can use the SendARP function to get the Mac Address.

    check this sample

    uses
     Windows,
     WinSock,
     SysUtils;
    
    
    function SendArp(DestIP,SrcIP:ULONG;pMacAddr:pointer;PhyAddrLen:pointer) : DWord; StdCall; external 'iphlpapi.dll' name 'SendARP';
    
    
    function GetMacAddr(const IPAddress: string; var ErrCode : DWORD): string;
    var
    MacAddr    : Array[0..5] of Byte;
    DestIP     : ULONG;
    PhyAddrLen : ULONG;
    WSAData    : TWSAData;
    begin
      Result    :='';
      WSAStartup($0101, WSAData);
      try
        ZeroMemory(@MacAddr,SizeOf(MacAddr));
        DestIP    :=inet_addr(PAnsiChar(IPAddress));
        PhyAddrLen:=SizeOf(MacAddr);
        ErrCode   :=SendArp(DestIP,0,@MacAddr,@PhyAddrLen);
        if ErrCode = S_OK then
         Result:=Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x',[MacAddr[0], MacAddr[1],MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]])
      finally
        WSACleanup;
      end;
    end;
    

提交回复
热议问题