Click here to Skip to main content
15,879,184 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  
This article explains how we can bypass the Connection Planner and establish a connection using the Connection Manager APIs on Windows Mobile devices.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; 


namespace Microsoft.Practices.Mobile.ConnectionManager
{
    public class ConnectionManager
    {
        #region "Native Connection Manager APIs"
        [DllImport("CellCore.dll", EntryPoint = "ConnMgrMapURL", SetLastError = true)]
        public static extern int ConnMgrMapURL(string url, ref Guid networkGuid, int passZero);
        
        [DllImport("CellCore.dll", EntryPoint = "ConnMgrEstablishConnection", SetLastError = true)]
        public static extern int ConnMgrEstablishConnection(ConnMgrConnectionInfo connectionInfo, ref IntPtr connectionHandle);

        [DllImport("CellCore.dll", EntryPoint = "ConnMgrEstablishConnectionSync", SetLastError = true)]
        public static extern int ConnMgrEstablishConnectionSync(ConnMgrConnectionInfo connectionInfo, ref IntPtr connectionHandle, uint dwTimeout, ref ConnMgrStatus dwStatus);

        [DllImport("CellCore.dll", EntryPoint = "ConnMgrReleaseConnection", SetLastError = true)]
        public static extern int ConnMgrReleaseConnection(IntPtr connectionHandle, int cache);

        [DllImport("CellCore.dll", EntryPoint = "ConnMgrConnectionStatus", SetLastError = true)]
        public static extern int ConnMgrConnectionStatus(IntPtr connectionHandle, ref ConnMgrStatus status);

        [DllImport("CellCore.dll", EntryPoint = "ConnMgrMapConRef", SetLastError = true)]
        public static extern int ConnMgrMapConRef(ConnMgrConRefTypeEnum e, string szConRef, ref Guid pGUID);

        #endregion

        #region "Constants & Structures"

        [Flags]
        public enum ConnMgrParam : int
        {
            GuidDestNet = 0x1,
            MaxCost = 0x2,
            MinRcvBw = 0x4,
            MaxConnLatency = 0x8
        }

        [Flags]
        public enum ConnMgrProxy : int
        {
            NoProxy = 0x0,
            Http = 0x1,
            Wap = 0x2,
            Socks4 = 0x4,
            Socks5 = 0x8
        }

        public enum ConnMgrPriority
        {
            UserInteractive = 0x8000,
            HighPriorityBackground = 0x0200,
            LowPriorityBackground = 0x0008
        }

        public enum ConnMgrStatus
        {
            Unknown = 0x00,
            Connected = 0x10,
            Suspended = 0x11,
            Disconnected = 0x20,
            ConnectionFailed = 0x21,
            ConnectionCanceled = 0x22,
            ConnectionDisabled = 0x23,
            NoPathToDestination = 0x24,
            WaitingForPath = 0x25,
            WaitingForPhone = 0x26,
            PhoneOff = 0x27,
            ExclusiveConflict = 0x28,
            NoResources = 0x29,
            ConnectionLinkFailed = 0x2a,
            AuthenticationFailed = 0x2b,
            NoPathWithProperty = 0x2c,
            WaitingConnection = 0x40,
            WaitingForResource = 0x41,
            WaitingForNetwork = 0x42,
            WaitingDisconnection = 0x80,
            WaitingConnectionAbort = 0x81
        }

        public enum ConnMgrConRefTypeEnum
        {
            ConRefType_NAP = 0,
            ConRefType_PROXY
        }


        [StructLayout(LayoutKind.Sequential)]
        public class ConnMgrConnectionInfo
        {
            Int32 cbSize;                          // DWORD
            public ConnMgrParam dwParams = 0;      // DWORD
            public ConnMgrProxy dwFlags = 0;       // DWORD
            public ConnMgrPriority dwPriority = 0; // DWORD
            public Int32 bExclusive = 0;           // BOOL
            public Int32 bDisabled = 0;            // BOOL
            public Guid guidDestNet = Guid.Empty;  // GUID
            public IntPtr hWnd = IntPtr.Zero;      // HWND
            public UInt32 uMsg = 0;                // UINT
            public Int32 lParam = 0;               // LPARAM
            public UInt32 ulMaxCost = 0;           // ULONG
            public UInt32 ulMinRcvBw = 0;          // ULONG
            public UInt32 ulMaxConnLatency = 0;    // ULONG 

            public ConnMgrConnectionInfo()
            {
                cbSize = Marshal.SizeOf(typeof(ConnMgrConnectionInfo));
            }

            public ConnMgrConnectionInfo(Guid destination, ConnMgrPriority priority, ConnMgrProxy proxy)
                : this()
            {
                guidDestNet = destination;
                dwParams = ConnMgrParam.GuidDestNet;
                dwPriority = priority;
                dwFlags = proxy;
            }

            public ConnMgrConnectionInfo(Guid destination, ConnMgrPriority priority)
                : this(destination, priority, ConnMgrProxy.NoProxy) { }

            public ConnMgrConnectionInfo(Guid destination)
                : this(destination, ConnMgrPriority.UserInteractive) { }
        } ;


        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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