如何以编程方式在 Qt 中获取网络管理器设备名称?

2022-01-19 00:00:00 qt network-programming c++

有没有可能在windows中使用Qt/C++获取网络适配器设备名称(网络适配器描述)?

is there any possibility to get the network adapter device name (network adapter description) using Qt / C++ in windows?

我使用了 QNetworkInterface,但它只返回适配器名称.我想知道哪个适配器是 USB 上的以太网.

I used QNetworkInterface, but it return the adapter name only. I want to know which adapter is Ethernet over USB.

QNetworkInterface interface;
QList<QNetworkInterface> IpList = interface.allInterfaces();
for (int i = 0; i < IpList.size(); i++)
    qDebug() << "Interface " << i << ":" << IpList.at(i).humanReadableName();

输出:

推荐答案

是否有可能获得网络适配器设备名称(网络适配器说明)在windows中使用Qt/C++

is there any possibility to get the network adapter device name (network adapter description) using Qt / C++ in windows

答案是否定的.Qt 没有任何功能来获取设备名称(描述).QNetworkInterface 只能获取接口名称和硬件地址(IP).

The answer is no. Qt does not have any functionality to get the device name (description). QNetworkInterface can only obtain the interface name and hardware address (IP).

在 Windows 上,您可以使用这个小代码示例.pAdapter->Description 应该包含您要查找的值.

On Windows you can use this small code example. pAdapter->Description should hold the value you are looking for.

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <QCoreApplication>
#pragma comment(lib, "IPHLPAPI.lib")

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    PIP_ADAPTER_INFO pAdapterInfo;
    pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO));
    ULONG buflen = sizeof(IP_ADAPTER_INFO);

    if(GetAdaptersInfo(pAdapterInfo, &buflen) == ERROR_BUFFER_OVERFLOW) {
      free(pAdapterInfo);
      pAdapterInfo = (IP_ADAPTER_INFO *) malloc(buflen);
    }

    if(GetAdaptersInfo(pAdapterInfo, &buflen) == NO_ERROR) {
        PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
        while (pAdapter) {
            printf("	Adapter Name: 	%s
", pAdapter->AdapterName);
            printf("	Adapter Desc: 	%s
", pAdapter->Description);
            printf("

");
            pAdapter = pAdapter->Next;
      }
    }
    return a.exec();
}

示例输出

    Adapter Name:   {DF6051AF-8F8F-4AA8-94A9-34656236F101}
    Adapter Desc:   VMware Virtual Ethernet Adapter for VMnet1


    Adapter Name:   {13C8DF49-6D60-4702-9B3A-688B2E372E42}
    Adapter Desc:   TAP-Windows Adapter V9


    Adapter Name:   {42635D10-33A3-4FE9-96BA-1071808B6E2B}
    Adapter Desc:   Realtek PCIe GBE Family Controller


    Adapter Name:   {AA62E2BA-D140-4C2C-A1B5-58082ED21E00}
    Adapter Desc:   1 x 1 11b/g/n Wireless LAN PCI Express Half Mini Card-Ad apter


    Adapter Name:   {7AE540D3-69FE-4BEE-A5CA-482CAF06DAB8}
    Adapter Desc:   VMware Virtual Ethernet Adapter for VMnet8

相关文章