【转载】张远山谈:Windows Mobile 下使用C#进行GPRS、CDMA开发

bell791002 2009-02-26 06:08:21
加精
转载于:http://blog.csdn.net/zhyuanshan/archive/2009/02/26/3940125.aspx
作者:张远山

正文:


有关GPRS、CDMA开发的文章网上已经有不少,但是由于Windows Mobile SDK提供的GPRS、CDMA连接操作的库只有C++版本的(即Connection Manager API),网上的文章大多数都是C++版本的,尽管也有C#编写的但是大多封装的有些不对并且没有经过很好的测试,本文在网络已有的资料上整理出如何用C#进行GPRS、CDMA开发。

您可以到一下站点免费下载作者的手机开发教程:http://www.itcast.net/course/detail/561

参考文献:
http://www.vckbase.com/document/viewdoc/?id=1803

http://www.cnblogs.com/jsjkandy/archive/2008/08/06/1262445.html

http://blogs.msdn.com/anthonywong/archive/2006/03/13/550686.aspx

开发环境
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++原型:

[code=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;

[/code]



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





这里参考http://www.vckbase.com/document/viewdoc/?id=1803例子用C#封装一个类库,并实现类似的Demo。运行界面如下图所示:


更多内容请查看原文:http://blog.csdn.net/zhyuanshan/archive/2009/02/26/3940125.aspx
...全文
1102 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
diy2005 2011-04-20
  • 打赏
  • 举报
回复
果然是好帖子
xHeroSky 2010-05-15
  • 打赏
  • 举报
回复
请问一下,获取系统进程,用到哪些函数呢?还有怎么写那些函数的原型呢?
raymond1026 2010-01-17
  • 打赏
  • 举报
回复
好DD,先看看,谢了
群龙 2010-01-13
  • 打赏
  • 举报
回复

oscar7879 2009-10-27
  • 打赏
  • 举报
回复
非常好,很好的资源
zidanezhang 2009-06-15
  • 打赏
  • 举报
回复
留个记号 关注
hujun_zero 2009-03-11
  • 打赏
  • 举报
回复
up!
chxljtt 2009-03-11
  • 打赏
  • 举报
回复
留个脚印
ZHOUGE 2009-03-05
  • 打赏
  • 举报
回复
xuexi
relive_qiankai 2009-03-05
  • 打赏
  • 举报
回复
我不太会C++,上个项目是在VS2005 windows mobile6.0用C#做了个应用程序Demo..感觉上如果有真实手机模拟器的话还可以,没有的话开发起来太累人.学习了!
relive_qiankai 2009-03-03
  • 打赏
  • 举报
回复
学习了 关注
shuihan20e 2009-02-27
  • 打赏
  • 举报
回复
willsmith 2009-02-27
  • 打赏
  • 举报
回复
先看看
seasonjie 2009-02-27
  • 打赏
  • 举报
回复
等我先仔细看看阿!
kinglovelili 2009-02-27
  • 打赏
  • 举报
回复
UP

2,089

社区成员

发帖
与我相关
我的任务
社区描述
在线培训学习经验交流分享、优质课程资源共享。
社区管理员
  • IT课程大本营社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧