查询与网络适配器关联的Windows网络配置文件
获取Windows网络适配器关联的网络配置文件
GetAdaptersAddresses()确实只能获取适配器的硬件、IP地址等基础信息,无法直接返回关联的网络配置文件。以下是几种可行的解决方案:
1. Windows Native API(C/C++)
如果需要通过代码实现,可结合WLAN API(针对无线适配器)或NetCfg API(针对有线适配器)来关联适配器与配置文件:
无线适配器示例代码
#include <wlanapi.h> #include <windows.h> #include <stdio.h> #pragma comment(lib, "wlanapi.lib") int main() { DWORD dwError = 0; HANDLE hClient = NULL; DWORD dwNegotiatedVersion = 0; // 初始化WLAN服务句柄 dwError = WlanOpenHandle(2, NULL, &dwNegotiatedVersion, &hClient); if (dwError != ERROR_SUCCESS) { printf("WlanOpenHandle failed: %d\n", dwError); return 1; } // 枚举所有WLAN适配器 PWLAN_INTERFACE_INFO_LIST pIfList = NULL; dwError = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwError != ERROR_SUCCESS) { printf("WlanEnumInterfaces failed: %d\n", dwError); WlanCloseHandle(hClient, NULL); return 1; } // 遍历适配器,获取关联配置文件 for (DWORD i = 0; i < pIfList->dwNumberOfItems; i++) { PWLAN_INTERFACE_INFO pIfInfo = &pIfList->InterfaceInfo[i]; printf("适配器GUID: %ws\n", pIfInfo->strInterfaceGuid); PWLAN_PROFILE_INFO_LIST pProfileList = NULL; dwError = WlanGetProfileList(hClient, &pIfInfo->InterfaceGuid, NULL, &pProfileList); if (dwError != ERROR_SUCCESS) { printf("WlanGetProfileList failed: %d\n", dwError); continue; } printf("关联的配置文件:\n"); for (DWORD j = 0; j < pProfileList->dwNumberOfItems; j++) { PWLAN_PROFILE_INFO pProfileInfo = &pProfileList->ProfileInfo[j]; printf("- %ws\n", pProfileInfo->strProfileName); } WlanFreeMemory(pProfileList); } // 释放资源 WlanFreeMemory(pIfList); WlanCloseHandle(hClient, NULL); return 0; }
这段代码会枚举所有无线适配器,并输出每个适配器关联的配置文件名称。对于有线以太网适配器,可使用NetCfg API(netcfgx.h)通过INetCfg接口枚举网络组件,关联适配器与对应的配置文件。
2. 命令行工具(Netsh)
无需编码的话,使用netsh命令可快速查看关联信息:
- 查看当前连接的无线适配器配置文件:
netsh wlan show interfaces
- 查看指定无线适配器的所有配置文件:
netsh wlan show profiles interface="Wi-Fi"
- 查看有线适配器的网络配置(对应配置文件信息):
netsh interface ip show config name="以太网"
3. PowerShell
PowerShell提供更简洁的方式获取适配器与配置文件的关联:
- 列出所有网络连接的配置文件及对应适配器:
Get-NetConnectionProfile | Select-Object InterfaceAlias, Name, NetworkCategory
- 获取特定适配器的配置文件:
Get-NetAdapter -Name "Wi-Fi" | Get-NetConnectionProfile
内容的提问来源于stack exchange,提问作者user3664223
相关产品推荐
相关产品推荐

