📄 connectionmanager.cs
字号:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
namespace Chapter9
{
/// <summary>
/// Specifies the state of the connection.
/// </summary>
public enum ConnectionStatus
{
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,
WaitingConnection = 0x40,
WaitingForResource = 0x41,
WaitingForNetwork = 0x42,
WaitingDisconnection = 0x80,
WaitingConnectionAbort = 0x81,
}
/// <summary>
/// Provides a simple wrapper around the Connection Manager APIs.
/// </summary>
public class ConnectionManager
{
private IntPtr handle;
private NativeMethods.CONNMGR_CONNECTIONINFO connectionInfo;
private ConnectionManagerMessageWindow messageWindow;
#region Constructor
/// <summary>
/// Creates a new instance of ConnectionManager.
/// </summary>
public ConnectionManager()
{
messageWindow = new ConnectionManagerMessageWindow(this);
connectionInfo = new NativeMethods.CONNMGR_CONNECTIONINFO();
connectionInfo.cbSize = Marshal.SizeOf(connectionInfo);
connectionInfo.dwFlags = NativeMethods.CONNMGR_FLAG.NO_ERROR_MSGS;
connectionInfo.dwParams = NativeMethods.CONNMGR_PARAM.GUIDDESTNET;
connectionInfo.dwPriority = ConnectionPriority.HighPriorityBackground;
connectionInfo.hWnd = messageWindow.Hwnd;
connectionInfo.uMsg = ConnectionManagerMessageWindow.WM_ConnectionManager;
}
#endregion
#region Connect
/// <summary>
/// Creates a connection request.
/// </summary>
/// <param name="destination"></param>
public void EstablishConnection(Guid destination)
{
ReleaseConnection();
connectionInfo.guidDestNet = destination;
int hresult = NativeMethods.ConnMgrEstablishConnection(ref connectionInfo, out handle);
}
/// <summary>
/// Creates a connection request but returns information only after the connection has been established or has failed.
/// </summary>
/// <param name="destination"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public ConnectionStatus EstablishConnectionSync(Guid destination, TimeSpan timeout)
{
ReleaseConnection();
ConnectionStatus status;
int hresult = NativeMethods.ConnMgrEstablishConnectionSync(ref connectionInfo, out handle, Convert.ToInt32(timeout.TotalMilliseconds), out status);
return status;
}
#endregion
#region Status
/// <summary>
/// Returns the status of the current connection.
/// </summary>
public ConnectionStatus ConnectionStatus
{
get
{
ConnectionStatus status = ConnectionStatus.Unknown;
if (handle != IntPtr.Zero)
{
int hresult = NativeMethods.ConnMgrConnectionStatus(handle, out status);
}
return status;
}
}
//raises the connectionstatuschanged event
internal void OnConnectionStatusChanged(ConnectionStatusChangedEventArgs e)
{
if (ConnectionStatusChanged != null)
{
ConnectionStatusChanged(e);
}
}
//event fired when connection manager sends a status changed message
public event ConnectionStatusChangedEventHandler ConnectionStatusChanged;
#endregion
#region Release
/// <summary>
/// Deletes the specified connection request, which could drop the physical connection.
/// </summary>
public void ReleaseConnection()
{
if (handle != IntPtr.Zero)
{
int hresult = NativeMethods.ConnMgrReleaseConnection(handle, 0);
handle = IntPtr.Zero;
}
}
#endregion
#region MapUrl
/// <summary>
/// Maps a Url to the globally unique identifier (Guid) of the network to which the device is connected.
/// </summary>
/// <param name="url">The URL to be mapped.</param>
/// <returns>Array of destination network identifiers.</returns>
public static Guid[] MapUrl(Uri url)
{
ArrayList al = new ArrayList();
Guid g;
int index = 0;
int hresult = 0;
while (hresult == 0)
{
//get next destination guid
hresult = NativeMethods.ConnMgrMapURL(url.ToString(), out g, ref index);
if (hresult == 0)
{
//if success add to arraylist
al.Add(g);
}
}
//return array of guids
return (Guid[])al.ToArray(typeof(Guid));
}
#endregion
}
public delegate void ConnectionStatusChangedEventHandler(ConnectionStatusChangedEventArgs e);
#region ConnectionStatusChangedEventArgs
public class ConnectionStatusChangedEventArgs : EventArgs
{
private ConnectionStatus connectionStatus;
public ConnectionStatusChangedEventArgs(ConnectionStatus status)
{
this.connectionStatus = status;
}
public ConnectionStatus ConnectionStatus
{
get
{
return connectionStatus;
}
}
}
#endregion
#region NativeMethods
internal class NativeMethods
{
[Flags()]
internal enum CONNMGR_PARAM
{
GUIDDESTNET = 0x1,
MAXCOST = 0x2,
MINRCVBW = 0x4,
MAXCONNLATENCY = 0x8,
}
[Flags()]
internal enum CONNMGR_FLAG
{
PROXY_HTTP = 0x1,
PROXY_WAP = 0x2,
PROXY_SOCKS4 = 0x4,
PROXY_SOCKS5 = 0x8,
SUSPEND_AWARE = 0x10,
REGISTERED_HOME = 0x20,
NO_ERROR_MSGS = 0x40,
}
[StructLayout(LayoutKind.Sequential)]
internal struct CONNMGR_CONNECTIONINFO
{
public int cbSize;
public CONNMGR_PARAM dwParams;
public CONNMGR_FLAG dwFlags;
public ConnectionPriority dwPriority;
public int bExclusive;
public int bDisabled;
public Guid guidDestNet;
public IntPtr hWnd;
public int uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
[DllImport("cellcore", SetLastError = true)]
internal static extern int ConnMgrEstablishConnection(
ref CONNMGR_CONNECTIONINFO pConnInfo, out IntPtr phConnection);
[DllImport("cellcore", SetLastError = true)]
internal static extern int ConnMgrEstablishConnectionSync(
ref CONNMGR_CONNECTIONINFO pConnInfo, out IntPtr phConnection,
int dwTimeout, out ConnectionStatus pdwStatus);
[DllImport("cellcore", SetLastError = true)]
internal static extern int ConnMgrConnectionStatus(IntPtr hConnection, out ConnectionStatus pdwStatus);
[DllImport("cellcore", SetLastError = true)]
internal static extern int ConnMgrReleaseConnection(IntPtr hConnection, int lCache);
[DllImport("cellcore", SetLastError = true)]
internal static extern int ConnMgrMapURL(string pwszURL, out Guid pguid, ref int pdwIndex);
[DllImport("coredll", SetLastError = true)]
internal static extern int RegisterWindowMessage(string lpString);
}
#endregion
internal class ConnectionManagerMessageWindow : MessageWindow
{
internal static int WM_ConnectionManager = NativeMethods.RegisterWindowMessage("ConnectionManagerEvent");
private ConnectionManager connectionManager;
internal ConnectionManagerMessageWindow(ConnectionManager parent)
{
this.connectionManager = parent;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ConnectionManager)
{
//this is a connection manager message
//convert the status
ConnectionStatus status = (ConnectionStatus)m.WParam.ToInt32();
//raise the event on the parent ConnectionManager class
this.connectionManager.OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(status));
}
//pass control to base wndproc implementation
base.WndProc(ref m);
}
}
[Flags()]
public enum ConnectionPriority
{
Voice = 0x20000,
UserInteractive = 0x08000,
UserBackground = 0x02000,
UserIdle = 0x0800,
HighPriorityBackground = 0x0200,
IdleBackground = 0x0080,
ExternalInteractive = 0x0020,
LowPriorityBackground = 0x0008,
//Cached = 0x0002,
//AlwaysOn = 0x0001,
}
#region Destination
/// <summary>
/// Standard destination networks.
/// </summary>
public static class Destination
{
/// <summary>
///
/// </summary>
public static readonly Guid Internet = new Guid("{436EF144-B4FB-4863-A041-8F905A62C572}");
/// <summary>
///
/// </summary>
public static readonly Guid Corporate = new Guid("{A1182988-0D73-439e-87AD-2A5B369F808B}");
/// <summary>
///
/// </summary>
public static readonly Guid Wap = new Guid("{7022E968-5A97-4051-BC1C-C578E2FBA5D9}");
/// <summary>
///
/// </summary>
public static readonly Guid SecureWap = new Guid("{F28D1F74-72BE-4394-A4A7-4E296219390C}");
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -