uGetMacAddr.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. unit uGetMacAddr;
  2. interface
  3. uses
  4. Windows,SysUtils, Classes;
  5. function GetMacAddress: TStringList;
  6. implementation
  7. Const
  8. MAX_HOSTNAME_LEN = 128; { from IPTYPES.H }
  9. MAX_DOMAIN_NAME_LEN = 128;
  10. MAX_SCOPE_ID_LEN = 256;
  11. MAX_ADAPTER_NAME_LENGTH = 256;
  12. MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
  13. MAX_ADAPTER_ADDRESS_LENGTH = 8;
  14. Type
  15. TIPAddressString = Array[0..4*4-1] of AnsiChar;
  16. PIPAddrString = ^TIPAddrString;
  17. TIPAddrString = Record
  18. Next : PIPAddrString;
  19. IPAddress : TIPAddressString;
  20. IPMask : TIPAddressString;
  21. Context : Integer;
  22. End;
  23. PFixedInfo = ^TFixedInfo;
  24. TFixedInfo = Record { FIXED_INFO }
  25. HostName : Array[0..MAX_HOSTNAME_LEN+3] of AnsiChar;
  26. DomainName : Array[0..MAX_DOMAIN_NAME_LEN+3] of AnsiChar;
  27. CurrentDNSServer : PIPAddrString;
  28. DNSServerList : TIPAddrString;
  29. NodeType : Integer;
  30. ScopeId : Array[0..MAX_SCOPE_ID_LEN+3] of AnsiChar;
  31. EnableRouting : Integer;
  32. EnableProxy : Integer;
  33. EnableDNS : Integer;
  34. End;
  35. PIPAdapterInfo = ^TIPAdapterInfo;
  36. TIPAdapterInfo = Record { IP_ADAPTER_INFO }
  37. Next : PIPAdapterInfo;
  38. ComboIndex : Integer;
  39. AdapterName : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of AnsiChar;
  40. Description : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of AnsiChar;
  41. AddressLength : Integer;
  42. Address : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
  43. Index : Integer;
  44. _Type : Integer;
  45. DHCPEnabled : Integer;
  46. CurrentIPAddress : PIPAddrString;
  47. IPAddressList : TIPAddrString;
  48. GatewayList : TIPAddrString;
  49. DHCPServer : TIPAddrString;
  50. HaveWINS : Bool;
  51. PrimaryWINSServer : TIPAddrString;
  52. SecondaryWINSServer : TIPAddrString;
  53. LeaseObtained : Integer;
  54. LeaseExpires : Integer;
  55. End;
  56. //Function GetNetworkParams(FI : PFixedInfo; Var BufLen : Integer) : Integer;
  57. // StdCall; External 'iphlpapi.dll' Name 'GetNetworkParams';
  58. Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
  59. StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';
  60. function GetMacAddress: TStringList;
  61. Var
  62. AI,Work : PIPAdapterInfo;
  63. Size : Integer;
  64. Res : Integer;
  65. // I : Integer;
  66. Function MACToStr(ByteArr : PByte; Len : Integer) : String;
  67. Begin
  68. Result := '';
  69. While (Len > 0) do Begin
  70. Result := Result+IntToHex(ByteArr^, 2);
  71. ByteArr := Pointer(Integer(ByteArr) + SizeOf(Byte));
  72. Dec(Len);
  73. End;
  74. SetLength(Result, Length(Result));
  75. End;
  76. Function GetAddrString(Addr : PIPAddrString) : String;
  77. Begin
  78. Result := '';
  79. While (Addr <> nil) do Begin
  80. Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
  81. Addr := Addr^.Next;
  82. End;
  83. End;
  84. Function TimeTToDateTimeStr(TimeT : Integer) : String;
  85. Const UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
  86. Var
  87. DT : TDateTime;
  88. TZ : TTimeZoneInformation;
  89. Res : DWord;
  90. Begin
  91. If (TimeT = 0) Then Result := ''
  92. Else Begin
  93. { Unix TIME_T is secs since 1/1/1970 }
  94. DT := UnixDateDelta+(TimeT / (24*60*60)); { in UTC }
  95. { calculate bias }
  96. Res := GetTimeZoneInformation(TZ);
  97. If (Res = TIME_ZONE_ID_INVALID) Then RaiseLastWin32Error;
  98. If (Res = TIME_ZONE_ID_STANDARD) Then Begin
  99. DT := DT-((TZ.Bias+TZ.StandardBias) / (24*60));
  100. Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.StandardName);
  101. End
  102. Else Begin { daylight saving time }
  103. DT := DT-((TZ.Bias+TZ.DaylightBias) / (24*60));
  104. Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.DaylightName);
  105. End;
  106. End;
  107. End;
  108. begin
  109. Size := 5120;
  110. GetMem(AI, Size);
  111. Res := GetAdaptersInfo(AI, Size);
  112. If (Res <> ERROR_SUCCESS) Then Begin
  113. SetLastError(Res);
  114. RaiseLastWin32Error;
  115. End;
  116. Work := AI;
  117. //I := 1;
  118. Result := TStringList.Create;
  119. Repeat
  120. //Add('');
  121. //Add('Adapter '+IntToStr(I));
  122. //Add(' ComboIndex: '+IntToStr(Work^.ComboIndex));
  123. //Add(' Adapter name: '+Work^.AdapterName);
  124. //Add(' Description: '+Work^.Description);
  125. //Add(' Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
  126. Result.Add(MACToStr(@Work^.Address, Work^.AddressLength));
  127. //Add(' Index: '+IntToStr(Work^.Index));
  128. //Add(' Type: '+IntToStr(Work^._Type));
  129. //Add(' DHCP: '+IntToStr(Work^.DHCPEnabled));
  130. //Add(' Current IP: '+GetAddrString(Work^.CurrentIPAddress));
  131. //Add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
  132. //Add(' Gateways: '+GetAddrString(@Work^.GatewayList));
  133. //Add(' DHCP servers: '+GetAddrString(@Work^.DHCPServer));
  134. //Add(' Has WINS: '+IntToStr(Integer(Work^.HaveWINS)));
  135. //Add(' Primary WINS: '+GetAddrString(@Work^.PrimaryWINSServer));
  136. //Add(' Secondary WINS: '+GetAddrString(@Work^.SecondaryWINSServer));
  137. //Add(' Lease obtained: '+TimeTToDateTimeStr(Work^.LeaseObtained));
  138. //Add(' Lease expires: '+TimeTToDateTimeStr(Work^.LeaseExpires));
  139. //Inc(I);
  140. Work := Work^.Next;
  141. Until (Work = nil);
  142. //Result:= Copy(Result, 1, 12);
  143. FreeMem(AI);
  144. end;
  145. end.