0

I need to check periodically if an network adapter is present under windows, I tried with GetInterfaceInfo but it doesn't return the "name" like "Local area connection 1" (that I need) but a long ID. I also find that code (here):

bool is_interface_online(std::string interface) {
    struct ifreq ifr;
    int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
    memset(&ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, interface.c_str());
    if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
            perror("SIOCGIFFLAGS");
    }
    close(sock);
    return !!(ifr.ifr_flags & IFF_UP);
}

But I can't port it to work on windows.

I also have a perfect command:

wmic NIC get NetConnectionID

But in c++ it seems that I need to write to a file first to read the result of the command (with ShellExecute), and as I will check maybe every second, I want to avoid disk operations.

Entretoize
  • 2,124
  • 3
  • 23
  • 44
  • Use `GetAdaptersInfo()` or `GetAdaptersAddress()` to enumerate available adapters. Names are one of the (many) fields provided. – Remy Lebeau Oct 24 '20 at 17:36
  • I don't get the "Name" I set myself in windows but I will do with it, thanks. – Entretoize Oct 26 '20 at 08:27
  • [`IP_ADAPTER_INFO::AdapterName`](https://learn.microsoft.com/en-us/windows/win32/api/iptypes/ns-iptypes-ip_adapter_info) and [`IP_ADAPTER_ADDRESSES::AdapterName`](https://learn.microsoft.com/en-us/windows/win32/api/iptypes/ns-iptypes-ip_adapter_addresses_lh) – Remy Lebeau Oct 26 '20 at 14:13
  • No, them give a kind of guid, but after having tried GetAdaptersAddresses I saw that there's a FriendlyName variable, and this is it ! – Entretoize Oct 27 '20 at 15:49

0 Answers0