Obtain the MAC address from an IP address

Source: Internet
Author: User

Uses
Winsock;

Function sendarp (ipaddr: ulong;
Temp: DWORD;
Ulmacaddr: pointer;
Ulmacaddrleng: pointer): DWORD; stdcall; External 'lplpapi. dll 'name 'sendarp ';

Procedure tform1.button1click (Sender: tobject );
VaR
Myip: ulong;
Mymac: array [0 .. 5] of byte;
Mymaclength: ulong;
R: integer;
Begin
Myip: = inet_addr (pchar ('192. 168.6.180 '));
Mymaclength: = length (mymac );
R: = sendarp (myip, 0, @ mymac, @ mymaclength );
Label1.caption: = 'errorcode: '+ inttostr (R );
Label2.caption: = format ('% 2.2x: % 2.2x: % 2.2x: % 2.2x: % 2.2x: % 2.2x', [mymac [0], mymac [1], mymac [2], mymac [3], mymac [4], mymac [5]);
End;

19:53:30 2. Obtain the MAC address (including multiple NICS) unit unit1;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
WinSock, stdctrls;

Const
Max_hostname_len = 128; {from iptypes. h}
Max_domain_name_len= 128;
Max_scope_id_len= 256;
Max_adapter_name_length = 256;
Max_adapter_description_length = 128;
Max_adapter_address_length = 8;

Type
Tipaddressstring = array [0 .. 4*4-1] of char;

Pipaddrstring = ^ tipaddrstring;
Tipaddrstring = record
Next: pipaddrstring;
IPaddress: tipaddressstring;
Ipmask: tipaddressstring;
Context: integer;
End;

Pfixedinfo = ^ tfixedinfo;
Tfixedinfo = record {fixed_info}
Hostname: array [0 .. max_hostname_len + 3] of char;
Domainname: array [0 .. max_domain_name_len + 3] of char;
Currentdnsserver: pipaddrstring;
Dnsserverlist: tipaddrstring;
Nodetype: integer;
Scopeid: array [0 .. max_scope_id_len + 3] of char;
Enablerouting: integer;
Enableproxy: integer;
Enabledns: integer;
End;

Pipadapterinfo = ^ tipadapterinfo;
Tipadapterinfo = record {ip_adapter_info}
Next: pipadapterinfo;
Comboindex: integer;
Adaptername: array [0 .. max_adapter_name_length + 3] of char;
Description: array [0 .. max_adapter_description_length + 3] of char;
Addresslength: integer;
Address: array [1 .. max_adapter_address_length] of byte;
Index: integer;
_ Type: integer;
Dhcpenabled: integer;
Currentipaddress: pipaddrstring;
Ipaddresslist: tipaddrstring;
Gatewaylist: tipaddrstring;
Dhcpserver: tipaddrstring;
Havewins: bool;
Primarywinsserver: tipaddrstring;
Secondarywinsserver: tipaddrstring;
Leaseobtained: integer;
Leaseexpires: integer;
End;

Type
Tform1 = Class (tform)
Button1: tbutton;
Memo1: tmemo;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Procedure getadapterinformation;
Public
{Public declarations}
End;

VaR
Form1: tform1;

Function sendarp (ipaddr: ulong;
Temp: DWORD;
Ulmacaddr: pointer;
Ulmacaddrleng: pointer): DWORD; stdcall;

Implementation

{$ R *. DFM}

Function sendarp; External 'lplpapi. dll 'name' sendarp ';
Function getadaptersinfo (AI: pipadapterinfo; var buflen: integer): integer;
Stdcall; External 'lplpapi. dll 'name' getadaptersinfo ';

Procedure tform1.getadapterinformation;
VaR
AI, work: pipadapterinfo;
Size: integer;
Res: integer;
I: integer;

Function mactostr (bytearr: pbyte; Len: integer): string;
Begin
Result: = '';
While (LEN> 0) Do begin
Result: = Result + inttohex (bytearr ^, 2) + '-';
Bytearr: = pointer (INTEGER (bytearr) + sizeof (byte ));
Dec (LEN );
End;
Setlength (result, length (result)-1); {remove last dash}
End;

Function getaddrstring (ADDR: pipaddrstring): string;
Begin
Result: = '';
While (ADDR <> nil) Do begin
Result: = Result + 'a: '+ ADDR ^. IPaddress +'m:' + ADDR ^. ipmask + #13;
ADDR: = ADDR ^. Next;
End;
End;

Function timettodatetimestr (timet: integer): string;
Const unixdatedelta = 25569; {days between 12/31/1899 and 1/1/1970}
VaR
DT: tdatetime;
TZ: ttimezoneinformation;
Res: DWORD;

Begin
If (timet = 0) then result: =''
Else begin
{UNIX time_t is secs since 1/1/1970}
DT: = unixdatedelta + (timet/(24*60*60); {in UTC}
{Calculate bias}
Res: = gettimezoneinformation (Tz );
If (RES = time_zone_id_invalid) Then raiselastwin32error;
If (RES = time_zone_id_standard) then begin
DT: = DT-(tz. bias + tz. standardbias)/(24*60 ));
Result: = datetimetostr (DT) + ''+ widechartostring (tz. standardname );
End
Else begin {Daylight Saving Time}
DT: = DT-(tz. bias + tz. daylightbias)/(24*60 ));
Result: = datetimetostr (DT) + ''+ widechartostring (tz. daylightname );
End;
End;
End;

Begin
Memo1.lines. Clear;
Size: = 5120;
Getmem (AI, size );
Res: = getadaptersinfo (AI, size );
If (RES <> error_success) then begin
Setlasterror (RES );
Raiselastwin32error;
End;
With memo1, lines do begin
Work: = AI;
I: = 1;
Repeat
Add ('');
Add ('adapter '+ inttostr (I ));
Add ('comboindex: '+ inttostr (Work ^. comboindex ));
Add ('adapter name: '+ work ^. adaptername );
Add ('description: '+ work ^. Description );
Add ('adapter address: '+ mactostr (@ WORK ^. Address, work ^. addresslength ));
Add ('index: '+ inttostr (Work ^. Index ));
Add ('Type: '+ inttostr (Work ^. _ type ));
Add ('dhcp: '+ inttostr (Work ^. dhcpenabled ));
Add ('current IP: '+ getaddrstring (Work ^. currentipaddress ));
Add ('IP addresses: '+ getaddrstring (@ WORK ^. ipaddresslist ));
Add ('gateways: '+ getaddrstring (@ WORK ^. gatewaylist ));
Add ('dhcp servers: '+ getaddrstring (@ WORK ^. dhcpserver ));
Add ('has wins: '+ inttostr (INTEGER (Work ^. havewins )));
Add ('Primary wins: '+ getaddrstring (@ WORK ^. primarywinsserver ));
Add ('secondary wins: '+ getaddrstring (@ WORK ^. secondarywinsserver ));
Add ('lease obtained: '+ timettodatetimestr (Work ^. leaseobtained ));
Add ('lease expires: '+ timettodatetimestr (Work ^. leaseexpires ));
INC (I );
Work: = work ^. Next;
Until (Work = nil );
End;
Freemem (AI );
End;

Procedure tform1.button1click (Sender: tobject );
Begin
Getadapterinformation;
End;

End.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.