The last two weeks I had the chance to use the wininet APIs for the first time. They seemed pretty straight-forward to me and I got my code to work. My code would not work whenever there was a proxy - so I was instructed that I needed to use the connection manager to establish the connection. That seems fair, but since I spent some time figuring out why my code was not working, I am writing this entry - hopefully it will be useful to someone else.

The main idea is that you need to use connection manager in order to get the access type and the proxy information, and then you can pass the information to the WININET InternetOpen API.

This is more or less what your code should look like. Once again this code is give as "AS IS", it has not been tested, and it is for instructional purposes only.
Enjoy!

-Luis Cabrera [Microsoft]

#include <wininet.h>
#include <initguid.h>
#include <connmgr.h>
#include <connmgr_proxy.h>
#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))

// pszDestAddr: The URL you would like to connect to: i.e. http://www.microsoft.com
//
extern "C"
HRESULT WINAPI ConnectAndDoSomething(LPTSTR pszDestAddr)
{
   
CONNMGR_CONNECTIONINFO ci = {0};
   
PROXY_CONFIG pcProxy = {0};
   
DWORD dwStatus = 0;
   
DWORD dwIndex = 0;
   
HRESULT hr = S_OK; 
   
HANDLE hConnection = NULL;
   
HANDLE hOpen = NULL;
   
LPTSTR pszProxy = NULL;
   
DWORD dwAccessType;

   // Register with the connection manager
   ci.cbSize = sizeof(CONNMGR_CONNECTIONINFO); 
   
ci.dwParams = CONNMGR_PARAM_GUIDDESTNET; 
   
ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP; 
   
ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;

   // Map the URL to a network, if we can
   hr = ConnMgrMapURL(pszDestAddr, &(ci.guidDestNet), &dwIndex);
   
//Check hr value.

   hr = ConnMgrEstablishConnectionSync(&ci, &hConnection, 25000, &dwStatus); 
   
//Check hr value.

   //Make sure dwStatus == CONNMGR_STATUS_CONNECTED
   // 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));
      
//Check hr value
   
}
   
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;
   
}

   // Open internet connection, and get merchandise information.
   
hOpen = InternetOpen(TEXT("Agent name"),
                        
dwAccessType,
                        
pszProxy,
                        
NULL,0);

   // Do whatever you need to do with the WININET connection here.
   
if (hConnection)
  
{
      
ConnMgrReleaseConnection(hConnection, TRUE);
   
}

    // Do any necessary cleanup.
    
return hr;
}