Click here to Skip to main content
15,875,017 members
Articles / Mobile Apps / Windows Mobile

Managed Wrapper to Connection Manager and How to Bypass the Connection Planner

Rate me:
Please Sign up or sign in to vote.
4.33/5 (11 votes)
24 Sep 2008Public Domain4 min read 69K   1.5K   37   12
This article explains how we can bypass the Connection Planner and establish a connection using the Connection Manager APIs on Windows Mobile devices.

Introduction

This article explains how we can bypass the Connection Planner and establish a connection using the Connection Manager APIs on Windows Mobile devices. A new application block Connection Manager is created which contains a wrapper to the Connection Manager native APIs on Windows Mobile devices. Also, the ConnectionMonitor application block, provided by the Microsoft Patterns & Practices group, is enhanced to retrieve all the connections available on the device.

What is the Connection Manager in Windows Mobile?

Connection Manager, which you can implement by using the Connection Manager API, centralizes and automates the establishment and management of various types of network connections for applications that connect to the Internet or to the corporate network from Windows Mobile devices. When one or more applications request a connection, the Connection Manager establishes a connection using an optimal connection type, and notifies all the applications that the connection is active. Applications are configured to specify a connection name and a network name.

The Connection Manager tracks which connections are in use or are requested by applications. It closes unused connections, automatically disconnects connections when they are idle for a specified period of time, and closes low-priority connections to open high-priority connections. For example, voice connections are typically given a higher priority than data connections.

The Connection Manager handles many different types of connections, including Point-to-Point Protocol (PPP), Remote Access Service (RAS), and Point-to-Point Tunneling Protocol (PPTP) connections. The Connection Manager can also configure proxy server settings to allow network resources through a firewall or a Wireless Application Protocol (WAP) gateway.

ConnectionManager/ms889547.connmgrarchitecture_en-us_MSDN.10_.gif

Fig. Connection Manager Architecture

Connection Monitor Application Block

This application block is provided by the Microsoft Patterns & Practices group. This application block allows your application to retrieve connectivity information from a mobile device and monitor connectivity status changes.

Connection Manager Functions

The Connection Manager API consists of about 11 functions, but you only need two of these functions to know the connection status.

FunctionDescription

ConnMgrEnumDestinations

This function enumerates the available networks.

ConnMgrQueryDetailedStatus

This function returns detailed information about the existing data connections on the Windows Mobile device.

Connection Manager Application Block

This application block provides Managed APIs to establish or release connection from a mobile device. This application block bridges the gap in the managed code which allows the user to manage his connections.

Connection Manager Functions

The Connection Manager API consists of about 11 functions, but you only need six of these functions for the tasks of establishing and releasing a network connection. In many cases, your application may require as few as two of these functions. The following table shows six functions:

FunctionDescription

ConnMgrMapURL

Retrieves the network identifier (Internet or Work) for the specified URL.

ConnMgrEstablishConnection

Selects and establishes the appropriate connection for the specified network identifier. This method returns without waiting for the connection attempt to complete; use ConnMgrConnectionStatus to determine the connection status.

ConnMgrEstablishConnectionSync

Selects and establishes the appropriate connection for the specified network identifier. This method does not return until the connection attempt completes.

ConnMgrReleaseConnection

Releases the specified connection, which may also close the connection.

ConnMgrConnectionStatus

Retrieves the status of the specified connection.

ConnMgrMapConRef

This function maps a connection reference to its corresponding GUID. This function enables you to bypass the Connection Planner by explicitly providing a GUID that will map to the connection.

Using the Code

Display Connection Status

CMConsole is the sample code which shows how a user can use the Connection Monitor and Connection Manager application blocks. This part of the Connection Monitor sample was provided by the Microsoft Patterns & Practices group, and has been enhanced to show how we can use the Connection Manager application block to establish a connection.

C#
public ConsoleForm()
{
      InitializeComponent();
      monitor = ConnectionMonitorFactory.CreateFromConfiguration();
      monitor.ActiveNetworkChanged += NetworkChangeHandler;
}

The console screen shows the different connections, Cell Connection, NIC Connection, and Desktop connection which are defined in the app.config. ConnectionMonitorFactory.CreateFromConfiguration() returns a ConnectionMonitor object which keeps track of the status of the various connections specified in the configuration file.

The ConnectionMonitorFactory object (monitor) retrieves a list of connections to be monitored from the app.config file. Internally, this object registers for SystemProperty for each of these connections. The ActiveNetworkChanged event indicates any one connection status has been changed so that we can update the connection list.

ConnectionManager/CM0.JPG

Fig. Display Connection List

Bypass Connection Planner

In the ConnectionList screen, it is implemented how we can bypass the connection planner in this code sample. It also explains how we can use the Connection Manager application block. ConnectionMonitorNativeHelper.GetConnectionList() retrieves all the available connections on the device. It is displayed in the screen below. We can select a particular connection, say, “MY GPRS Connection”, and select the “Connect” menu in this sample to connect to the selected connection.

Note:- You can create a new GPRS connection on your WM 6.0 device emulator and test it with a Cellular Emulator.

ConnectionManager/CM1.JPG

Fig. List of all the available connections on the mobile device
C#
private void mnuConnect_Click(object sender, EventArgs e)
{
    const int CONNMONITOR_STATUS_CONNECTED = 0x10;
    ConnectionMonitorNativeHelper.CONNMONITOR_CONNECTION_DETAILED_STATUS cnStatus = 
                                  cnList[lstConnection.SelectedIndex];
    if (_connectionHandle != IntPtr.Zero)
    {
    ConnMgrReleaseConnection(_connectionHandle, 0);
    _connectionHandle = IntPtr.Zero;
    }
    if (cnStatus.dwConnectionStatus != CONNMONITOR_STATUS_CONNECTED)
    {
    ConnMgrStatus status = ConnMgrStatus.Unknown;
    Guid forceConnGuid = new Guid();
    if (ConnMgrMapConRef(ConnMgrConRefTypeEnum.ConRefType_NAP, 
        cnStatus.pszDescription, ref forceConnGuid) != 0)
    {
        ConnMgrMapConRef(ConnMgrConRefTypeEnum.ConRefType_PROXY, 
                         cnStatus.pszDescription, ref forceConnGuid);
    }

    ConnMgrConnectionInfo info = new ConnMgrConnectionInfo(forceConnGuid, 
                                 ConnMgrPriority.HighPriorityBackground);
    ConnMgrEstablishConnectionSync(info, ref _connectionHandle, 
                                   _syncConnectTimeout, ref status);

    if (status == ConnMgrStatus.Connected)
        MessageBox.Show("Connect Succeeded");
    else
        MessageBox.Show("Connect failed: " + status.ToString());

    }
}

ConnectionManager/CM2.JPG

Fig. After a connection was successfully made

References

  1. Establishing Network Connectivity with the Windows Mobile Connection Manager
  2. Connection Manager Application Development for Windows Mobile-based Devices
  3. Smart Client Software Factory

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Architect Tata Consultancy Services
India India
Naynesh Shah is a senior solution architect working with the Mobile Solutions - Technology Excellency Group of TATA Consultancy Services.

He specializes in architecting enterprise and embedded mobile solutions based on Windows CE and Windows Mobile. He is also involved in helping customers define the mobile middleware adoption strategy.

Comments and Discussions

 
Bugactive connection / isconnected status bug Pin
Figuerres29-Mar-13 9:57
Figuerres29-Mar-13 9:57 
GeneralMy vote of 5 Pin
hjgode29-Sep-10 18:14
hjgode29-Sep-10 18:14 
GeneralRe: My vote of 5 Pin
hjgode12-Jun-12 7:43
hjgode12-Jun-12 7:43 
Generalchange the link (GPRS->WiFi or WiFi->GPRS) Pin
bsw00617-Mar-10 6:54
bsw00617-Mar-10 6:54 
GeneralRe: change the link (GPRS->WiFi or WiFi->GPRS) Pin
Floppe24-May-12 1:40
Floppe24-May-12 1:40 
General2 parallel connections Pin
Nandiator25-Nov-09 5:51
Nandiator25-Nov-09 5:51 
GeneralUse it in NETCF and select correct AP for Request Pin
sir_zealot@go2.pl16-Nov-09 0:35
sir_zealot@go2.pl16-Nov-09 0:35 
Answergood articl. but the connection cannot be shared with other applications. Pin
E_lwh28-Apr-09 18:09
E_lwh28-Apr-09 18:09 
QuestionUnable to Open CMConsole Pin
Member 434273616-Nov-08 22:25
Member 434273616-Nov-08 22:25 
AnswerRe: Unable to Open CMConsole Pin
RobstaHendricks22-Mar-10 3:48
RobstaHendricks22-Mar-10 3:48 
GeneralThanks for a very interesting article, one comment to help a new user to protect himself for incorrect usage. Pin
scottpj30-Sep-08 4:40
scottpj30-Sep-08 4:40 
GeneralIt's not possible to bypass the Connection Planner if you are using System.Net.HttpWebRequest Pin
Rodrigo Fernandez29-Sep-08 22:00
Rodrigo Fernandez29-Sep-08 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.