使用RAPI库操作移动设备资料.docx

上传人:b****6 文档编号:16735685 上传时间:2023-07-16 格式:DOCX 页数:18 大小:23.15KB
下载 相关 举报
使用RAPI库操作移动设备资料.docx_第1页
第1页 / 共18页
使用RAPI库操作移动设备资料.docx_第2页
第2页 / 共18页
使用RAPI库操作移动设备资料.docx_第3页
第3页 / 共18页
使用RAPI库操作移动设备资料.docx_第4页
第4页 / 共18页
使用RAPI库操作移动设备资料.docx_第5页
第5页 / 共18页
使用RAPI库操作移动设备资料.docx_第6页
第6页 / 共18页
使用RAPI库操作移动设备资料.docx_第7页
第7页 / 共18页
使用RAPI库操作移动设备资料.docx_第8页
第8页 / 共18页
使用RAPI库操作移动设备资料.docx_第9页
第9页 / 共18页
使用RAPI库操作移动设备资料.docx_第10页
第10页 / 共18页
使用RAPI库操作移动设备资料.docx_第11页
第11页 / 共18页
使用RAPI库操作移动设备资料.docx_第12页
第12页 / 共18页
使用RAPI库操作移动设备资料.docx_第13页
第13页 / 共18页
使用RAPI库操作移动设备资料.docx_第14页
第14页 / 共18页
使用RAPI库操作移动设备资料.docx_第15页
第15页 / 共18页
使用RAPI库操作移动设备资料.docx_第16页
第16页 / 共18页
使用RAPI库操作移动设备资料.docx_第17页
第17页 / 共18页
使用RAPI库操作移动设备资料.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

使用RAPI库操作移动设备资料.docx

《使用RAPI库操作移动设备资料.docx》由会员分享,可在线阅读,更多相关《使用RAPI库操作移动设备资料.docx(18页珍藏版)》请在冰点文库上搜索。

使用RAPI库操作移动设备资料.docx

使用RAPI库操作移动设备资料

使用RAPI库操作移动设备——C#语言描述

WindowsMobile日益成熟,开发者队伍也越来越壮大。

作为一个10年的计算机热爱者和程序员,我也经受不住新技术的诱惑,倒腾起Mobile这个玩具。

Mobile和Windows的血缘关系决定了它在Windows程序员中的受欢迎程度,在网络上随便搜索一下,关于Mobile应用、开发的文章数不胜数。

可是对于计划开发一款全能的Desktop<=>Device同步管理程序的我来说,却发现资源少得可怜——仅仅在MSDN和两个国外的Developer网站上发现了一点资料。

现在我仍然在搜索学习中,在这里把我迄今掌握的一点心得写出来,希望能起到抛砖引玉的功效。

另请各位高手指正。

Mobile的开发资源很繁杂,很多人常常弄不清究竟要安装哪些工具才能搭建出合适的开发环境。

但是我相信MicrosoftSMARTPHONE2003SDK和MicrosoftPOCKETPC2003SDK是所有的人都知道的,它们分别为SmartPhone和PocketPC提供了必不可少的支持。

兄弟我至今没有做出什么成绩,囊中羞涩,好容易攒了台SmartPhone,今天就已MicrosoftSMARTPHONE2003SDK为例吧。

SMARTPHONESDK包含了大量的API,列表如下(选自SDK文档):

SmartphoneAPI

Description

ActiveSync

创建移动应用程序安装和配置,同步服务模块,过滤器和协助访问ActiveSync服务的应用。

BluetoothAPI

创建支持蓝牙设备的Mobile应用程序,比如耳机,打印机和其他移动设备。

CEMessaging(CEMAPI)

创建messagingapplications

ConfigurationServiceProviders

创建可配置各种CSPs(ConfigurationServiceProviders)的应用

ConnectionManager

创建可自动管理移动设备网络连接的应用

ControlAPI

在你的移动应用程序中使用Smartphone控件

DeviceManagementAPI

创建可远程访问移动设备配置管理的应用程序

GameAPI(GAPI)

创建高性能的实时游戏

HomeScreenAPI

创建用户界面插件

HTMLControl

创建可显示HTML文本和嵌入图片,解析XML和绑定URL到别名的应用程序

MIDI

创建可播放MIDI文件的应用程序

ObjectExchange(OBEX)

创建对象交换应用,允许移动设备自由的通过无线交换数据

PocketOutlookObjectModel(POOM)API

创建可操作收件箱部件(联系人,日历和任务)的移动应用程序

ProjectsControl

创建可以和ProjectsControl交互的应用

RemoteAPI(RAPI)

创建可以同步或控制移动设备的桌面应用程序

SpeechRecognizer

为应用程序增加语音识别功能(比如语音拨号)

Telephony

创建支持电话和短信的应用程序

UserInterface

管理输入面板,增加用户界面元素到你的移动应用程序

VibrateAPI

为你的移动应用程序增加震动特性

VoiceRecorderControl

创建移动数字录音程序

WindowsUserInterfaceControls

创建将移动扩展合并到标准Microsoft®Windows®CE用户界面控件的应用

要创建Desktop<=>Device的桌面同步管理程序,主要就依靠SDKAPI中的RemoteAPI(RAPI)。

RAPI库由一组函数组成,这些函数可用于通过桌面应用程序管理设备,包括设备的目录文件、设备的注册表和系统信息。

废话不多说,我们先来看看如何管理设备中的目录文件。

RAPI提供了一组文件管理的方法(不完全列表,详见SDK文档。

):

Function

Description

CeCopyFile

复制文件

CeCreateDirectory

创建目录

CeCreateFile

创建,打开文件、管道、通讯资源、磁盘设备或者控制台。

返回一个句柄用来访问对象。

CeDeleteFile

删除文件

CeFindAllFiles

从指定的WindowsCE目录中获取所有文件和目录的信息,并且复制到一个包含CE_FIND_DATA结构的数组中

CeFindFirstFile

在目录中查找匹配给定文件名的一个文件

CeFindClose

关闭指定的查找句柄,CeFindFirstFile和CeFindNextFile函数用这个句柄查找文件

CeFindNextFile

从上一次访问的CeFindFirstFile继续查找文件

CeGetFileAttributes

返回指定文件或目录的属性

CeGetFileSize

获取指定文件的字节大小

CeGetFileTime

获取文件创建日期时间,最后访问日期时间和最后修改日期时间

CeMoveFile

移动(重命名)一个文件或者目录

CeReadFile

从文件指针处读取文件数据

CeWriteFile

从文件指针处写入文件数据

首先要说明的是,任何RAPI操作都需要首先初始化与设备的连接:

Function

Description

CeRapiInit(RAPI)

创建WindowsCEremoteapplication-programminginterface(RAPI).

[C#.NET]

 

usingSystem;

usingSystem.Runtime.InteropServices;

publicclassRAPI

{

publicvoidRapiInit()

{

intret=CeRapiInit();

if(ret!

=0)

{

//连接失败,获取失败代码

inte=CeRapiGetError();

//抛出异常

Marshal.ThrowExceptionForHR(ret);

}

//连接成功

//ToDo

}

[DllImport("rapi.dll",CharSet=CharSet.Unicode)]

internalstaticexternintCeRapiGetError();

[DllImport("rapi.dll",CharSet=CharSet.Unicode)]

internalstaticexternintCeRapiInit();

}

 

连接建立后,就可以进行文件操作了。

看一个将文件复制到设备的例子:

[C#.NET]

usingSystem;

usingSystem.Runtime.InteropServices;

usingSystem.IO;

publicclassRAPI

{

privateconstuintGENERIC_WRITE=0x40000000;//设置读写权限

privateconstshortCREATE_NEW=1;//创建新文件

privateconstshortFILE_ATTRIBUTE_NORMAL=0x80;//设置文件属性

privateconstshortINVALID_HANDLE_VALUE=-1;//错误句柄

IntPtrremoteFile=IntPtr.Zero;

StringLocalFileName=@"c:

\test.txt";//本地计算机文件名

StringRemoteFileName=@"\MyDocuments\test.txt";//远程设备文件名

byte[]buffer=newbyte[0x1000];//传输缓冲区定义为4k

FileStreamlocalFile;

intbytesread=0;

intbyteswritten=0;

intfilepos=0;

publicRapiFile()

{

//创建远程文件

remoteFile=CeCreateFile(RemoteFileName,GENERIC_WRITE,0,0,CREATE_NEW,

FILE_ATTRIBUTE_NORMAL,0);

//检查文件是否创建成功

if((int)remoteFile==INVALID_HANDLE_VALUE)

{

thrownewException("Couldnotcreateremotefile");

}

//打开本地文件

localFile=newFileStream(LocalFileName,FileMode.Open);

//读取4K字节

bytesread=localFile.Read(buffer,filepos,buffer.Length);

while(bytesread>0)

{

//移动文件指针到已读取的位置

filepos+=bytesread;

//写缓冲区数据到远程设备文件

if(!

Convert.ToBoolean(CeWriteFile(remoteFile,buffer,bytesread,

refbyteswritten,0)))

{//检查是否成功,不成功关闭文件句柄,抛出异常

CeCloseHandle(remoteFile);

thrownewException("Couldnotwritetoremotefile");

}

try

{

//重新填充本地缓冲区

bytesread=localFile.Read(buffer,0,buffer.Length);

}

catch(Exception)

{

bytesread=0;

}

}

//关闭本地文件

localFile.Close();

//关闭远程文件

CeCloseHandle(remoteFile);

}

//声明要引用的API

[DllImport("rapi.dll",CharSet=CharSet.Unicode)]

internalstaticexternintCeCloseHandle(IntPtrhObject);

[DllImport("rapi.dll",CharSet=CharSet.Unicode)]

internalstaticexternintCeWriteFile(IntPtrhFile,byte[]lpBuffer,

intnNumberOfbytesToWrite,refintlpNumberOfbytesWritten,intlpOverlapped);

[DllImport("rapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]

internalstaticexternIntPtrCeCreateFile(

stringlpFileName,

uintdwDesiredAccess,

intdwShareMode,

intlpSecurityAttributes,

intdwCreationDisposition,

intdwFlagsAndAttributes,

inthTemplateFile);

}

 

操作完毕后在合适的时候需要断开RAPI连接,使用如下函数(选自SDK文档):

Function

Description

CeRapiUninit(RAPI)

销毁WindowsCEremoteapplication-programminginterface(RAPI).

[C#.NET]

usingSystem;

usingSystem.Runtime.InteropServices;

publicclassRAPIUninit

{

publicRAPIUninit()

{

CeRapiUninit();

}

//声明要引用的API

[DllImport("rapi.dll",CharSet=CharSet.Unicode)]

internalstaticexternintCeRapiUninit();

}

 

文件操作的函数有很多,基本思路都是一样的,在这里就不一一举例了。

请注意文件句柄使用以后一定要释放。

我们再看一个取系统信息的例子,RAPI提供了一些取系统信息的函数(选自SDK文档,本人翻译):

Function

Description

CeGetSystemInfo

返回当前系统信息

CeGetSystemMetrics

获取Windows元素的尺寸和系统设置

CeGetVersionEx

获取当前运行的操作系统版本的扩展信息

CeGetSystemPowerStatusEx

获取电池状态

CeGlobalMemoryStatus

获取系统物理内存和虚拟内存信息

CeGetStoreInformation

获取存储器信息并填入STORE_INFORMATION结构

 

[C#.net]

publicclassRAPI

{

SYSTEM_INFOsi;//系统信息

OSVERSIONINFOversionInfo;//版本信息

SYSTEM_POWER_STATUS_EXPowerStatus;//电源信息

MEMORYSTATUSms;//内存信息

Stringinfo;

publicvoidsystemInfo()

{

//检索系统信息

try

{

CeGetSystemInfo(outsi);

}

catch(Exception)

{

thrownewException("Errorretrievingsysteminfo.");

}

//检索设备操作系统版本号。

boolb;

versionInfo.dwOSVersionInfoSize=Marshal.SizeOf(typeof(OSVERSIONINFO));//设置为结构大小

b=CeGetVersionEx(outversionInfo);

if(!

b)

{

thrownewException("Errorretrievingversioninformation.");

}

//检索设备电源状态

try

{

CeGetSystemPowerStatusEx(outPowerStatus,true);//true表示读取最新的电源信息,否则将从缓存中获得

}

catch(Exception)

{

thrownewException("Errorretrievingsystempowerstatus.");

}

//检索设备内存状态

CeGlobalMemoryStatus(outms);

//设置检索信息的格式。

info="Theconnecteddevicehasan";

switch(si.wProcessorArchitecture)

{

caseProcessorArchitecture.Intel:

info+="Intelprocessor.\n";

break;

caseProcessorArchitecture.MIPS:

info+="MIPSprocessor.\n";

break;

caseProcessorArchitecture.ARM:

info+="ARMprocessor.\n";

break;

default:

info="unknownprocessortype.\n";

break;

}

info+="OSversion:

"+versionInfo.dwMajorVersion+"."+versionInfo.dwMinorVersion+"."+

versionInfo.dwBuildNumber+"\n";

if(PowerStatus.ACLineStatus==1)

{

info+="OnACpower:

YES\n";

}

else

{

info+="OnACpower:

NO\n";

}

info+="Batterylevel:

"+PowerStatus.BatteryLifePercent+"%\n";

info+="Totalmemory:

"+String.Format("{0:

###,###,###}",ms.dwTotalPhys)+

"\n";

//显示结果。

Console.WriteLine(info);

}

#region声明API,详见SDK文档

[DllImport("rapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]

internalstaticexternintCeGetSystemInfo(outSYSTEM_INFOpSI);

[DllImport("rapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]

internalstaticexternboolCeGetVersionEx(outOSVERSIONINFOlpVersionInformation);

[DllImport("rapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]

internalstaticexternboolCeGetSystemPowerStatusEx(outSYSTEM_POWER_STATUS_EXpStatus,boolfUpdate);

[DllImport("rapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]

internalstaticexternvoidCeGlobalMemoryStatus(outMEMORYSTATUSmsce);

#endregion

#region声明结构

///

///处理器架构(CeGetSystemInfo)

///

publicenumProcessorArchitecture:

short

{

///

///Intel

///

Intel=0,

///

///MIPS

///

MIPS=1,

///

///Alpha

///

Alpha=2,

///

///PowerPC

///

PPC=3,

///

///HitachiSHx

///

SHX=4,

///

///ARM

///

ARM=5,

///

///IA64

///

IA64=6,

///

///Alpha64

///

Alpha64=7,

///

///Unknown

///

Unknown=-1

}

///

///移动设备内存信息

///

[StructLayout(LayoutKind.Sequential)]

publicstructMEMORYSTATUS

{

internaluintdwLength;

///

///当前内存占用(%)

///

publicintdwMemoryLoad;

///

///物理内存总量

///

publicintdwTotalPhys;

///

///可用物理内存

///

publicintdwAvailPhys;

///

///分页数

///

publicintdwTotalPageFile;

///

///未分页

///

publicintdwAvailPageFile;

///

///虚拟内存总量

///

publicintdwTotalVirtual;

///

///可用虚拟内存

///

publicintdwAvailVirtual;

}

///

///移动设备电源信息

///

publicstructSYSTEM_POWER_STATUS_EX

{

///

///交流电状态

///

publicbyteACLineStatus;

///

///电池充电状态。

1High,2Low,4Critical,8Charging,128Nosystembattery,255Unknownstatus

///

publicbyteBatteryFlag;

///

///电池电量剩余百分比

///

publicbyteBatteryLifePercent;

///

///保留字段,设置为0

///

internalbyteReserved1;

///

///电池电量剩余时间(秒)

///

publicintBatteryLifeTime;

///

///电池充满电的总可用时间(秒)

///

publicintBatteryFullLifeTime;

///

///保留字段,设置为0

///

internalbyteReserved2;

///

///后备电池状态

///

publicbyteBackupBatteryFlag;

///

///后备电池剩余电量百分比

///

publicbyteBackupBatteryLifePercent;

///

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 考试认证 > 交规考试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2