unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WinSock;
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;
Edit1: TEdit;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetAdapterInformation;
procedure GetAdapterInformation2;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetAdaptersInfo(AI: PIPAdapterInfo; var BufLen: Integer): Integer;
stdcall; external 'iphlpapi.dll' Name 'GetAdaptersInfo';
function SendArp(ipaddr: ulong; temp: dword; ulmacaddr: pointer; ulmacaddrleng: pointer): dword;
stdcall; external 'Iphlpapi.dll' Name 'SendARP';
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.GetAdapterInformation2;
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;
begin
Size := 5120;
GetMem(AI, Size);
Res := GetAdaptersInfo(AI, Size);
if (Res <> ERROR_SUCCESS) then begin
SetLastError(Res);
RaiseLastWin32Error;
end;
Work := AI;
I := 1;
Edit1.text := Work^.Description + ' ' + MACToStr(@Work^.Address, Work^.AddressLength);
FreeMem(AI);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GetAdapterInformation;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
GetAdapterInformation2;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
myip: ulong;
mymac: array[0..5] of Byte;
mymaclength: ulong;
r: Integer;
begin
//myip := inet_addr(PChar('192.168.0.1'));
myip := inet_addr(PChar('localhost'));
mymaclength := Length(mymac);
r := SendArp(myip, 0, @mymac, @mymaclength);
Edit1.text := 'errorcode:' + IntToStr(r);
Edit1.text := 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;
end.