Get information about the installed network adapters

前端 未结 5 1168
无人共我
无人共我 2020-12-28 11:23

I am using Delphi XE2 Update 4 on Windows XP sp3

I am looking to get max possible information from installed network adapters, specially the broadcast ip.

Fo

5条回答
  •  北海茫月
    2020-12-28 11:53

    Following Remy Lebeau sugestion and help documenting this thread I found this source code in delphi, tested with XP and W7, that brings the information using GetAdaptersInfo().

    Credits to Brad Prendergast original post updated by Markus Humm final version

    I have added the subnet mask reporting to make clear to newbies like me where the information is stored:

    uses IpHlpApi, IpTypes;
    
    procedure RetrieveLocalAdapterInformation(strings: TStrings);
    var
      pAdapterInfo, pTempAdapterInfo: PIP_ADAPTER_INFO;
      AdapterInfo: IP_ADAPTER_INFO;
      BufLen: DWORD;
      Status: DWORD;
      strMAC: String;
      i: Integer;
    begin
      strings.Clear;
    
      BufLen:= sizeof(AdapterInfo);
      pAdapterInfo:= @AdapterInfo;
    
      Status:= GetAdaptersInfo(nil, BufLen);
      pAdapterInfo:= AllocMem(BufLen);
      try
        Status:= GetAdaptersInfo(pAdapterInfo, BufLen);
    
        if (Status <> ERROR_SUCCESS) then
          begin
            case Status of
              ERROR_NOT_SUPPORTED:
                strings.Add('GetAdaptersInfo is not supported by the operating ' +
                            'system running on the local computer.');
              ERROR_NO_DATA:
                strings.Add('No network adapter on the local computer.');
            else
                strings.Add('GetAdaptersInfo failed with error #' + IntToStr(Status));
            end;
            Dispose(pAdapterInfo);
            Exit;
          end;
    
        while (pAdapterInfo <> nil) do
          begin
            strings.Add('Description: ' + pAdapterInfo^.Description);
            strings.Add('Name: ' + pAdapterInfo^.AdapterName);
    
            strMAC := '';
            for I := 0 to pAdapterInfo^.AddressLength - 1 do
                strMAC := strMAC + '-' + IntToHex(pAdapterInfo^.Address[I], 2);
    
            Delete(strMAC, 1, 1);
            strings.Add('MAC address: ' + strMAC);
            strings.Add('IP address: ' + pAdapterInfo^.IpAddressList.IpAddress.S);
            strings.Add('IP subnet mask: ' + pAdapterInfo^.IpAddressList.IpMask.S);
            strings.Add('Gateway: ' + pAdapterInfo^.GatewayList.IpAddress.S);
            strings.Add('DHCP enabled: ' + IntTOStr(pAdapterInfo^.DhcpEnabled));
            strings.Add('DHCP: ' + pAdapterInfo^.DhcpServer.IpAddress.S);
            strings.Add('Have WINS: ' + BoolToStr(pAdapterInfo^.HaveWins,True));
            strings.Add('Primary WINS: ' + pAdapterInfo^.PrimaryWinsServer.IpAddress.S);
            strings.Add('Secondary WINS: ' + pAdapterInfo^.SecondaryWinsServer.IpAddress.S);
    
            pTempAdapterInfo := pAdapterInfo;
            pAdapterInfo:= pAdapterInfo^.Next;
          if assigned(pAdapterInfo) then Dispose(pTempAdapterInfo);
        end;
      finally
        Dispose(pAdapterInfo);
      end;
    end;
    

提交回复
热议问题