Thursday, May 5, 2011

CInternetSession::OpenURL on Windows Mobile causes error 12029 (cannot connect)

I'm trying to access data using HTTP by calling CInternetSession::OpenUrl on Windows Mobile 5 (coding in C++ with MFC). I always get an exception with error code 12029 (cannot connect).

I suspect that I need to use the Connection Manager API to create a connection first. Can someone confirm that?

I am going to try coding it up, based on information here (http://msdn.microsoft.com/en-us/magazine/dd263096.aspx), and I'll report my experiences as an answer if appropriate. It would be nice to get other input too.

I have successfully opened a connection using this code:

// Find out which type of connection is needed for this URL.
GUID guid;
HRESULT hresult = ConnMgrMapURL((LPCTSTR)url,&guid,NULL);
if (!SUCCEEDED(hresult))
{
delete [] url;
aError = CartoType::KErrorInternetIo;
return NULL;
}

// Get a connection.
CONNMGR_CONNECTIONINFO cinfo;
memset(&cinfo,0,sizeof(cinfo));
cinfo.cbSize = sizeof(cinfo);
cinfo.bDisabled = FALSE;
cinfo.bExclusive = FALSE;
cinfo.guidDestNet = guid;
cinfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
cinfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
cinfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
DWORD status;
hresult = ConnMgrEstablishConnectionSync(&cinfo,&iConnectionHandle,15000,&status);

and I know that it worked because it sets status to CONNMGR_STATUS_CONNECTED; nevertheless, I call CInternetSession::OpenURL immediately afterwards and it throws an exception.

From stackoverflow
  • Here's some code that works. It uses the lower-level Windows API, not MFC. Perhaps it's not ideal and contains redundancies (do I really need the ConnMgr calls?), but it does work:

    // Find out which type of connection is needed for this URL.
    GUID guid;
    HRESULT hresult = ConnMgrMapURL((LPCTSTR)url,&guid,NULL);
    if (!SUCCEEDED(hresult))
     {
     delete [] url;
     aError = CartoType::KErrorInternetIo;
     return NULL;
     }
    
    // Get a connection.
    CONNMGR_CONNECTIONINFO cinfo;
    memset(&cinfo,0,sizeof(cinfo));
    cinfo.cbSize = sizeof(cinfo);
    cinfo.bDisabled = FALSE;
    cinfo.bExclusive = FALSE;
    cinfo.guidDestNet = guid;
    cinfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
    cinfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
    cinfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
    DWORD status;
    hresult = ConnMgrEstablishConnectionSync(&cinfo,&iConnectionHandle,15000,&status);
    
    HINTERNET hinternet = InternetOpen(_T("CartoType"),INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    HINTERNET hfile = InternetOpenUrl(hinternet,(LPCTSTR)url,NULL,0,0,1);
    

    This returns a valid handle that I can read using InternetReadFile, then close using InternetCloseHandle.

    Shane Powell : You don't need the ConnMgr if there is already a connection available (Wifi or ActiveSync). Where the ConnMgr calls do come in useful is if you wish to create a cellular data connection if no connection exists currently (e.g. 3G) or if you want to create a VPN connection.

0 comments:

Post a Comment