How can I check whether the user’s network connection is roaming or metered?

Raymond Chen

There are few ways of doing this.

The classic Win32 way is to call Get­Network­Connectivity­Hint:

#include <iphlpapi.h>

NL_NETWORK_CONNECTIVITY_HINT connectivityHint{};
auto error = GetNetworkConnectivityHint(&connectivityHint);
if (error != NO_ERROR) { /* handle the error somehow */ }

The NL_NETWORK_CONNECTIVITY_HINT contains information about the type of network you’re on. We’ll come back to this structure later.

The Windows Runtime way is a little different.

#include <winrt/Windows.Networking.Connectivity.h>

auto connection = winrt::Windows::Networking::Connectivity::
        NetworkInformation::GetInternetConnectionProfile();
if (!connection) { /* no internet connectivity */ }
auto cost = connection.GetConnectionCost();

The NL_NETWORK_CONNECTIVITY_HINT and the Connection­Cost provide roughly the same information.

NL_NETWORK_CONNECTIVITY_HINT Connection­Cost
Connectivity­Level N/A
Connectivity­Cost Network­Cost­Type
Approaching­Data­Limit Approaching­Data­Limit
Over­Data­Limit Over­Data­Limit
Roaming Roaming
N/A Background­Data­Usage­Restricted

The Connectivity­Level is N/A for Connection­Cost because the internet connection profile by definition has internet connectivity, so there’s no need for it to tell you.

The NL_NETWORK_CONNECTIVITY_HINT does not tell you whether background data usage is restricted.

The Connectivity­Cost / Network­Cost­Type tells you how much the network costs.

  • Unknown: No information available
  • Unrestricted: Unlimited
  • Fixed: Can use up to a fixed limit
  • Variable: Pay by usage