通过 ARP 协议获取局域网内指定 IP 地址的机器的 MAC 地址

本文介绍了一个使用Delphi实现的简单程序,该程序能够通过发送ARP请求将IPv4地址转换为对应的MAC地址。代码展示了如何利用Iphlpapi.dll中的SendARP函数来获取本地网络上特定IP地址所对应的MAC地址。

http://blog.qdac.cc/?p=2902

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值