本文主要是介绍Windows Mobile 下使用C#进行GPRS、CDMA开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
有关GPRS、CDMA开发的文章网上已经有不少,但是由于WindowsMobile SDK提供的GPRS、CDMA连接操作的库只有C++版本的(即Connection ManagerAPI),网上的文章大多数都是C++版本的,尽管也有C#编写的但是大多封装的有些不对并且没有经过很好的测试,本文在网络已有的资料上整理出如何用C#进行GPRS、CDMA开发。
参考文献:
Windows Mobile 中如何建立 GPRS 连接以便 Socket 能正常通信
GPRS开发系列文章之进阶篇
Establish a GPRS connection with TcpClient
开发环境
Visual Studio 2005
Windows Mobile 6 SDK Professional
头文件封装
我们要用到Windows Mobile SDK的一个C++的头文件Connmgr.h,它的路径是:c:\program files\windows mobile 6 sdk\pocketpc\include\armv4i\connmgr.h,首先要用C#重新定义要用到的这个头文件的常量、结构和外部函数声明。
有关C#平台封送的更多说明可以参考MSDN:
http://msdn.microsoft.com/zh-cn/library/fzhhdwae(VS.80).aspx
常量声明:
const int S_OK = 0;
const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
const uint INFINITE = 0xffffffff;
const uint CONNMGR_STATUS_CONNECTED = 0x10;
const int CONNMGR_MAX_DESC = 128; // @constdefine Max size of a network description
const int CONNMGR_FLAG_PROXY_HTTP = 0x1; // @constdefine HTTP Proxy supported
const int CONNMGR_FLAG_PROXY_WAP = 0x2; // @constdefine WAP Proxy (gateway) supported
const int CONNMGR_FLAG_PROXY_SOCKS4 = 0x4; // @constdefine SOCKS4 Proxy supported
const int CONNMGR_FLAG_PROXY_SOCKS5 = 0x8; // @constdefine SOCKS5 Proxy supported
const UInt16 IDC_WAIT = 32514;
const UInt16 IDC_ARROW = 32512;
结构封装声明:
C++原型:
typedef struct _CONNMGR_CONNECTIONINFO
{
DWORD cbSize; // @field Size of this structure
DWORD dwParams; // @field Valid parms, set of CONNMGR_PARAM_*
DWORD dwFlags; // @field Connection flags, set of CONNMGR_FLAG_*
DWORD dwPriority; // @field Priority, one of CONNMGR_PRIORITY_*
BOOL bExclusive; // @field Connection is exclusive, see comments
BOOL bDisabled; // @field Don't actually connect
GUID guidDestNet; // @field GUID of network to connect to
HWND hWnd; // @field hWnd to post status change messages to
UINT uMsg; // @field Msg to use when posting status changes
LPARAM lParam; // @field lParam to use when posting status changes
ULONG ulMaxCost; // @field Max acceptable cost of connection
ULONG ulMinRcvBw; // @field Min acceptable receive bandwidth of connection
ULONG ulMaxConnLatency; // @field Max acceptable connect latency
} CONNMGR_CONNECTIONINFO;
typedef struct _CONNMGR_DESTINATION_INFO
{
GUID guid; // @field GUID associated with network
TCHAR szDescription[CONNMGR_MAX_DESC]; // @field Description of network
BOOL fSecure; // @field Is it OK to allow multi-homing on the network
} CONNMGR_DESTINATION_INFO;
typedef struct _GUID { // size is 16
DWORD Data1;
WORD Data2;
WORD Data3;
BYTE Data4[8];
} GUID;
C#声明
[StructLayout(LayoutKind.Sequential)]
public struct CONNMGR_CONNECTIONINFO
{
public uint cbSize;
public uint dwParams;
public uint dwFlags;
public uint dwPriority;
public int bExclusive;
public int bDisabled;
public GUID guidDestNet;
public IntPtr hWnd;
public uint uMsg;
public uint lParam;
public uint ulMaxCost;
public uint ulMinRcvBw;
public uint ulMaxConnLatency;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CONNMGR_DESTINATION_INFO
{
public GUID guid; // @field GUID associated with network
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = CONNMGR_MAX_DESC)]
public string szDescription; // @field Description of network
public int fSecure; // @field Is it OK to allow multi-homing on the network
}
public struct GUID
{ // size is 16
public uint Data1;
public UInt16 Data2;
public UInt16 Data3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] Data4;
}
函数封送声明: C++原型: 请参考connmgr.h C#声明: |
[DllImport("coredll.dll")]
public static extern uint GetTickCount();
[DllImport("coredll.dll")]
public static extern uint WaitForSingleObject(IntPtr hHandle,uint dwMilliseconds);
[DllImport("cellcore.dll")]
public static extern int ConnMgrMapURL(string pwszURL, ref GUID pguid, ref uint pdwIndex);
[DllImport("cellcore.dll")]
public static extern int ConnMgrEstablishConnectionSync(ref CONNMGR_CONNECTIONINFO ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);
[DllImport("cellcore.dll")]
private static extern IntPtr ConnMgrApiReadyEvent();
[DllImport("cellcore.dll")]
public static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);
[DllImport("cellcore.dll")]
public static extern int ConnMgrEnumDestinations(int nIndex,ref CONNMGR_DESTINATION_INFO pDestInfo);
[DllImport("cellcore.dll")]
public static extern int ConnMgrConnectionStatus(IntPtr hConnection, // @parm Handle to connection, returned from ConnMgrEstablishConnection
ref uint pdwStatus // @parm Returns current connection status, one of CONNMGR_STATUS_*
);
[DllImport("coredll.dll")]
private static extern int CloseHandle(IntPtr hObject);
这里参考 Windows Mobile 中如何建立 GPRS 连接以便 Socket 能正常通信 例子用C#封装一个类库,并实现类似的Demo。运行界面如下图所示:
手机设置
在这里给出GPRS连接的设置说明,大多数的手机采用默认设置就可以直接使用了,并不用自己设置,但是如果你发现上面代码运行后不能建立GPRS连接,测可以尝试下面的设置。
在上面的GPRS连接代码中,我们使用了“Internet 设置”这个设置,如果手机还没有建立这个连接设置或者已经建立设置但是连接总是失败,则可以按下面的方式建立或修改连接设置(这里使用Windows Mobile 5操作系统,动感地带SIM卡,用模拟器截图):
1.打开“开始->设置”,点击“连接”如图1所示。
图 1连接
2.如果您的手机已经有Internet设置,则在图2中会有“管理现有连接”的链接,点击它可以进行修改设置,在这里点击“添加新调制解调器连接”,增加一个Internet 设置。
图2 连接设置
3.在弹出的如图3所示的对话框中,选择调制解调器为“电话线路(GPRS)”。 图3 调制解调器 图3 调制解调器 4.在弹出如图4所示的对话框中,“访问点名称”输入“cmnet”。 图4 访问点名称 5.在弹出的如图5所示的对话框中,直接点击“完成”。对于专用网络则要输入移动提供的用户名和密码。 图5用户名密码 6.点击“完成”后在“Internet 设置”里多了一个“GRPS连接互联网”的设置,如果此处有几个设置,请确保您要使用的设置处于选中状态。点击“OK”退出设置。此时您可以使用本问提供的实例程序来尝试连接,在连接的时候手机不要和电脑处于同步状态,否则手机可能不会进行拨号。 图6 完成设置 从上面的使用我们可以知道,建立GPRS连接和建立CDMA连接的差别仅仅是连接设置的不同而已,也就是拨号的问题。但是网络上很多人问GPRS开发和CDMA开发问题,这里通过这个Demo,希望可以给学习移动开发的朋友们提供一些帮助。 |
这篇关于Windows Mobile 下使用C#进行GPRS、CDMA开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!