找出物理网络适配器

问题描述 投票:0回答:1

使用 PowerShell,我可以通过以下方式找到所有物理网络适配器:

Get-NetAdapter -Physical

Win32 API中有类似的函数吗?

我已经打电话了

GetAdaptersAddresses 

获取有关我安装的网络适配器的信息。 但返回的结构 IP_ADAPTER_ADDRESSES 没有有关物理适配器的信息。 我怎样才能找到它?

winapi network-programming
1个回答
0
投票

您可以使用网络配置接口 API,如下所示:

#include <windows.h>
#include <stdio.h>
#include <Netcfgx.h>
#include <devguid.h>

int main()
{
  INetCfg* cfg = nullptr;
  IEnumNetCfgComponent* components = nullptr;

  // TOTO: HRESULT checks omitted to be added
  auto hr = CoInitialize(nullptr);
  hr = CoCreateInstance(CLSID_CNetCfg, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&cfg));
  hr = cfg->Initialize(nullptr);
  hr = cfg->EnumComponents(&GUID_DEVCLASS_NET, &components);
  do
  {
    INetCfgComponent* component = nullptr;
    hr = components->Next(1, &component, nullptr);
    if (FAILED(hr) || !component)
      break;

    DWORD characteristics = 0;
    component->GetCharacteristics(&characteristics);
    if ((characteristics & NCF_PHYSICAL) == NCF_PHYSICAL)
    {
      LPWSTR name = nullptr;
      component->GetDisplayName(&name);
      wprintf(L"%s 0x%08X\n", name, characteristics);
      if (name)
      {
        CoTaskMemFree(name);
      }
    }
    
    component->Release();

  } while (true);

  hr = cfg->Uninitialize();
  components->Release();
  cfg->Release();

  CoUninitialize();
  return 0;
}

或者您可以将 WMI 与 Win32_NetworkAdapter 类一起使用,因为它具有布尔值

PhysicalAdapter
属性。

© www.soinside.com 2019 - 2024. All rights reserved.