英文:
How to use C++ to check whether a specific network connection in Win10/11 is set to automatically obtain DNS or manual configuration?
问题
如何使用C++来检查在Win10/11中特定的网络连接是否设置为自动获取DNS或手动配置?
谢谢
我尝试过使用Win API [GetInterfaceDnsSettings] 和 [GetAdaptersInfo],但我仍然没有获得所需的结果。
英文:
How to use C++ to check whether a specific network connection in Win10/11 is set to automatically obtain DNS or manual configuration?
Thanks
I have tried using the Win API [GetInterfaceDnsSettings] and [GetAdaptersInfo], but I still haven’t obtained the desired results.
答案1
得分: 1
GetAdaptersInfo
应该可以获取所需的信息。这是一个快速示例:
#include <windows.h>
#include <iphlpapi.h>
#include <iostream>
#include <iomanip>
#include <string>
#pragma comment(lib, "Iphlpapi.lib")
std::ostream &operator<<(std::ostream &os, IP_ADAPTER_INFO const &info) {
os << std::left << std::setw(40) << info.Description << "\t";
os << std::left << std::setw(16) << std::setprecision(16) << info.IpAddressList.IpAddress.String;
return os << std::setw(8) << (info.DhcpEnabled ? "DHCP" : "Manual");
}
int main() {
IP_ADAPTER_INFO *info = NULL, *pos;
DWORD size = 0;
GetAdaptersInfo(info, &size);
info = (IP_ADAPTER_INFO *)malloc(size);
GetAdaptersInfo(info, &size);
for (pos=info; pos!=NULL; pos=pos->Next) {
std::cout << *pos << "\n";
}
free(info);
return 0;
}
至少对我来说,这会产生看起来正确的结果:
Intel(R) Ethernet Connection I217-LM 192.168.1.34 DHCP
VirtualBox Host-Only Ethernet Adapter 192.168.56.1 Manual
VirtualBox Host-Only Ethernet Adapter #2 192.168.96.1 Manual
VirtualBox Host-Only Ethernet Adapter #3 192.168.30.1 Manual
英文:
GetAdaptersInfo
should get you the required information. Here's a quick demo:
#include <windows.h>
#include <iphlpapi.h>
#include <iostream>
#include <iomanip>
#include <string>
#pragma comment(lib, "Iphlpapi.lib")
std::ostream &operator<<(std::ostream &os, IP_ADAPTER_INFO const &info) {
os << std::left << std::setw(40) << info.Description << "\t";
os << std::left << std::setw(16) << std::setprecision(16) << info.IpAddressList.IpAddress.String;
return os << std::setw(8) << (info.DhcpEnabled ? "DHCP" : "Manual");
}
int main() {
IP_ADAPTER_INFO *info = NULL, *pos;
DWORD size = 0;
GetAdaptersInfo(info, &size);
info = (IP_ADAPTER_INFO *)malloc(size);
GetAdaptersInfo(info, &size);
for (pos=info; pos!=NULL; pos=pos->Next) {
std::cout << *pos << "\n";
}
free(info);
return 0;
}
At least for me this produces results that look correct:
Intel(R) Ethernet Connection I217-LM 192.168.1.34 DHCP
VirtualBox Host-Only Ethernet Adapter 192.168.56.1 Manual
VirtualBox Host-Only Ethernet Adapter #2 192.168.96.1 Manual
VirtualBox Host-Only Ethernet Adapter #3 192.168.30.1 Manual
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论