2009-02-23 145 views

回答

16

Project JEDI API Header Library获取Microsoft IP Helper Library的JEDI转换 - 该文件为IPHlpAPI.zip。解压文件,你需要IpTypes.pas和IpHlpApi.pas。然后你可以使用这样的事情:

procedure TForm1.Button1Click(Sender: TObject); 
var 
    NumInterfaces: Cardinal; 
    AdapterInfo: array of TIpAdapterInfo; 
    OutBufLen: ULONG; 
    i: integer; 
begin 
    GetNumberOfInterfaces(NumInterfaces); 
    SetLength(AdapterInfo, NumInterfaces); 
    OutBufLen := NumInterfaces * SizeOf(TIpAdapterInfo); 
    GetAdaptersInfo(@AdapterInfo[0], OutBufLen); 

    Memo1.Lines.Clear; 
    for i := 0 to NumInterfaces - 1 do begin 
    Memo1.Lines.Add(Format('%.2x:%.2x:%.2x:%.2x:%.2x:%.2x', 
     [AdapterInfo[i].Address[0], AdapterInfo[i].Address[1], 
     AdapterInfo[i].Address[2], AdapterInfo[i].Address[3], 
     AdapterInfo[i].Address[4], AdapterInfo[i].Address[5]])); 
    end; 
end; 

(省略处理所有的错误,你应该增加它当然)。

+0

我用它,对我来说,给所有MAC地址为00:00:00 :00:00:00。我会在这做什么错?如此处所述,我使用OutBufLen作为Cardinal而非ULONG。 – skjoshi 2013-11-12 10:54:57

0

不知道几乎所有关于delphi的东西,如何运行%system32%\ ipconfig.exe/all并解析输出?

+0

输出可能会被本地化,因此分析可能需要测试对不同的Windows版本 - 从阿富汗到津巴布韦 – mjn 2015-11-16 14:36:47

1

GetAdaptersAddresses function是获得自2001年以来与Windows XP的适配器的信息的首选方式。

适配器的信息在IP_ADAPTER_ADDRESSES structure中由AdapterAddresses参数返回。

GetAdaptersAddresses功能可以为的IPv4的IPv6地址检索信息。

调用GetAdaptersAddresses函数的推荐方法是预先分配参数AdapterAddresses指向的15KB工作缓冲区。在典型的计算机上,这会显着降低GetAdaptersAddresses函数返回ERROR_BUFFER_OVERFLOW的机会,这将需要多次调用GetAdaptersAddresses函数。


procedure TForm1.Button1Click(Sender: TObject); 
const 
    AF_UNSPEC = 0; 
    GAA_FLAG_INCLUDE_ALL_INTERFACES = $100; 
    WORKING_BUFFER_SIZE = 15000; 
    MAX_TRIES = 3; 
var 
    pAddresses, 
    pCurrAddresses: PIpAdapterAddresses; 
    dwRetVal, 
    outBufLen: Cardinal; 
    i: Integer; 
    macAddress: string; 
begin 
    Memo1.Lines.Clear; 

    outBufLen := WORKING_BUFFER_SIZE; 
    pAddresses := nil; 
    i := 0; 
    repeat 
    if Assigned(pAddresses) then 
     FreeMem(pAddresses); 

    GetMem(pAddresses, outBufLen); 
    if not Assigned(pAddresses) then 
     raise Exception.Create('Memory allocation failed for IP_ADAPTER_ADDRESSES struct'); 

    dwRetVal := GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_INTERFACES, nil, pAddresses, @outBufLen); 
    Inc(i); 
    until (dwRetVal <> ERROR_BUFFER_OVERFLOW) or (i = MAX_TRIES); 

    try 
    if NO_ERROR <> dwRetVal then begin 
     if ERROR_NO_DATA = dwRetVal then begin 
     MessageDlg('No addresses were found for the requested parameters', mtInformation, [mbOK], 0); 
     Exit; 
     end 
     else 
     raise Exception.Create(SysErrorMessage(dwRetVal)); 
    end; 

    pCurrAddresses := pAddresses; 
    while Assigned(pCurrAddresses) do begin 
     if pCurrAddresses^.PhysicalAddressLength > 0 then begin 
     Memo1.Lines.Add(pCurrAddresses^.FriendlyName); 
     macAddress := ''; 
     for i := 0 to pCurrAddresses^.PhysicalAddressLength - 1 do begin 
      if i > 0 then 
      macAddress := macAddress + ':'; 
      macAddress := macAddress + Format('%.2X', [pCurrAddresses^.PhysicalAddress[i]]); 
     end; 
     Memo1.Lines.Add(macAddress); 
     Memo1.Lines.Add(''); 
     end; 
     pCurrAddresses := pCurrAddresses^.Next; 
    end; 

    finally 
    if Assigned(pAddresses) then 
     FreeMem(pAddresses); 
    end; 
end;