如何列出所有启用的网络连接名称(接口)
How to list all enabled network connection names (Interfaces)
我使用以下 Delphi 过程 GetAdapters
列出所有启用的网络适配器:
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;
当我需要更改 IP 时,我需要指定网络接口名称,而不是适配器名称。
如何列出网络连接名称,就像在 Windows 网络和共享中心中列出的那样。
示例:
Windows 7
Local Area Connection
Wireless Network Connection
Wireless Network Connection 1
Windows 10
Wifi
Wifi 2
etc..
Win32_NetworkAdapter
has an InterfaceIndex
property. Read an adapter's InterfaceIndex
value and then run another query searching Win32_IP4RouteTable
相同的 InterfaceIndex
值。 Win32_IP4RouteTable
具有 Name
、Caption
和 Description
属性。
也就是说,Win32_NetworkAdapter
已弃用,主要是因为它只支持 IPv4。请改用 MSFT_NetAdapter
,它同时支持 IP4 和 IPv6,并且具有 InterfaceName
和 InterfaceDescription
属性。
也就是说,另一种选择是不使用 WMI 来完成此任务。可以使用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()
等
我使用以下 Delphi 过程 GetAdapters
列出所有启用的网络适配器:
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;
当我需要更改 IP 时,我需要指定网络接口名称,而不是适配器名称。
如何列出网络连接名称,就像在 Windows 网络和共享中心中列出的那样。
示例:
Windows 7
Local Area Connection
Wireless Network Connection
Wireless Network Connection 1
Windows 10
Wifi
Wifi 2
etc..
Win32_NetworkAdapter
has an InterfaceIndex
property. Read an adapter's InterfaceIndex
value and then run another query searching Win32_IP4RouteTable
相同的 InterfaceIndex
值。 Win32_IP4RouteTable
具有 Name
、Caption
和 Description
属性。
也就是说,Win32_NetworkAdapter
已弃用,主要是因为它只支持 IPv4。请改用 MSFT_NetAdapter
,它同时支持 IP4 和 IPv6,并且具有 InterfaceName
和 InterfaceDescription
属性。
也就是说,另一种选择是不使用 WMI 来完成此任务。可以使用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()
等