2009-02-23 147 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;