Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 24 Sep 2008
Rate this:
Please Sign up or sign in to vote.
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.

Function Description

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:

Function Description

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.

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
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

Share

About the Author

Naynesh Shah
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 PinmemberFiguerres29-Mar-13 9:57 
GeneralMy vote of 5 Pinmemberhjgode29-Sep-10 18:14 
GeneralRe: My vote of 5 Pinmemberhjgode12-Jun-12 7:43 
Generalchange the link (GPRS->WiFi or WiFi->GPRS) Pinmemberbsw00617-Mar-10 6:54 
GeneralRe: change the link (GPRS->WiFi or WiFi->GPRS) PinmemberFloppe24-May-12 1:40 
General2 parallel connections PinmemberNandiator25-Nov-09 5:51 
GeneralUse it in NETCF and select correct AP for Request Pinmembersir_zealot@go2.pl16-Nov-09 0:35 
Hello.
I am using System.Net.HttpWebRequest and it seams that it is possible to bypass the Connection Planer only if request is made right after ConnMgrMapRef.
 
I have connected to my site witch shows ip of request. I was able to make requests on specific access points if I added MyRequest() after line ConnMgrMapRef(....) in mnuConnect_Click event;
 
But calling MyRequest in other event caused connection always through WiFi.
 
*Why request is different if MyRequest method is used in other events.
*Is this method (do request right after ConnMgrMapRef) reliable.
 
I must have a way to determine witch AP will be used on requests.
 
Thanks for any replay.
 
PS. Great article Smile | :)
Answergood articl. but the connection cannot be shared with other applications. PinmemberE_lwh28-Apr-09 18:09 
QuestionUnable to Open CMConsole PinmemberMember 434273616-Nov-08 22:25 
AnswerRe: Unable to Open CMConsole PinmemberRobstaHendricks22-Mar-10 3:48 
GeneralThanks for a very interesting article, one comment to help a new user to protect himself for incorrect usage. PinmemberMember 460340530-Sep-08 4:40 
GeneralIt's not possible to bypass the Connection Planner if you are using System.Net.HttpWebRequest PinmemberRodrigo Fernandez29-Sep-08 22:00 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 24 Sep 2008
Article Copyright 2008 by Naynesh Shah
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid