{"id":261,"date":"2008-04-29T23:53:47","date_gmt":"2008-04-29T23:53:47","guid":{"rendered":"http:\/\/dalelane.co.uk\/blog\/?p=261"},"modified":"2008-04-30T12:33:20","modified_gmt":"2008-04-30T12:33:20","slug":"programmatically-making-an-internet-connection-in-windows-mobile-in-c","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=261","title":{"rendered":"Programmatically making an Internet connection in Windows Mobile in C++"},"content":{"rendered":"<p>Although a lot of people seem to be finding my <a href=\"http:\/\/dalelane.co.uk\/page.php?id=1047\">Twitter widget for Windows Mobile<\/a> useful, it seems that there are also <a href=\"http:\/\/dalelane.co.uk\/blog\/?p=244\">a few people who noticed that it was hacked together in a few hours overnight<\/a>!<\/p>\n<p>One of the more noticed issues was the fact that the widget reused the mobile&#8217;s existing Internet connection. <\/p>\n<p>It was described in emails such as:<\/p>\n<blockquote><p>Some times I have to invoke a data session with PIE or another networked app before it will let me send a twit. Anyway to make it start it&#8217;s own network session if one doesn&#8217;t already exist?<\/p><\/blockquote>\n<p>and in tweets such as <\/p>\n<p><a href=\"http:\/\/twitter.com\/kenfoldsfive\/statuses\/790095281\" target=\"_blank\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/i267.photobucket.com\/albums\/ii311\/dale_lane\/080429-tweet-ken1.gif\" alt=\"ooh i'm liking cetwit. But not you, twittoday\"\/><\/a><\/p>\n<p><a href=\"http:\/\/twitter.com\/kenfoldsfive\/statuses\/790096215\" target=\"_blank\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/i267.photobucket.com\/albums\/ii311\/dale_lane\/080429-tweet-ken2.gif\" alt=\"oh and a side note to data apps everywhere: if you want a connection, REQUEST IT YOURSELF. I have better things to do than holding your hand\"\/><\/a><\/p>\n<p>The issue is that when you use the web services APIs, this is all handled for you. But I rolled my own HTTP POST code using the wininet API. And these low-level calls aren&#8217;t so helpful.<\/p>\n<p>It wasn&#8217;t a problem for me, because my phone is always connected anyway. But enough people have mentioned it, so I figured it was worth looking into!<\/p>\n<p>If anyone is interested in how you start a connection programmatically in C++, read on.<\/p>\n<p><!--more--><\/p>\n<pre style=\"border: thin solid silver; background-color: #eeeeee; padding: 0.7em; font-size: 1.1em; overflow: auto;\">\r\n#include \"initguid.h\"\r\n#include \"connmgr.h\"\r\n\r\n...\r\n\r\nBOOL ConnectToNetwork(int timeoutsecs)\r\n{\r\n  \/\/ handle to connection to start\r\n  HANDLE hConnection = NULL;\r\n\r\n  \/\/ stores return value identifying status of connection attempt\r\n  DWORD dwStatus;\r\n\r\n  \/\/ initialise connection info structure\r\n  CONNMGR_CONNECTIONINFO pConnectionInfo;\r\n  ZeroMemory(&pConnectionInfo, sizeof(CONNMGR_CONNECTIONINFO));\r\n   \r\n  \/\/ set structure size\r\n  pConnectionInfo.cbSize = sizeof(CONNMGR_CONNECTIONINFO);\r\n\r\n  \/\/ set priority to identify that a user initiated this request\r\n  \/\/ and the GUI is waiting on the creation of the connection\r\n  pConnectionInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;\r\n\r\n  \/\/ identify the network to connect to\r\n  pConnectionInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;\r\n  pConnectionInfo.guidDestNet = IID_DestNetInternet;\r\n\r\n  \/\/ specify that other applications can use this connection\r\n  pConnectionInfo.bExclusive = FALSE;\r\n\r\n  \/\/ specify that a connection should be made\r\n  pConnectionInfo.bDisabled = FALSE;\r\n\r\n  \/\/ request connection\r\n  HRESULT hr = ConnMgrEstablishConnectionSync(&pConnectionInfo, \r\n                                              &hConnection,\r\n                                              timeoutsecs * 1000, \r\n                                              &dwStatus);\r\n\r\n  if (hr == S_OK)\r\n  {\r\n    return TRUE;\r\n  }\r\n  else \r\n  {\r\n    switch (dwStatus)\r\n    {\r\n    case CONNMGR_STATUS_DISCONNECTED:\r\n      MessageBox(NULL,TEXT(\"The connection has been disconnected\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    case CONNMGR_STATUS_WAITINGFORPATH:\r\n      MessageBox(NULL,TEXT(\"A path to the destination exists but is not presently available\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    case CONNMGR_STATUS_WAITINGFORRESOURCE:\r\n      MessageBox(NULL,TEXT(\"Another client is using resources that this connection requires\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    case CONNMGR_STATUS_WAITINGFORPHONE:\r\n      MessageBox(NULL,TEXT(\"Connection cannot be made while call in progress\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    case CONNMGR_STATUS_NOPATHTODESTINATION:\r\n      MessageBox(NULL,TEXT(\"No path to the destination could be found\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    case CONNMGR_STATUS_CONNECTIONFAILED:\r\n      MessageBox(NULL,TEXT(\"The connection failed and cannot be reestablished\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    case CONNMGR_STATUS_CONNECTIONCANCELED:\r\n      MessageBox(NULL,TEXT(\"The user aborted the connection\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;    \r\n    case CONNMGR_STATUS_WAITINGCONNECTION:\r\n      MessageBox(NULL,TEXT(\"The device is attempting to connect\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;          \r\n    case CONNMGR_STATUS_WAITINGCONNECTIONABORT:\r\n      MessageBox(NULL,TEXT(\"The device is aborting the connection attempt\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;          \r\n    case CONNMGR_STATUS_WAITINGDISCONNECTION:\r\n      MessageBox(NULL,TEXT(\"The connection is being brought down\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    default:\r\n      MessageBox(NULL,TEXT(\"The connection attempt failed\"),TEXT(\"Connection Manager\"),MB_ICONERROR);\r\n      break;\r\n    }\r\n    return FALSE;\r\n  }\r\n}<\/pre>\n<p>There is still more that needs to be done to TwitToday before I can really call it a finished app&#8230; but it&#8217;s getting closer! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although a lot of people seem to be finding my Twitter widget for Windows Mobile useful, it seems that there are also a few people who noticed that it was hacked together in a few hours overnight! One of the more noticed issues was the fact that the widget reused the mobile&#8217;s existing Internet connection. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[36,187,188,151,19,189],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-code","tag-c","tag-connection-manager","tag-connmgrestablishconnection","tag-twitter","tag-windows-mobile","tag-wininet"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}