从PDA上拷贝文件到PC

微软提供一套远程API(RAPI)来从PC端操作移动设备,下面是把 CE 中的文件拷贝到 PC 端的一个函数,使用了RAPI中的以下几个函数:
CeCreateFile // 创建一个文件句柄
CeReadFile // 读文件函数
CeCloseHandle // 关闭文件句柄

BOOL CopyFileFromCEToPC(CString strPCfile, CString strCEfile)
{
    HANDLE    hSrcFile = NULL;        // ce file handle
    HANDLE    hDestFile = NULL;        // dest file handle
    DWORD    dwError;            // retrieve error code   

    // Opening ce file
    TRACE("Opening src file .../n");
    WCHAR    wchSrcFile[MAX_PATH];
    memset(wchSrcFile, 0, sizeof(wchSrcFile));
    wcscpy(wcSrcPath, strCEfile.AllocSysString());
    hSrcFile = CeCreateFile(wchSrcFile, GENERIC_READ, 0/*cannot be shared*/, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hSrcFile != INVALID_HANDLE_VALUE)
    {
        // ce file be opened
        TRACE("Open src file successed!/n");

        // Opening dest file
        hDestFile = CreateFile(dest, GENERIC_WRITE, 0/*cannot be shared*/, NULL,
            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

        if(hDestFile != INVALID_HANDLE_VALUE)
        {
            // dest file be opened
            TRACE("Create dest file successed!/n");

            // read ce file and write to dest file
            TRACE("reading ce file and write to dest file .../n");
            BOOL    bResult;                    // return values of read and write file
            const int BUFFER_SIZE = 512*1024;    // 512k Bytes, buffer bytes size for receives data
            char szBuffer[BUFFER_SIZE];            // buffer for receives data
            DWORD dwNumOfBytesRead;                // pointer to number of bytes read
            memset(szBuffer, 0, sizeof(szBuffer)*sizeof(char));
            bResult = CeReadFile(hSrcFile, szBuffer, BUFFER_SIZE, &dwNumOfBytesRead, NULL);
            while(bResult && dwNumOfBytesRead>0)
            {
                //bResult = WriteFile(hDestFile, szBuffer, BUFFER_SIZE, &dwNumOfBytesRead, NULL);
                bResult = WriteFile(hDestFile, szBuffer, dwNumOfBytesRead, &dwNumOfBytesRead, NULL);
                if (!bResult)
                {
                    dwError = GetLastError();
                    TRACE("%s, %d/n", "write dest file error, error code is: ", dwError);                                   
                    CloseHandle(hSrcFile);
                    CloseHandle(hDestFile);
                    return FALSE;
                }
               
                memset(szBuffer, 0, sizeof(szBuffer)*sizeof(char));
                bResult = CeReadFile(hSrcFile, szBuffer, BUFFER_SIZE, &dwNumOfBytesRead, NULL);
            }
        }
        else
        {
            // dest file can't be opened
            dwError = GetLastError();
            TRACE("%s, %d/n", "Open dest file failed, error code is: ", dwError);
            CeCloseHandle(hSrcFile);
            CloseHandle(hDestFile);
            return FALSE;
        }
    }
    else
    {
        // ce file can't be opened   
        dwError = GetLastError();
        TRACE("%s, %d/n", "Open src file failed, error code is: ", dwError);       
        CeCloseHandle(hSrcFile);
        return FALSE;
    }

    // close file
    TRACE("reading ce file and write to dest file is successful!/n");
    CeCloseHandle(hSrcFile);
    CloseHandle(hDestFile);
    return TRUE;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值