2009-03-11

How to query Windows Connection Manager in C++ for Internet connection parameters

Recently I had to create an HTTP connection from an application on Windows Mobile. After a while I figured out that the Connection Manager would be the way to go to determine connection parameters, like proxy settings etc.

You can then use e.g. WinInet functions for the actual data traffic. I will post an example for that later on. The following has been tested on Windows Mobile 5.0 and 6.0.

By the way, I will have to look for a way to get the formatting and syntax highlighting optimized.


int QueryConnMgr()
{
HANDLE hConnMgr = NULL;
BOOL bMgrAvailable = FALSE;
hConnMgr = ConnMgrApiReadyEvent();

if(!hConnMgr)
{ // error handling
return 0;
}
// wait for half a second to allow ConnMgr to get ready
DWORD dwRC = WaitForSingleObject(hConnMgr, 500);
if (WAIT_OBJECT_0 == dwRC)
{
bMgrAvailable = TRUE;
}
if(hConnMgr)
{
CloseHandle(hConnMgr);
}
if(!bMgrAvailable)
{ // error handling
return 0;
}

// create CONNMGR_CONNECTIONINFO structure required for URL mapping
CONNMGR_CONNECTIONINFO ci = {0};
PROXY_CONFIG pcProxy = {0};
DWORD dwIndex = 0;
DWORD dwStatus = 0;
HRESULT hr = S_OK;
HANDLE hOpen = NULL;
HANDLE hConnection = NULL;
ci.cbSize = sizeof(CONNMGR_CONNECTIONINFO);
ci.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP;
ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;

// Attempt to map the URL to a target net
hr = ConnMgrMapURL(TEXT("http://your.server.url"), &(ci.guidDestNet), &dwIndex);
if (S_OK != hr)
{ // error handling
return 0;
}

// make synchronous connection attempt with timeout of 15 seconds
hr = ConnMgrEstablishConnectionSync(&ci, &hConnection, 15000, &dwStatus);
if (S_OK != hr)
{ // error handling
return 0;
}

if (dwStatus != CONNMGR_STATUS_CONNECTED)
{ // makes no sense to continue
return 0;
}

// Get proxy information.
hr = ConnMgrProviderMessage( hConnection, &IID_ConnPrv_IProxyExtension, NULL,
0, 0, (PBYTE)&pcProxy, sizeof(pcProxy));

if (S_OK == hr)
{
dwAccessType = INTERNET_OPEN_TYPE_PROXY;
pszProxy = (LPTSTR) LocalAlloc(LPTR, ARRAYSIZE(pcProxy.szProxyServer));
// Make sure pszProxy was allocated.
hr = StringCchCopyN(pszProxy, ARRAYSIZE(pcProxy.szProxyServer),
pcProxy.szProxyServer, ARRAYSIZE(pcProxy.szProxyServer));
if (S_OK != hr)
{ // reset proxy info on error
pszProxy = NULL;
}
}
else if (E_NOINTERFACE == hr)
{
dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
pszProxy = NULL;

// Reset hr, since it's not really an error here.
hr = S_OK;
}
else
{
// unexpected hr code
return 0;
}

ConnMgrReleaseConnection(hConnection, TRUE);
return 1;
}

6 comments:

  1. Awesome blog post nice quality .bast VPNA good VPN provider will offer servers in a large range of different countries.

    ReplyDelete
  2. Thank you so much for taking the time for you personally to share such a nice info. I truly favor to reading your post. we Protect our Internet Reputation data by Anti Spyware Software. Its very secure for our data. I am also provide Internet Reputation services. its very Needed for our data.

    ReplyDelete
  3. SEO consultant Canada and link building freelancer, servicing ... companies and digital advertising agencies in both Canada and the United ...This Blog is Very Helpful .

    ReplyDelete
  4. Very Good paper! 10/10 I want to thank the writer and the staff support for the work. Thanks a lot.Kansas CIty SEO The fact is that loads of companies claim they do local SEO but only end up hurting your brand in the long run with low quality, black hat methods.

    ReplyDelete
  5. your blog is great full and image is nice use,you use a different type content the topic is very much demand today and everyone wants to read about it thanks you so much.
    ------------
    Web marketing services in Toronto

    ReplyDelete
  6. Very Good paper! 10/10 I want to thank the writer and the staff support for the work. Thanks a lot.

    Grommet Curtains

    ReplyDelete