Establishing an Internet Connection Using Connection Manager (Windows Embedded CE 6.0)

1/6/2010

This topic describes how to establish an Internet connection by using Connection Manager.

Before performing the following procedures, complete the steps in Making a Data Connection Using Connection Manager.

To establish an Internet connection

  1. In the source code for your application, use a constant to represent the destination network that you want to connect to. For example, for "the Internet," you can use IID_DestNetInternet, which is defined in ConnMgr.h.

  2. Create a BOOL value called IsConnected that is set when the application is successfully connected, and is cleared when the application is disconnected.

  3. Create a CONNMGR_CONNECTIONINFO structure. Make sure that you set the guidDestNet member to the GUID value for your connection. Also pass in a handle to the application main window that receives status notifications in the hWnd member.

  4. Create a connection request either asynchronously or synchronously.

  5. When the connection succeeds, set IsConnected to true.

    All application code that requires connectivity must execute on the condition that IsConnected is true.

Example

The following code example asynchronously requests a connection from Connection Manager. When the application is connected, it executes code that connects to the Internet.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include <windows.h>
#include "dubinet.h"

#define INITGUID

#include <connmgr.h>

#define INTERNET_BUFFER_SIZE  10*1024
BOOL IsConnected;

void DoInternetReadFile(HINTERNET hFile)
{
    DWORD dwRead = 0;
    char  pBuf[INTERNET_BUFFER_SIZE + 1];
    TCHAR szBuf[INTERNET_BUFFER_SIZE + 1];

    while(InternetReadFile(hFile, pBuf, sizeof(pBuf), &dwRead))
    {
        if(!dwRead)
        {
            break;
        }

        pBuf[dwRead]                = 0;
        pBuf[INTERNET_BUFFER_SIZE]  = 0;
        szBuf[INTERNET_BUFFER_SIZE] = 0;

        MultiByteToWideChar(
            CP_ACP, 
            0,
            pBuf, 
            -1, 
            szBuf, 
            INTERNET_BUFFER_SIZE 
            );
        RETAILMSG(1, ( TEXT("InternetReadFile() read %d bytes => %s \r\n"), dwRead, szBuf));
    }
}

void DoInternetTransaction()
{
    HINTERNET hInet = InternetOpen(
        L"bttest", 
        INTERNET_OPEN_TYPE_DIRECT, 
        0,
        0, 
        0 
        );
    
    HINTERNET hConnect = InternetConnect( 
        hInet, 
        L"m.live.com", 
        INTERNET_DEFAULT_HTTP_PORT, 
        NULL, 
        NULL, 
        INTERNET_SERVICE_HTTP, 
        0, 
        0 
        );

    if(!hConnect)
    {
        return;
    }
    
    HINTERNET hRequest = HttpOpenRequest( 
        hConnect, 
        L"GET", 
        L"", 
        NULL, 
        NULL, 
        NULL, 
        INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 
        0 
        );
        
    if(!hRequest)
    {
        return;
    }
    
    BOOL bRequestOkay = HttpSendRequest( 
        hRequest, 
        NULL, 
        0, 
        NULL, 
        0 
        );
    if(bRequestOkay)
    {
        DoInternetReadFile( hRequest );
    }   

    InternetCloseHandle (hRequest);
    InternetCloseHandle (hConnect);
    InternetCloseHandle (hInet);
}

BOOL EstablishConnection()
{
    CONNMGR_CONNECTIONINFO  ci = {0};
    HANDLE ConnMgrConnection;
    
    ci.cbSize     = sizeof(ci);
    ci.dwParams   = CONNMGR_PARAM_GUIDDESTNET;
    ci.dwFlags    = CONNMGR_FLAG_SUSPEND_AWARE;
    ci.dwPriority = CONNMGR_PRIORITY_USERBACKGROUND;
    ci.bExclusive = FALSE;

    ci.guidDestNet = IID_DestNetInternet;
    
    DWORD ConnectionStatus;
    HRESULT hr = ConnMgrEstablishConnectionSync(
        &ci,
        &ConnMgrConnection,
        500000,            
        &ConnectionStatus
        );

    if(FAILED(hr) || !ConnMgrConnection)
    {
        return FALSE;
    }        
    IsConnected = true;
    return TRUE;
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
    EstablishConnection();
     if (IsConnected)
     {
           DoInternetTransaction();
     }
}

See Also

Concepts

Connection Manager How-to Topics

Other Resources

Connection Manager Application Development