1. 程式人生 > >通過 ARP 協議獲取區域網內指定 IP 地址的機器的 MAC 地址

通過 ARP 協議獲取區域網內指定 IP 地址的機器的 MAC 地址

unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,WinSock;

type
  TForm5 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function SendARP(ipaddr: ulong; temp: dword; ulmacaddr: pointer; ulmacaddrleng:
    pointer): dword; stdcall; external 'Iphlpapi.dll' Name 'SendARP';

function IP2Mac(ipaddr:ULong):String;

var
  Form5: TForm5;

implementation

{$R *.dfm}


function IP2Mac(ipaddr:ULong):String;
var
  AMac: array [0 .. 5] of BYTE;
  l: ulong;
  r: integer;
begin
  l:=6;
  r := SendARP(ipaddr, 0, @AMac, @l);
  if r=0 then
    Result:=Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x', [AMac[0], AMac[1], AMac[2], AMac[3], AMac[4],AMac[5]])
  else
    Result:='';
end;

procedure TForm5.Button1Click(Sender: TObject);
begin
    //
    ShowMessage(IP2Mac(inet_addr(PAnsiChar('10.0.0.100'))));
end;


end.