this slowpoke moves

Get MAC Address

uses Winsock

function SendARP(const DestIP, SrcIP: in_addr; pMacAddr: PULONG;
var PhyAddrLen: ULONG): DWORD;
    stdcall; external 'IPHLPAPI.DLL' name 'SendARP';
    
//

function GetMacAddr(const IPv4: String): String;
   const
     SrcIP: in_addr = (S_addr: 0);
   var
     DestIP: in_addr;
     MacAddr: array[0..5] of Byte;
     MacAddrLen, RC: DWORD;
     i: Integer;
 begin
   Result := '';
   DestIP.S_addr := inet_addr(PAnsiChar(AnsiString(IPv4)));
   MacAddrLen := Length(MacAddr);
   // msdn.microsoft.com/e...aa366358(VS.85).aspx
   RC := SendARP(DestIP,SrcIP,@MacAddr,MacAddrLen);
   case RC of
     NO_ERROR:
       begin
         if (MacAddrLen = 6) then
           for i := 0 to 5 do begin
             if (i > 0) then
               Result := Result +'-';
             Result := Result +IntToHex(MacAddr[i],2);
           end
         else
           Result := 'Error: MacAddrLen='+IntToStr(MacAddrLen);
       end;
     ERROR_GEN_FAILURE:
       Result := 'ERROR_GEN_FAILURE';
     ERROR_INVALID_PARAMETER:
       Result := 'ERROR_INVALID_PARAMETER';
     ERROR_INVALID_USER_BUFFER:
       Result := 'ERROR_INVALID_USER_BUFFER';
     ERROR_BAD_NET_NAME:
       Result := 'ERROR_BAD_NET_NAME';
     ERROR_BUFFER_OVERFLOW:
       Result := 'ERROR_BUFFER_OVERFLOW';
     else
       Result := 'Error: RC='+IntToStr(RC);
   end;
 end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
const MAC = '192.168.0.1';
begin
  Label1.Caption := GetMacAddr(MAC); // Es kann auch ein String eingesetzt werden.
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate