使用RAPI库操作移动设备.doc_第1页
使用RAPI库操作移动设备.doc_第2页
使用RAPI库操作移动设备.doc_第3页
使用RAPI库操作移动设备.doc_第4页
使用RAPI库操作移动设备.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

使用RAPI库操作移动设备C#语言描述Windows Mobile日益成熟,开发者队伍也越来越壮大。作为一个10年的计算机热爱者和程序员,我也经受不住新技术的诱惑,倒腾起Mobile这个玩具。Mobile和Windows的血缘关系决定了它在Windows程序员中的受欢迎程度,在网络上随便搜索一下,关于Mobile应用、开发的文章数不胜数。可是对于计划开发一款全能的DesktopDevice同步管理程序的我来说,却发现资源少得可怜仅仅在MSDN和两个国外的Developer网站上发现了一点资料。现在我仍然在搜索学习中,在这里把我迄今掌握的一点心得写出来,希望能起到抛砖引玉的功效。另请各位高手指正。Mobile的开发资源很繁杂,很多人常常弄不清究竟要安装哪些工具才能搭建出合适的开发环境。但是我相信Microsoft SMARTPHONE 2003 SDK和Microsoft POCKETPC 2003 SDK是所有的人都知道的,它们分别为SmartPhone和PocketPC提供了必不可少的支持。兄弟我至今没有做出什么成绩,囊中羞涩,好容易攒了台SmartPhone,今天就已Microsoft SMARTPHONE 2003 SDK为例吧。SMARTPHONE SDK包含了大量的API,列表如下(选自SDK文档):Smartphone APIDescriptionActiveSync创建移动应用程序安装和配置,同步服务模块,过滤器和协助访问ActiveSync服务的应用。Bluetooth API创建支持蓝牙设备的Mobile应用程序,比如耳机,打印机和其他移动设备。CE Messaging (CEMAPI)创建messaging applicationsConfiguration Service Providers创建可配置各种CSPs(Configuration Service Providers)的应用Connection Manager创建可自动管理移动设备网络连接的应用Control API在你的移动应用程序中使用Smartphone控件Device Management API创建可远程访问移动设备配置管理的应用程序Game API (GAPI)创建高性能的实时游戏Home Screen API创建用户界面插件HTML Control创建可显示HTML文本和嵌入图片,解析XML和绑定URL到别名的应用程序MIDI创建可播放MIDI文件的应用程序Object Exchange (OBEX)创建对象交换应用,允许移动设备自由的通过无线交换数据Pocket Outlook Object Model (POOM) API创建可操作收件箱部件(联系人,日历和任务)的移动应用程序Projects Control创建可以和Projects Control交互的应用Remote API (RAPI)创建可以同步或控制移动设备的桌面应用程序Speech Recognizer为应用程序增加语音识别功能(比如语音拨号)Telephony创建支持电话和短信的应用程序User Interface管理输入面板,增加用户界面元素到你的移动应用程序Vibrate API为你的移动应用程序增加震动特性Voice Recorder Control创建移动数字录音程序Windows User Interface Controls创建将移动扩展合并到标准Microsoft Windows CE用户界面控件的应用要创建DesktopDevice的桌面同步管理程序,主要就依靠SDK API中的Remote API(RAPI)。RAPI 库由一组函数组成,这些函数可用于通过桌面应用程序管理设备,包括设备的目录文件、设备的注册表和系统信息。废话不多说,我们先来看看如何管理设备中的目录文件。RAPI提供了一组文件管理的方法(不完全列表,详见SDK文档。):FunctionDescriptionCeCopyFile复制文件CeCreateDirectory创建目录CeCreateFile创建,打开文件、管道、通讯资源、磁盘设备或者控制台。返回一个句柄用来访问对象。CeDeleteFile删除文件CeFindAllFiles从指定的Windows CE目录中获取所有文件和目录的信息,并且复制到一个包含CE_FIND_DATA结构的数组中CeFindFirstFile在目录中查找匹配给定文件名的一个文件CeFindClose关闭指定的查找句柄,CeFindFirstFile和CeFindNextFile 函数用这个句柄查找文件CeFindNextFile从上一次访问的CeFindFirstFile继续查找文件CeGetFileAttributes返回指定文件或目录的属性CeGetFileSize获取指定文件的字节大小CeGetFileTime获取文件创建日期时间,最后访问日期时间和最后修改日期时间CeMoveFile移动(重命名)一个文件或者目录CeReadFile从文件指针处读取文件数据CeWriteFile从文件指针处写入文件数据首先要说明的是,任何RAPI操作都需要首先初始化与设备的连接:FunctionDescriptionCeRapiInit (RAPI)创建Windows CE remote application-programming interface (RAPI).C#.NETusing System;using System.Runtime.InteropServices;public class RAPI public void RapiInit() int ret = CeRapiInit(); if( ret != 0) / 连接失败,获取失败代码 int e = CeRapiGetError(); / 抛出异常 Marshal.ThrowExceptionForHR(ret); / 连接成功 / To Do DllImport(rapi.dll, CharSet=CharSet.Unicode) internal static extern int CeRapiGetError(); DllImport(rapi.dll, CharSet=CharSet.Unicode) internal static extern int CeRapiInit();连接建立后,就可以进行文件操作了。看一个将文件复制到设备的例子:C#.NETusing System;using System.Runtime.InteropServices;using System.IO;public class RAPI private const uint GENERIC_WRITE = 0x40000000; / 设置读写权限 private const short CREATE_NEW = 1; / 创建新文件 private const short FILE_ATTRIBUTE_NORMAL = 0x80; / 设置文件属性 private const short INVALID_HANDLE_VALUE = -1; / 错误句柄 IntPtr remoteFile = IntPtr.Zero; String LocalFileName = c:test.txt; / 本地计算机文件名 String RemoteFileName = My Documentstest.txt; / 远程设备文件名 byte buffer = new byte0x1000; / 传输缓冲区定义为4k FileStream localFile; int bytesread = 0; int byteswritten = 0; int filepos = 0; public RapiFile() / 创建远程文件 remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0); / 检查文件是否创建成功 if (int)remoteFile = INVALID_HANDLE_VALUE) throw new Exception(Could not create remote file); / 打开本地文件 localFile = new FileStream(LocalFileName, FileMode.Open); / 读取4K字节 bytesread = localFile.Read(buffer, filepos, buffer.Length); while(bytesread 0) / 移动文件指针到已读取的位置 filepos += bytesread; / 写缓冲区数据到远程设备文件 if(! Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread, ref byteswritten, 0) / 检查是否成功,不成功关闭文件句柄,抛出异常 CeCloseHandle(remoteFile); throw new Exception(Could not write to remote file); try / 重新填充本地缓冲区 bytesread = localFile.Read(buffer, 0, buffer.Length); catch(Exception) bytesread = 0; / 关闭本地文件 localFile.Close(); / 关闭远程文件 CeCloseHandle(remoteFile); / 声明要引用的API DllImport(rapi.dll, CharSet=CharSet.Unicode) internal static extern int CeCloseHandle(IntPtr hObject); DllImport(rapi.dll, CharSet=CharSet.Unicode) internal static extern int CeWriteFile(IntPtr hFile, byte lpBuffer, int nNumberOfbytesToWrite, ref int lpNumberOfbytesWritten, int lpOverlapped); DllImport(rapi.dll, CharSet=CharSet.Unicode, SetLastError=true) internal static extern IntPtr CeCreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);操作完毕后在合适的时候需要断开RAPI连接,使用如下函数(选自SDK文档):FunctionDescriptionCeRapiUninit (RAPI)销毁Windows CE remote application-programming interface (RAPI).C#.NETusing System;using System.Runtime.InteropServices;public class RAPIUninit public RAPIUninit() CeRapiUninit(); / 声明要引用的API DllImport(rapi.dll, CharSet=CharSet.Unicode) internal static extern int CeRapiUninit();文件操作的函数有很多,基本思路都是一样的,在这里就不一一举例了。请注意文件句柄使用以后一定要释放。我们再看一个取系统信息的例子,RAPI提供了一些取系统信息的函数(选自SDK文档,本人翻译):FunctionDescriptionCeGetSystemInfo返回当前系统信息CeGetSystemMetrics获取Windows元素的尺寸和系统设置CeGetVersionEx获取当前运行的操作系统版本的扩展信息CeGetSystemPowerStatusEx获取电池状态CeGlobalMemoryStatus获取系统物理内存和虚拟内存信息CeGetStoreInformation获取存储器信息并填入STORE_INFORMATION结构C#.netpublic class RAPI SYSTEM_INFO si; / 系统信息 OSVERSIONINFO versionInfo; / 版本信息 SYSTEM_POWER_STATUS_EX PowerStatus; / 电源信息 MEMORYSTATUS ms; / 内存信息 String info; public void systemInfo() / 检索系统信息 try CeGetSystemInfo(out si); catch(Exception) throw new Exception(Error retrieving system info.); / 检索设备操作系统版本号。 bool b; versionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFO); / 设置为结构大小 b = CeGetVersionEx(out versionInfo); if(!b) throw new Exception(Error retrieving version information.); / 检索设备电源状态 try CeGetSystemPowerStatusEx(out PowerStatus, true); / true 表示读取最新的电源信息,否则将从缓存中获得 catch(Exception) throw new Exception(Error retrieving system power status.); / 检索设备内存状态 CeGlobalMemoryStatus( out ms ); / 设置检索信息的格式。 info = The connected device has an ; switch (si.wProcessorArchitecture) case ProcessorArchitecture.Intel: info += Intel processor.n; break; case ProcessorArchitecture.MIPS: info += MIPS processor.n; break; case ProcessorArchitecture.ARM: info += ARM processor.n; break; default: info = unknown processor type.n; break; info += OS version: + versionInfo.dwMajorVersion + . + versionInfo.dwMinorVersion + . + versionInfo.dwBuildNumber + n; if (PowerStatus.ACLineStatus = 1) info += On AC power:YESn; else info += On AC power:NO n; info += Battery level: + PowerStatus.BatteryLifePercent + %n; info += Total memory: + String.Format(0:#,#,#, ms.dwTotalPhys) + n; / 显示结果。 Console.WriteLine(info); #region 声明API,详见SDK文档 DllImport(rapi.dll, CharSet=CharSet.Unicode, SetLastError=true) internal static extern int CeGetSystemInfo(out SYSTEM_INFO pSI); DllImport(rapi.dll, CharSet=CharSet.Unicode, SetLastError=true) internal static extern bool CeGetVersionEx(out OSVERSIONINFO lpVersionInformation); DllImport(rapi.dll, CharSet=CharSet.Unicode, SetLastError=true) internal static extern bool CeGetSystemPowerStatusEx(out SYSTEM_POWER_STATUS_EX pStatus, bool fUpdate); DllImport(rapi.dll, CharSet=CharSet.Unicode, SetLastError=true) internal static extern void CeGlobalMemoryStatus(out MEMORYSTATUS msce); #endregion #region 声明结构 / / 处理器架构 (CeGetSystemInfo) / public enum ProcessorArchitecture : short / / Intel / Intel = 0, / / MIPS / MIPS = 1, / / Alpha / Alpha = 2, / / PowerPC / PPC = 3, / / Hitachi SHx / SHX = 4, / / ARM / ARM = 5, / / IA64 / IA64 = 6, / / Alpha 64 / Alpha64 = 7, / / Unknown / Unknown = -1 / / 移动设备内存信息 / StructLayout(LayoutKind.Sequential) public struct MEMORYSTATUS internal uint dwLength; / / 当前内存占用 (%) / public int dwMemoryLoad; / / 物理内存总量 / public int dwTotalPhys; / / 可用物理内存 / public int dwAvailPhys; / / 分页数 / public int dwTotalPageFile; / / 未分页 / public int dwAvailPageFile; / / 虚拟内存总量 / public int dwTotalVirtual; / / 可用虚拟内存 / public int dwAvailVirtual; / / 移动设备电源信息 / public struct SYSTEM_POWER_STATUS_EX / / 交流电状态 / public byte ACLineStatus; / / 电池充电状态。1 High,2 Low,4 Critical,8 Charging,128 No system battery,255 Unknown status / public byte BatteryFlag; / / 电池电量剩余百分比 / public byte BatteryLifePercent; / / 保留字段,设置为0 / internal byte Reserved1; / / 电池电量剩余时间(秒) / public int BatteryLifeTime; / / 电池充满电的总可用时间(秒) / public int BatteryFullLifeTime; / / 保留字段,设置为0 / internal byte Reserved2; / / 后备电池状态 / public byte BackupBatteryFlag; / / 后备电池剩余电量百分比 / public byte BackupBatteryLifePercent; / / 保留字段,设置为0 / internal byte Reserved3; / / 后备电池电量剩余时间(秒) / public int BackupBatteryLifeTime; / / 后备电池充满电的总可用时间(秒) / public int BackupBatteryFullLifeTime; / / OSVERSIONINFO platform type / public enum PlatformType : int / / Win32 on Windows CE. / VER_PLATFORM_WIN32_CE = 3 / / 操作系统版本信息 / public struct OSVERSIONINFO internal int dwOSVersionInfoSize; / / 主版本信息 / public int dwMajorVersion; / / 副版本信息 / public int dwMinorVersion; / / 编译信息 / public int dwBuildNumber; / / 操作系统类型 / public PlatformType dwPlatformId; / / 处理器类型 (CeGetSystemInfo) / public enum ProcessorType : int / / 386 / PROCESSOR_INTEL_386 = 386, / / 486 / PROCESSOR_INTEL_486 = 486, / / Pentium / PROCESSOR_INTEL_PENTIUM = 586, / / P2 / PROCESSOR_INTEL_PENTIUMII = 686, / / IA 64 / PROCESSOR_INTEL_IA64 = 2200, / / MIPS 4000 series / PROCESSOR_MIPS_R4000 = 4000, / / Alpha 21064 / PROCESSOR_ALPHA_21064 = 21064, / / PowerPC 403 / PROCESSOR_PPC_403 = 403, / / PowerPC 601 / PROCESSOR_PPC_601 = 601, / / PowerPC 603 / PROCESSOR_PPC_603 = 603, / / PowerPC 604 / PROCESSOR_PPC_604 = 604, / / PowerPC 620 / PROCESSOR_PPC_620 = 620, / / Hitachi SH3 / PROCESSOR_HITACHI_SH3 = 10003, / / Hitachi SH3E / PROCESSOR_HITACHI_SH3E = 10004, / / Hitachi SH4 / PROCESSOR_HITACHI_SH4 = 10005, / / Motorola 821 / PROCESSOR_MOTOROLA_821 = 821, / / Hitachi SH3 / PROCESSOR_SHx_SH3 = 103, / / Hitachi SH4 / PROCESSOR_SHx_SH4 = 104, / / Intel StrongARM / PROCESSOR_STRONGARM = 2577, / / ARM720 / PROCESSOR_ARM720 = 1824, / / ARM820 / PROCESSOR_ARM820 = 2080, / / ARM920 / PROCESSOR_ARM920 = 2336, / / ARM 7 / PROCESSOR_ARM_7TDMI = 70001 / / CeGetSystemInfo的数据结构 / public struct SYSTEM_INFO / / 处理器架构 / public ProcessorArchitecture wProcessorArchitecture; / / 保留 / internal ushort wReserved; / / Specifies the page size and the granularity of page protection and commitment. / public int dwPageSize; / / 应用程序可访问内存地址的最小值 /(Pointer to the lowest memory address accessible to applications / and dynamic-link libraries (DLLs). ) / public int lpMinimumApplicationAddress; / / 应用程序可访问内存地址的最大值(Pointer to the highest memory address / accessible to applications and DLLs.) / public int lpMaximumApplicationAddress; / / Specifies a mask representing the set of processors configured into / the system. Bit 0 is processor 0; bit 31 is processor 31. / public int dwActiveProcessorMask; / / 处理器数量(Specifies the number of processors in the system.) / public int dwNumberOfProcessors; / / 处理器类型(Specifies the type of processor in the system.) / public Pro

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论