Delphi调用WinSock和API来取MAC地址

1,首先申明API函数如下:

Function GetIpMacStr(IpAdr:PCHAR):PCHAR;stdcall;
Function sendarp(ipaddr:ulong;
temp:dword;
ulmacaddr:pointer;
ulmacaddrleng:pointer) : DWord; StdCall; External 'Iphlpapi.dll' Name 'SendARP';

 

2,调用过程如下:

Function GetIpMacStr(IpAdr:PCHAR):PCHAR;stdcall;
  var
    I:integer;
    myip:ulong;
    mymac:array[0..5] of byte;
    mymaclength:ulong;
    rsa:integer;
    macstr:string;
  begin
    macstr := '';
    myip:=inet_addr(PCHAR(IpAdr));
    try
      mymaclength:=length(mymac);
      rsa:=sendarp(myip,0,@mymac,@mymaclength);
      if rsa = 0 then
      begin
          for I := 0 to 5 do    // Iterate
          begin
            macstr := macstr + inttohex(integer(mymac[i]), 2);// + '-';
          end;    // for
          Result := PChar(macstr);
      end
      else
        Result := PChar('-1');
    except
      Result := PChar('-2');
    end;
  end; 

你可能感兴趣的:(String,api,function,Integer,byte,Delphi)