So I have tried looking at a couple of different answers for this. One that I thought may have promise was this one:
How to check network interface type is Ethernet or Wireless on Windows using Qt?
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 INetworkConnection
, NativeWiFi
, etc. But either they do not do what I want, or I just cannot figure out how to do it from the available documentation.
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:
- If the computer is connected to Wireless only, run the program
- If the computer is connected to Wired only, don't run the program
- If the computer is connected to both Wired AND Wireless, don't run the program
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!
EDIT: I should also include what I have tried so far.
I tried using GetAdaptersInfo
and getting the Type
trait from the PIP_ADAPTER_INFO
variable type, but that always gives me Unknown type 71
whether I am on Ethernet or not.
The documentation for that GetAdaptersInfo
is here:
https://msdn.microsoft.com/en-us/library/aa365917%28VS.85%29.aspx
Thanks
EDIT 2: Here is the code I was using for GetAdaptersInfo
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, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW){
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
}
if((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR){
pAdapter = pAdapterInfo;
switch(pAdapter->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->Type);
return false;
break;
}
}
if(pAdapterInfo)
free(pAdapterInfo);
return false;
}