📄 copyfiletodevice.cpp
字号:
//////////////////////////////////////////////////////////////////////
//
// 文件:CopyFileToDevice.h
//
// 功能:将pc机中的txt文件拷贝到移动设备中
//
// 作者:wanghw
//
// 时间:2007.08.31
//
// 版本:v1.0
//
// 修改:无
//
//////////////////////////////////////////////////////////////////////
// CopyFileToDevice.cpp: implementation of the CCopyFileToDevice class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "工程的app.h"
#include "CopyFileToDevice.h"
#include "rapi.h" //引入rapi
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCopyFileToDevice::CCopyFileToDevice()
{
bInitOK = FALSE;
}
CCopyFileToDevice::~CCopyFileToDevice()
{
//注销dll
CeRapiUninit();
}
/////////////////////////////////////////////////////////////////////
//
// 函数:Init
// 功能:初始化类
// 输入:NULL
// 输出:是否初始化成功,TRUE:成功,FALSE:失败
// 修改:无
//
/////////////////////////////////////////////////////////////////////
BOOL CCopyFileToDevice::Init()
{
RAPIINIT struRapiInit; //这个是CeRapiInitEx函数要求的入口参数
DWORD dwWaitResult = 0; //等待初始化完成事件的变量
HRESULT hRapiResult = NULL; //CeRapiInitEx的返回HANDLE
struRapiInit.cbSize = sizeof(RAPIINIT); //填满该结构体仅有的三个成员
struRapiInit.hrRapiInit = NULL; //明知是输出参数也顺手填一下, 我以前吃过亏, 惊弓之鸟
struRapiInit.heRapiInit = NULL;
hRapiResult = CeRapiInitEx(&struRapiInit); //关键点
dwWaitResult = WaitForSingleObject(struRapiInit.heRapiInit, 2000); //关键点
if( hRapiResult == S_OK &&
struRapiInit.hrRapiInit == S_OK &&
dwWaitResult != WAIT_TIMEOUT) //保守起见, 三个返回值都判断
{
bInitOK = TRUE;
return TRUE;
}
else
{
AfxMessageBox(_T("连接移动设备初始化失败,无法连接移动设备!"));
bInitOK = FALSE;
return FALSE;
}
}
/////////////////////////////////////////////////////////////////////
//
// 函数:MoveFile
// 功能:将pc机上的txt文件移动到移动设备上
// 输入:pc机上txt文件的全路径名
// 输出:BOOL,TRUE:成功,FALSE:失败
// 修改:无
//
/////////////////////////////////////////////////////////////////////
BOOL CCopyFileToDevice::MoveFile(CString strFile)
{
if (!bInitOK)
{
return FALSE;
}
CString strSrcFile; //pc机上文件
CString strNewFile; //新建的临时文件
CString strDestFile; //待发送文件
CString strFileName; //文件名
WCHAR wNewFile[MAX_PATH];
WCHAR wDestFile[MAX_PATH];
LPCWSTR pNewFile;
LPCWSTR pDestFile;
BOOL bSendOK;
bSendOK = FALSE;
strSrcFile = strFile;
//取文件名
strFileName = GetFileNameByPath(strSrcFile);
//设置移动设备上新建的临时文件的名称
strNewFile = _T("\\ResidentFlash\\Temp\\" + strFileName);
//设置移动设备上待发送文件的名称
strDestFile = _T("\\ResidentFlash\\Manage\\" + strFileName);
//文件全路径名转换:CString->LPCWSTR
ZeroMemory(&wNewFile, sizeof(WCHAR)*MAX_PATH);
MultiByteToWideChar(CP_ACP, 0, strNewFile, -1, wNewFile, MAX_PATH);
pNewFile = wNewFile;
//文件全路径名转换:CString->LPCWSTR
ZeroMemory(&wDestFile, sizeof(WCHAR)*MAX_PATH);
MultiByteToWideChar(CP_ACP, 0, strDestFile, -1, wDestFile, MAX_PATH);
pDestFile = wDestFile;
//在移动设备上创建临时文件
if (!CreateFile(pNewFile, strSrcFile))
{
return FALSE;
}
//将临时文件拷贝到目标文件夹下
bSendOK = CeCopyFile(pNewFile, pDestFile, TRUE);
if (bSendOK)
{
//删除移动设备上的临时文件
CeDeleteFile(pNewFile);
}
//设置pc机上文件的属性为normal
::SetFileAttributes(strSrcFile, FILE_ATTRIBUTE_NORMAL);
//删除pc机上的临时文件
::DeleteFile(strSrcFile);
return TRUE;
}
/////////////////////////////////////////////////////////////////////
//
// 函数:CreateFile
// 功能:在移动设备上新建文件
// 输入:lpNewFile:新建文件的全路径名,szDesktopFile:pc机上
// 文件的全路径名
// 输出:BOOL,TRUE:成功,FALSE:失败
// 修改:无
//
/////////////////////////////////////////////////////////////////////
BOOL CCopyFileToDevice::CreateFile(LPCWSTR lpNewFile, CString szDesktopFile)
{
LPCWSTR pNewFile = lpNewFile;
CString strDesktopFile = szDesktopFile;
//将pc机上的txt文件读入到内存(data)中
CFile srcFile;
if (!(srcFile.Open(strDesktopFile, CFile::modeRead | CFile::typeBinary)))
{
return FALSE;
}
long myFileLength = srcFile.GetLength(); //文件长度
byte* data = new byte[myFileLength];
memset(data, 0, myFileLength);
srcFile.Read(data, myFileLength);
srcFile.Close();
//在移动设备上创建新的txt文件
HANDLE hCreateFile;
DWORD dWriten;
hCreateFile = CeCreateFile(pNewFile, GENERIC_WRITE, FILE_SHARE_WRITE, \
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
CeWriteFile(hCreateFile, data, myFileLength, &dWriten, NULL);
CeCloseHandle(hCreateFile);
//delete
delete[] data;
return TRUE;
}
/////////////////////////////////////////////////////////////////////
//
// 函数:GetFileNameByPath
// 功能:根据文件的全路径名得到文件名
// 输入:文件的全路径名
// 输出:文件名
// 修改:无
//
/////////////////////////////////////////////////////////////////////
CString CCopyFileToDevice::GetFileNameByPath(CString szPath)
{
CString strPath = szPath;
CString strFileName = _T("");
int nPos;
nPos = strPath.ReverseFind('\\');
if (nPos != -1)
{
strFileName = strPath.Right(strPath.GetLength() - nPos - 1);
}
return strFileName;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -