VC++ 通过 GetAdaptersInfo 函数获取网卡适配器信息列表

发布时间:2023年12月18日

快速获取系统当前网络设备列表的方法,相对WMI方法,该方法的效率极高,但是获取的信息质量不行,(比较少)但只是对于要做强大网络工具的人来说,如果日常开发,提供的信息是足够用了的。

            typedef enum
            {
                OperationalStatus_Unknown,
                OperationalStatus_Up,
                OperationalStatus_Down,
            } OperationalStatus;

            typedef struct
            {
                ppp::string                             Id;
                ppp::string                             Name;
                ppp::string                             Address;
                ppp::string                             Mask;
                ppp::string                             GatewayServer;
                ppp::string                             DhcpServer;
                ppp::string                             PrimaryWinsServer;
                ppp::string                             SecondaryWinsServer;
                ppp::string                             MacAddress;
                int                                     IfIndex;
                int                                     IfType; // MIB_IF_TYPE
                OperationalStatus                       Status;
            } AdapterInterface;
            bool GetAllAdapterInterfaces(std::vector<AdapterInterfacePtr>& interfaces) noexcept
            {
                ULONG structSize = sizeof(IP_ADAPTER_INFO);
                std::shared_ptr<IP_ADAPTER_INFO> pArray = std::shared_ptr<IP_ADAPTER_INFO>((IP_ADAPTER_INFO*)Malloc(structSize),
                    [](IP_ADAPTER_INFO* p) noexcept
                    {
                        Mfree(p);
                    });

                int err = GetAdaptersInfo(pArray.get(), &structSize);
                if (err == ERROR_BUFFER_OVERFLOW) // ERROR_BUFFER_OVERFLOW == 111
                {
                    // Buffer was too small, reallocate the correct size for the buffer.
                    pArray = std::shared_ptr<IP_ADAPTER_INFO>((IP_ADAPTER_INFO*)Malloc(structSize),
                        [](IP_ADAPTER_INFO* p) noexcept
                        {
                            Mfree(p);
                        });

                    err = GetAdaptersInfo(pArray.get(), &structSize);
                } // if

                std::string any = "0.0.0.0";
                if (err == ERROR_SUCCESS)
                {
                    // Call Succeeded
                    IP_ADAPTER_INFO* pEntry = pArray.get();
                    do
                    {
                        // Retrieve the adapter info from the memory address
                        IP_ADAPTER_INFO& entry = *pEntry;
                        AdapterInterfacePtr interfacex = make_shared_object<AdapterInterface>();
                        interfacex->Id = entry.AdapterName;
                        interfacex->IfIndex = entry.Index;
                        interfacex->Name = entry.Description;
                        interfacex->Address = entry.IpAddressList.IpAddress.String;
                        interfacex->Mask = entry.IpAddressList.IpMask.String;
                        interfacex->GatewayServer = entry.GatewayList.IpAddress.String;
                        interfacex->IfType = entry.Type;
                        interfacex->Status = GetOperationalStatus(entry.Index);
                        interfacex->MacAddress = BytesToMacAddress(entry.Address, (int)entry.AddressLength);

                        interfaces.emplace_back(interfacex);
                        if (entry.DhcpEnabled != 0)
                        {
                            interfacex->DhcpServer = entry.DhcpServer.IpAddress.String;
                        }

                        if (entry.HaveWins)
                        {
                            interfacex->PrimaryWinsServer = entry.PrimaryWinsServer.IpAddress.String;
                            interfacex->SecondaryWinsServer = entry.SecondaryWinsServer.IpAddress.String;
                        }

                        if (interfacex->Address.empty()) interfacex->Address = any;
                        if (interfacex->Mask.empty()) interfacex->Mask = any;
                        if (interfacex->GatewayServer.empty()) interfacex->GatewayServer = any;
                        if (interfacex->DhcpServer.empty()) interfacex->DhcpServer = any;
                        if (interfacex->PrimaryWinsServer.empty()) interfacex->PrimaryWinsServer = any;
                        if (interfacex->SecondaryWinsServer.empty()) interfacex->SecondaryWinsServer = any;

                        // Get next adapter (if any)
                        pEntry = entry.Next;
                    } while (NULL != pEntry);
                }
                return err == ERROR_SUCCESS;
            }
            OperationalStatus GetOperationalStatus(int interface_index) noexcept
            {
                if (interface_index < 0)
                {
                    return OperationalStatus_Unknown;
                }

                MIB_IFROW m;
                m.dwIndex = interface_index;

                DWORD err = GetIfEntry(&m);
                if (err == ERROR_SUCCESS)
                {
                    if (m.dwOperStatus == IF_OPER_STATUS_OPERATIONAL)
                    {
                        return OperationalStatus_Up;
                    }
                    return OperationalStatus_Down;
                }
                return OperationalStatus_Unknown;
            }
            std::string BytesToMacAddress(const void* data, int size) noexcept
            {
                if ((size < 1) || (NULL != data && size < 1))
                {
                    data = NULL;
                    size = 0;
                }

                // Set default MAC address
                unsigned char default_byte_arr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                int num_of_bytes_to_copy = (size <= 6) ? size : 6;
                memcpy(default_byte_arr, data, num_of_bytes_to_copy);

                char mac_str[18];
                sprintf(mac_str, "%02X:%02X:%02X:%02X:%02X:%02X",
                    default_byte_arr[0], default_byte_arr[1], default_byte_arr[2],
                    default_byte_arr[3], default_byte_arr[4], default_byte_arr[5]);
                return mac_str;
            }

            std::shared_ptr<MIB_IFTABLE> GetIfTable() noexcept
            {
                DWORD dwSize = 0;
                if (::GetIfTable(NULL, &dwSize, FALSE) != ERROR_INSUFFICIENT_BUFFER)
                {
                    return NULL;
                }

                std::shared_ptr<MIB_IFTABLE> pIfTable = std::shared_ptr<MIB_IFTABLE>((MIB_IFTABLE*)Malloc(dwSize),
                    [](MIB_IFTABLE* p) noexcept
                    {
                        Mfree(p);
                    });
                if (pIfTable == NULL)
                {
                    return NULL;
                }

                if (::GetIfTable(pIfTable.get(), &dwSize, FALSE) != NO_ERROR)
                {
                    return NULL;
                }

                return pIfTable;
            }

            std::shared_ptr<MIB_IFROW> GetIfEntry(int interface_index) noexcept
            {
                if (interface_index < 0)
                {
                    return NULL;
                }

                std::shared_ptr<MIB_IFROW> pIfRow = std::shared_ptr<MIB_IFROW>((MIB_IFROW*)Malloc(sizeof(MIB_IFROW)),
                    [](MIB_IFROW* p) noexcept
                    {
                        Mfree(p);
                    });
                if (NULL == pIfRow)
                {
                    return NULL;
                }

                pIfRow->dwIndex = interface_index;
                if (::GetIfEntry(pIfRow.get()) != NO_ERROR)
                {
                    return NULL;
                }

                return pIfRow;
            }

文章来源:https://blog.csdn.net/liulilittle/article/details/135067834
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。