//获取网卡信息<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>const<br>NULL_IP='0.0.0.0';<br>AdaptTypes : array[0..6] of string[10] =('other','ethernet','tokenring', 'FDDI', 'PPP', 'loopback', 'SLIP' );<br>type<br>TMacAddress = array[1..8] of byte;<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Memo1: TMemo;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>PTIP_ADDRESS_STRING = ^TIP_ADDRESS_STRING;<br>TIP_ADDRESS_STRING = array[0..15] of char; // IP as string<br>//<br>PTIP_ADDR_STRING = ^TIP_ADDR_STRING;<br>TIP_ADDR_STRING = packed record // for use in linked lists<br>Next: PTIP_ADDR_STRING;<br>IpAddress: TIP_ADDRESS_STRING;<br>IpMask: TIP_ADDRESS_STRING;<br>Context: DWORD;<br>end;<br><br>TTIME_T = array[1..325] of byte; // hack! MS time.h missing!<br><br>PTIP_ADAPTER_INFO = ^TIP_ADAPTER_INFO;<br>TIP_ADAPTER_INFO = packed record<br>Next: PTIP_ADAPTER_INFO;<br>ComboIndex: DWORD;<br>AdapterName: array[1..256 + 4] of char;<br>Description: array[1..128 + 4] of char;<br>AddressLength: UINT;<br>Address: TMacAddress;<br>Index: DWORD;<br>aType: UINT;<br>DHCPEnabled: UINT;<br>CurrentIPAddress: PTIP_ADDR_STRING;<br>IPAddressList: TIP_ADDR_STRING;<br>GatewayList: TIP_ADDR_STRING;<br>DHCPServer: TIP_ADDR_STRING;<br>HaveWINS: BOOL;<br>PrimaryWINSServer: TIP_ADDR_STRING;<br>SecondaryWINSServer: TIP_ADDR_STRING;<br>LeaseObtained: TTIME_T; //??<br>LeaseExpires: TTIME_T; //??<br>end;<br><br>function GetAdaptersInfo( pAdapterInfo: PTIP_ADAPTER_INFO;pOutBufLen: PULONG ): DWORD;stdcall; external 'IPHLPAPI.DLL';<br><br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>uses Unit2;<br><br>{$R *.dfm}<br><br><br>function MAC2String(mac:TMacAddress;macLen:integer):String;<br>var<br>i:integer;<br>begin<br>if macLen=0 then<br>begin<br>Result := '00-00-00-00-00-00';<br>EXIT;<br>end else Result := '';<br>for i:=1 to MACLen do result:=result+format('%2.2X-',[MAC]);<br>delete(result,length(result),1);<br>end;<br><br>procedure Get_AdaptersInfo( List: TStrings );<br>var<br>Error,<br>BufLen : DWORD;<br>P : Pointer;<br>AdapterInfo : PTIP_ADAPTER_INFO;<br>Descr,<br>LocalIP,<br>GatewayIP,<br>DHCPIP : string;<br>begin<br>if not Assigned( List ) then EXIT;<br>List.Clear;<br>BufLen := SizeOf( AdapterInfo^ );<br>New( AdapterInfo );<br>Error := GetAdaptersInfo( AdapterInfo, @BufLen );<br>P := AdapterInfo;<br>if Error = NO_ERROR then<br>begin<br>while P <> nil do<br>with TIP_ADAPTER_INFO(P^) do<br>begin<br>SetLength( Descr, SizeOf( Description ));<br>Descr := Trim( string( Description ) );<br><br>if IPAddressList.IpAddress[1] <> #0 then LocalIP := IPAddressList.IpAddress<br>else LocalIP := NULL_IP;<br><br>if GateWayList.IPAddress[1] <> #0 then GateWayIP := GatewayList.IPAddress<br>else GateWayIP := NULL_IP;<br><br>if DHCPServer.IPAddress[1] <> #0 then DHCPIP := DHCPServer.IPAddress<br>else DHCPIP := NULL_IP;<br><br>List.Add( Descr );<br>list.Add(' 索引 | 类型 | MAC 地址 |DHCP| IP 地址 | 网关 IP | DHCP_IP');<br>List.Add( Format('%8.8x|%6s|%16s| %2d |%16s|%16s|%16s',[Index, AdaptTypes[aType],<br>Mac2String(TMacAddress(Address),AddressLength),DHCPEnabled, LocalIP, GatewayIP, DHCPIP]));<br>List.Add(' ');<br>P := Next; // TIP_ADAPTER_INFO(P^).Next points to next entry<br>end // with<br>end // while<br>else<br>List.Add( SysErrorMessage( Error ) );<br>Dispose( AdapterInfo );<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> Get_AdaptersInfo(Memo1.Lines);<br>end;<br><br>end.