How to get network manager device name in Qt programmatically?

后端 未结 1 1021
日久生厌
日久生厌 2021-01-25 04:39

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

I used QNetworkInterface, but it return

相关标签:
1条回答
  • 2021-01-25 04:57

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

    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).

    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("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
                printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
                printf("\n\n");
                pAdapter = pAdapter->Next;
          }
        }
        return a.exec();
    }
    

    Example output

        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
    
    0 讨论(0)
提交回复
热议问题