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

RAPI提供了一组文件管理方法

CeCopyFile:复制文件

CeCreateDirectory:创建目录

CeCreateFile:创建,打开文件、管道、通讯资源、磁盘设备或者控制台。返回一个句柄用来访问对象。

CeDeleteFile:删除文件

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

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

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

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

CeGetFileAttributes:返回指定文件和目录的属性

CeGetFileSize:获取指定文件的字节大小

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

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

CeReadFile:从文件指针处读取文件

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

任何RAPI操作都需要首先初始化和设备的连接

CeRapiInit():同步初始化设备

CeRapiInitEx():异步初始化设备,并返回一个事件句柄

操作完毕后在合适的时候断开RAPI连接

CeRapiUninit(): 断开或停止设备连接

下面是复制文件到PDA的一个例子:

        private const uint GENERIC_WRITE = 0x40000000;//设置读写权限
private const short CREATE_NEW = ;//创建新文件
private const short FILE_ATTRIBUTE_NORMAL = 0x80;//设置文件属性
private const short INVALID_HANDLE_VALUE = -;//错误句柄 private const int TimeOut = ;//异步连接设备超时时间2秒 private void btnCopyFileToPDA_Click(object sender, EventArgs e)
{
//1、初始化设备连接 ///---同步连接失败---
//int ret = CeRapiInit();
//if (ret != 0)
//{
// //连接失败,获取失败代码
// int error = CeRapiGetError();
// //抛出异常
// Marshal.ThrowExceptionForHR(ret);
//} ///---采用异步连接---
Rapiinit ri = new Rapiinit();
ri.cbsize = Marshal.SizeOf(ri);
uint hRes = CeRapiInitEx(ref ri);
ManualResetEvent me = new ManualResetEvent(false);
me.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(ri.heRapiInit, false);
if (!me.WaitOne(TimeOut, true))
{
CeRapiUninit();
} //2、PC复制文件到PDA IntPtr remoteFile = IntPtr.Zero;
String LocalFileName = @"D:\test.txt";//本地计算机文件名
String RemoteFileName = @"\ResidentFlash\Fire\test.txt";//远程设备文件名
byte[] buffer = new byte[0x1000];//传输缓冲区定义为4k
FileStream localFile; int bytesread = ;
int bytesrwritten = ;
int filepos = ; //创建远程文件
remoteFile = CeCreateFile(RemoteFileName, GENERIC_WRITE, , , CREATE_NEW, FILE_ATTRIBUTE_NORMAL, );
//检查文件是否创建成功
if ((int)remoteFile == INVALID_HANDLE_VALUE)
{
throw new Exception("Could not create remote file.");
}
else
{
//打开本地文件
localFile = new FileStream(LocalFileName, FileMode.Open);
//读取4k字节
bytesread = localFile.Read(buffer, filepos, buffer.Length);
while (bytesread > )
{
//移动文件指针到已读取的位置
filepos += bytesread;
//写缓冲区数据到远程设备文件
if (!Convert.ToBoolean(CeWriteFile(remoteFile, buffer, bytesread, ref bytesrwritten, )))
{
//检查是否成功,不成功关闭文件句柄,抛出异常
CeCloseHandle(remoteFile);
throw new Exception("Could not write to remote file.");
}
try
{
//重新填入本地缓冲区
bytesread = localFile.Read(buffer, , buffer.Length);
}
catch(Exception)
{
bytesread = ;
}
}
}
//关闭本地文件
localFile.Close();
//关闭远程文件
CeCloseHandle(remoteFile); //3、断开连接 文件句柄使用后一定要释放 CeRapiUninit(); } [StructLayout(LayoutKind.Explicit)]
private struct Rapiinit
{
[FieldOffset()]
public int cbsize;
[FieldOffset()]
public readonly IntPtr heRapiInit;
[FieldOffset()]
private readonly IntPtr hrRapiInit;
} [DllImport("rapi.dll")]
private static extern uint CeRapiInitEx(ref Rapiinit pRapiInt); [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiGetError(); [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiInit(); [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 dwDesireAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
internal static extern int CeRapiUninit();

MSDN:https://msdn.microsoft.com/zh-cn/ee497185

Remote API(RAPI)之 文件管理的更多相关文章

  1. Docker入门教程(八)Docker Remote API

    Docker入门教程(八)Docker Remote API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第八篇,重点介绍了Docker Remote ...

  2. 【转+自己研究】新姿势之Docker Remote API未授权访问漏洞分析和利用

    0x00 概述 最近提交了一些关于 docker remote api 未授权访问导致代码泄露.获取服务器root权限的漏洞,造成的影响都比较严重,比如 新姿势之获取果壳全站代码和多台机器root权限 ...

  3. Docker remote API简单配置使用

    1.启动docker remote API的方式如下: docker -d -H uninx:///var/run/docker.sock -H tcp://0.0.0.0:5678 2.但是为了伴随 ...

  4. V-REP Remote API(C++)实现简单的关节转动

    基础内容参考:https://www.cnblogs.com/eternalmoonbeam/p/10753149.html V-REP客户端设置: 在V-REP场景文件中需要添加三个实体,包括两个形 ...

  5. docker remote api enable in ubuntu

    现在使用docker作为开发环境,操作系统是ubuntu16.10,pycharm中使用remote interpreter,需要用到remote api,结果发现自己的原答案是针对ubuntu 14 ...

  6. Docker Remote API v1.24

    1. Brief introduction The Remote API has replaced rcli. The daemon listens on unix:///var/run/docker ...

  7. 关于docker remote api未授权访问漏洞的学习与研究

    漏洞介绍: 该未授权访问漏洞是因为docker remote api可以执行docker命令,从官方文档可以看出,该接口是目的是取代docker 命令界面,通过url操作docker. docker ...

  8. docker 开启remote api

    docker官方文档上有相关说明(Configure and run Docker on various distributions),ubuntu上是可行的 sudo vi /etc/default ...

  9. Docker开启Remote API 访问 2375端口

    Docker常见端口 我看到的常见docker端口包括: 2375:未加密的docker socket,远程root无密码访问主机2376:tls加密套接字,很可能这是您的CI服务器4243端口作为h ...

  10. Docker remote API

    Docker remote API 该教程基于Ubuntu或Debian环境,如果不是,请略过本文 Docker API 在Docker生态系统中一共有三种API Registry API:提供了与来 ...

随机推荐

  1. SQL中的DATENAME()函数

    SQL从时间字段值中获取年份使用DATENAME()函数. DATENAME()函数语法:DATENAME(param,date) date是时间字段名 或一个时间值 param是指定要返回日期部分的 ...

  2. Spring的四种事务特性,五种隔离级别,七种传播行为

    Spring事务: 什么是事务: 事务逻辑上的一组对数据对操作,组成这些操作的各个逻辑单元,要么一起成功,要么一起失败. 事务特性(4种): 原子性(atomicity):强调事务的不可分割:一致性( ...

  3. Python与用户的交互

    目录 Python与用户的交互 为什么交互 如何交互 Python2 中的交互 Python与用户的交互 为什么交互 让我们来回顾计算机的发明有何意义,计算机的发明是为了奴役计算机,解放劳动力.假设我 ...

  4. Spring实战(十)Spring AOP应用——为方法引入新功能、为对象引入新方法

    切面最基本的元素是通知和切点,切点用于准确定位应该在什么地方应用切面的通知. 1.Spring借助AspectJ的切点表达式语言来定义Spring切面 在Spring中,要使用AspectJ的切点表达 ...

  5. mysql 8.x 集群出现:Last_IO_Error: error connecting to master 'repl@xxx:3306' - retry-time: 60 retries: 1

    网上的经验:网络不同,账号密码不对,密码太长,密码由 # 字符:检查MASTER_HOST,MASTER_USER,MASTER_PASSWORD(不知道 MASTER_LOG_FILE 有没有影响) ...

  6. ptf转图片

    1.spire 官方的有水印,通过引用 //private readonly static PdfDocument doc = new PdfDocument(); //public static S ...

  7. JS基础_break和continue

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 一行python能干什么?

    我们都知道,python作为一个编程语言,它有一个最大的优势就是代码简短,那么一行python代码能实现哪些操作呢?一起来看看吧! 1.打印Hello World! 这是最基础的,相信不管学习哪一门语 ...

  9. git pull文件时和本地文件冲突 方法之一

    1.先将本地修改存储起来 2.pull内容 3.还原暂存的内容 4.解决文件中冲突的的部分 打开 dsa.txt 文件手动解决冲突. 其中Updated upstream 和=====之间的内容就是p ...

  10. 线程----code

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...