title: Check if Windows Computer on Ethernet Via C++
body: <p>So I have tried looking at a couple of different answers for this. One that I thought may have promise was this one:</p>
  
  <p><a href="https://stackoverflow.com/questions/21680108/how-to-check-network-interface-type-is-ethernet-or-wireless-on-windows-using-qt">How to check network interface type is Ethernet or Wireless on Windows using Qt?</a></p>
  
  <p>However, I don't really know too much about Networking or even about Windows. Personally, I cannot understand most of the Microsoft documentation on their websites. I have tried things like <code>INetworkConnection</code>, <code>NativeWiFi</code>, etc. But either they do not do what I want, or I just cannot figure out how to do it from the available documentation.</p>
  
  <p>With that being said, I would like to use C++ to check if the device this program is being run on is connected to the internet via Ethernet cable. Basically, I want to do the following:</p>
  
  <ul>
  <li>If the computer is connected to Wireless only, run the program</li>
  <li>If the computer is connected to Wired only, don't run the program</li>
  <li>If the computer is connected to both Wired AND Wireless, don't run the program</li>
  </ul>
  
  <p>However, the problem is that I don't know how to check if the device has Ethernet connected. Is there a way to do this? I am NOT using QT. Thank you!</p>
  
  <hr>
  
  <p>EDIT: I should also include what I have tried so far.</p>
  
  <p>I tried using <code>GetAdaptersInfo</code> and getting the <code>Type</code> trait from the <code>PIP_ADAPTER_INFO</code> variable type, but that always gives me <code>Unknown type 71</code> whether I am on Ethernet or not.</p>
  
  <p>The documentation for that <code>GetAdaptersInfo</code> is here:
  <a href="https://msdn.microsoft.com/en-us/library/aa365917%28VS.85%29.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/aa365917%28VS.85%29.aspx</a></p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>EDIT 2: Here is the code I was using for GetAdaptersInfo</p>
  
  <pre><code>bool is_on_ethernet{
      PIP_ADAPTER_INFO pAdapterInfo;
      PIP_ADAPTER_INFO pAdapter = NULL;
      DWORD dwRetVal = 0;
      UINT i;
  
      struct tm newtime;
      char buffer[32];
      errno_t error;
  
      ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
      pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
  
      if(pAdapterInfo == NULL)
          printf("Error allocating memory need to call GetAdaptersInfo");
  
      if(GetAdaptersInfo(pAdapterInfo, &amp;ulOutBufLen) == ERROR_BUFFER_OVERFLOW){
          free(pAdapterInfo);
          pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
      }
  
      if((dwRetVal = GetAdaptersInfo(pAdapterInfo, &amp;ulOutBufLen)) == NO_ERROR){
          pAdapter = pAdapterInfo;
  
          switch(pAdapter-&gt;Type){
              case MIB_IF_TYPE_OTHER:
                  printf("Other\n");
                  return false;
                  break;
              case MIB_IF_TYPE_ETHERNET:
                  printf("Ethernet\h");
                  return true;
                  break;
              case MIB_IF_TYPE_TOKENRING:
                  printf("Token Ring\n");
                  return false;
                  break;
              case MIB_IF_TYPE_FDDI
                  printf("FDDI\n");
                  return false;
                  break;
              case MIB_IF_TYPE_PPP
                  printf("PPP\n");
                  return false;
                  break;
              case MIB_IF_TYPE_LOOPBACK
                  printf("Lookback\n");
                  return false;
                  break;
              case MIB_IF_TYPE_SLIP
                  printf("Slip\n");
                  return false;
                  break;
              default
                  printf("Unknown type %ld\n\n", pAdapter-&gt;Type);
                  return false;
                  break;
          }
      }
  
      if(pAdapterInfo)
          free(pAdapterInfo);
  
      return false;
  }
  </code></pre>