2010. április 8., csütörtök

How to get a list of all network adapters of a PC


Problem/Question/Abstract:

How to get a list of all network adapters of a PC

Answer:

Example which can be used for all other hardware, too. Doesn't seem to work for Win 2000 though.

uses
  Registry;

procedure GetNetworkAdapters(const List: TStrings);
var
  R: TRegistry;
  i: Integer;
begin
  List.BeginUpdate;
  try
    R := TRegistry.Create;
    try
      R.RootKey := HKEY_DYN_DATA;
      if R.OpenKeyReadOnly('\Config Manager\Enum') then
      try
        R.GetKeyNames(List);
      finally
        R.CloseKey;
      end;
      for i := List.Count - 1 downto 0 do
        if (List[i] = '') or
                                        not R.OpenKeyReadOnly('\Config Manager\Enum\' + List[i]) then
          List.Delete(i)
        else
        try
          List[i] := R.ReadString('HardwareKey');
        finally
          R.CloseKey;
        end;
      R.RootKey := HKEY_LOCAL_MACHINE;
      for i := List.Count - 1 downto 0 do
        if (List[i] = '') or not R.OpenKeyReadOnly('\Enum\' + List[i]) then
          List.Delete(i)
        else
        try
          if CompareText(R.ReadString('Class'), 'net') = 0 then
            List[i] := R.ReadString('DeviceDesc')
          else
            List.Delete(i);
        finally
          R.CloseKey;
        end;
    finally
      R.Free;
    end;
  finally
    List.EndUpdate;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése