{"id":107285,"date":"2022-10-13T07:00:00","date_gmt":"2022-10-13T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107285"},"modified":"2022-10-13T08:22:46","modified_gmt":"2022-10-13T15:22:46","slug":"20221013-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221013-00\/?p=107285","title":{"rendered":"How can I check whether the user&#8217;s network connection is roaming or metered?"},"content":{"rendered":"<p>There are few ways of doing this.<\/p>\n<p>The classic Win32 way is to call <code>Get\u00adNetwork\u00adConnectivity\u00adHint<\/code>:<\/p>\n<pre>#include &lt;iphlpapi.h&gt;\r\n\r\nNL_NETWORK_CONNECTIVITY_HINT connectivityHint{};\r\nauto error = GetNetworkConnectivityHint(&amp;connectivityHint);\r\nif (error != NO_ERROR) { \/* handle the error somehow *\/ }\r\n<\/pre>\n<p>The <code>NL_<wbr \/>NETWORK_<wbr \/>CONNECTIVITY_<wbr \/>HINT<\/code> contains information about the type of network you&#8217;re on. We&#8217;ll come back to this structure later.<\/p>\n<p>The Windows Runtime way is a little different.<\/p>\n<pre>#include &lt;winrt\/Windows.Networking.Connectivity.h&gt;\r\n\r\nauto connection = winrt::Windows::Networking::Connectivity::\r\n        NetworkInformation::GetInternetConnectionProfile();\r\nif (!connection) { \/* no internet connectivity *\/ }\r\nauto cost = connection.GetConnectionCost();\r\n<\/pre>\n<p>The <code>NL_<wbr \/>NETWORK_<wbr \/>CONNECTIVITY_<wbr \/>HINT<\/code> and the <code>Connection\u00adCost<\/code> provide roughly the same information.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th><code>NL_<wbr \/>NETWORK_<wbr \/>CONNECTIVITY_<wbr \/>HINT<\/code><\/th>\n<th><code>Connection\u00adCost<\/code><\/th>\n<\/tr>\n<tr>\n<td><code>Connectivity\u00adLevel<\/code><\/td>\n<td>N\/A<\/td>\n<\/tr>\n<tr>\n<td><code>Connectivity\u00adCost<\/code><\/td>\n<td><code>Network\u00adCost\u00adType<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Approaching\u00adData\u00adLimit<\/code><\/td>\n<td><code>Approaching\u00adData\u00adLimit<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Over\u00adData\u00adLimit<\/code><\/td>\n<td><code>Over\u00adData\u00adLimit<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>Roaming<\/code><\/td>\n<td><code>Roaming<\/code><\/td>\n<\/tr>\n<tr>\n<td>N\/A<\/td>\n<td><code>Background\u00adData\u00adUsage\u00adRestricted<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <code>Connectivity\u00adLevel<\/code> is N\/A for <code>Connection\u00adCost<\/code> because the internet connection profile by definition has internet connectivity, so there&#8217;s no need for it to tell you.<\/p>\n<p>The <code>NL_<wbr \/>NETWORK_<wbr \/>CONNECTIVITY_<wbr \/>HINT<\/code> does not tell you whether background data usage is restricted.<\/p>\n<p>The <code>Connectivity\u00adCost<\/code> \/ <code>Network\u00adCost\u00adType<\/code> tells you how much the network costs.<\/p>\n<ul>\n<li>Unknown: No information available<\/li>\n<li>Unrestricted: Unlimited<\/li>\n<li>Fixed: Can use up to a fixed limit<\/li>\n<li>Variable: Pay by usage<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The classic way and the Windows Runtime way.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-107285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The classic way and the Windows Runtime way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=107285"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107285\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=107285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}