October 13th, 2022

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

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
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Jonathan Duncan

    How does windows find out the information to implement these APIs? Is there some protocol for ISPs to communicate their billing policy ?