-1

I use the following Delphi procedure GetAdapters to list all enabled network adapters:

procedure GetAdapters;
var
  oBindObj : IDispatch;
  oNetAdapters, oNetAdapter,
  odnsAddr, oWMIService : OleVariant;
  i, iValue : LongWord;
  oEnum : IEnumVariant;
  oCtx : IBindCtx;
  oMk : IMoniker;
  sFileObj : WideString;
begin
  MainForm.sComboBox1.Items.Clear;
  sFileObj := 'winmgmts:\\.\root\cimv2';

  OleCheck(CreateBindCtx(0,oCtx));
  OleCheck(MkParseDisplayNameEx(oCtx, PWideChar(sFileObj), i, oMk));
  OleCheck(oMk.BindToObject(oCtx, nil, IUnknown, oBindObj));
  oWMIService := oBindObj;

  oNetAdapters := oWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter=True AND MACAddress IS NOT NULL AND AdapterType IS NOT NULL');

  oEnum := IUnknown(oNetAdapters._NewEnum) as IEnumVariant;

  while oEnum.Next(1, oNetAdapter, iValue) = 0 do begin
    try
      MainForm.sCombobox1.Items.Add(oNetAdapter.Caption);
    except
    end;

    oNetAdapter := Unassigned;
  end;

  odnsAddr := Unassigned;
  oNetAdapters := Unassigned;
  oWMIService := Unassigned;
end;

When I need to change my IP, I need to specify the network interface name, not the adapter name.

How can I list the network connection names like it is listed in Windows Network and Sharing Center.

Example:

Windows 7

Local Area Connection
Wireless Network Connection
Wireless Network Connection 1

Windows 10

Wifi
Wifi 2
etc..
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Master
  • 328
  • 3
  • 10

1 Answers1

3

Win32_NetworkAdapter has an InterfaceIndex property. Read an adapter's InterfaceIndex value and then run another query searching Win32_IP4RouteTable for the same InterfaceIndex value. Win32_IP4RouteTable has Name, Caption, and Description properties.

That being said, Win32_NetworkAdapter is deprecated, mainly because it only supports IPv4. Use MSFT_NetAdapter instead, which supports both IP4 and IPv6, and has InterfaceName and InterfaceDescription properties.

That being said, an alternative option is not to use WMI for this task. You can use GetAdaptersAddresses() instead. It returns an array of IP_ADAPTER_ADDRESSES items, where IP_ADAPTER_ADDRESSES has IfIndex and Ipv6IfIndex fields, which can be used to match up entries in the arrays returned by GetInterfaceInfo(), GetIfTable(), GetIfTable2(), etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    in fact i don't know how to use the other functions you told, i'm new to networking, if you can give me an example code, for now i fixed it by change MainForm.sCombobox1.Items.Add(oNetAdapter.Caption); to MainForm.sCombobox1.Items.Add(oNetAdapter.NetConnectionID); thanks for your detailed answer, i will keep searching and learning – Master Aug 30 '15 at 12:51