How to use C++ to check whether a specific network connection in Win10/11 is set to automatically obtain DNS or manual configuration?

huangapple go评论69阅读模式
英文:

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 &lt;windows.h&gt;
#include &lt;iphlpapi.h&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;string&gt;

#pragma comment(lib, &quot;Iphlpapi.lib&quot;)

std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, IP_ADAPTER_INFO const &amp;info) {
    os &lt;&lt; std::left &lt;&lt; std::setw(40) &lt;&lt; info.Description &lt;&lt; &quot;\t&quot;;
    os &lt;&lt; std::left &lt;&lt; std::setw(16) &lt;&lt; std::setprecision(16) &lt;&lt; info.IpAddressList.IpAddress.String;
	return os &lt;&lt; std::setw(8) &lt;&lt; (info.DhcpEnabled ? &quot;DHCP&quot; : &quot;Manual&quot;);
}

int main() {

    IP_ADAPTER_INFO *info = NULL, *pos;
    DWORD size = 0;

    GetAdaptersInfo(info, &amp;size);

    info = (IP_ADAPTER_INFO *)malloc(size);

    GetAdaptersInfo(info, &amp;size);

    for (pos=info; pos!=NULL; pos=pos-&gt;Next) {
        std::cout &lt;&lt; *pos &lt;&lt; &quot;\n&quot;;
    }

    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

huangapple
  • 本文由 发表于 2023年7月13日 13:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定