#include
#include
#include
#include
#include
#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
void PrintDirectory(LPWSTR cePath, LPWSTR pcPath ,UINT Indent)
{
DWORD foundCount;
LPCE_FIND_DATA findDataArray;
HRESULT hr;
// FILE *stream, *stream2;
WCHAR searchPath[MAX_PATH];
hr = StringCchCopyW(searchPath, ARRAYSIZE(searchPath), cePath);
if(FAILED(hr))
{
return;
}
hr = StringCchCatW(searchPath, ARRAYSIZE(searchPath), L"*");
if(FAILED(hr))
{
return;
}
if(!CeFindAllFiles(searchPath,
FAF_ATTRIBUTES | FAF_NAME,
&foundCount,
&findDataArray))
{
_tprintf( TEXT("*** CeFindAllFiles failed. ***\n"));
return;
}
if(!foundCount)
return;
for(UINT i = 0; i < foundCount; i++)
{
for(UINT indCount = 0; indCount < Indent; indCount++)
_tprintf( TEXT(" "));
if(findDataArray.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tprintf( TEXT("[%s]\n"),findDataArray.cFileName);
//CePath
WCHAR newcePath[MAX_PATH];
hr = StringCchCopyW(newcePath, ARRAYSIZE(newcePath), cePath);
if(FAILED(hr))
{
return;
}
hr = StringCchCatW(newcePath, ARRAYSIZE(newcePath), findDataArray.cFileName);
if(FAILED(hr))
{
return;
}
hr = StringCchCatW(newcePath, ARRAYSIZE(newcePath), L"\\");
if(FAILED(hr))
{
return;
}
//PcPath
WCHAR newpcPath[MAX_PATH];
hr = StringCchCopyW(newpcPath, ARRAYSIZE(newpcPath), pcPath);
if(FAILED(hr))
{
return;
}
hr = StringCchCatW(newpcPath, ARRAYSIZE(newpcPath), findDataArray.cFileName);
if(FAILED(hr))
{
return;
}
if(!CreateDirectoryW(newpcPath,NULL))
{
_tprintf( TEXT("创建目录失败"));
return;
}
hr = StringCchCatW(newpcPath, ARRAYSIZE(newpcPath), L"\\");
if(FAILED(hr))
{
return;
}
PrintDirectory(newcePath, newpcPath,Indent + 1);
}
else
{
_tprintf( TEXT("%s\n"),findDataArray.cFileName);
HANDLE remoteFile;
byte buffer[0x5000]; // 传输缓冲区定义为4k
//得到CE文件全名(包括路径)
WCHAR cePathFileName[MAX_PATH];
hr = StringCchCopyW(cePathFileName, ARRAYSIZE(cePathFileName),cePath);
hr = StringCchCatW(cePathFileName, ARRAYSIZE(cePathFileName), findDataArray.cFileName);
//得到PC文件全名(包括路径)
WCHAR pcPathFileName[MAX_PATH];
hr = StringCchCopyW(pcPathFileName, ARRAYSIZE(pcPathFileName), pcPath);
hr = StringCchCatW(pcPathFileName, ARRAYSIZE(pcPathFileName), findDataArray.cFileName);
remoteFile = CeCreateFile(cePathFileName, GENERIC_READ, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
// 读取4K字节
DWORD bytesread;
DWORD size = CeGetFileSize(remoteFile,&bytesread);
long iresult = CeReadFile(remoteFile, buffer, size, &bytesread, 0);
// 检查文件是否读取成功
if (remoteFile == INVALID_HANDLE_VALUE)
{
_tprintf(_T("Could not read remote file "));
}
HANDLE hFile = CreateFile(pcPathFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ| FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == NULL || !CloseHandle(hFile))
{
_tprintf(_T("Could not Creat file "));
}
FILE *pf;
pf = _wfopen(pcPathFileName,L"w+b");
size_t xx = fwrite(buffer,sizeof( byte ),size,pf);
fclose(pf);
// 关闭远程文件
CeCloseHandle(remoteFile);
}
}
if (findDataArray)
{
RapiFreeBuffer(findDataArray);
}
}
void main()
{
HRESULT hRapiResult;
_tprintf( TEXT("Connecting to Windows CE..."));
hRapiResult = CeRapiInit();
if (FAILED(hRapiResult))
{
_tprintf( TEXT("Failed\n"));
return;
}
_tprintf( TEXT("Success\n"));
// PrintDirectory( L"\\Temp\\", L"c:\\",0);
CeRapiUninit();
}
以上是用RAPI类库实现的设备到PC的目录复制,在VS2005上编译通过.如有疑问发MAIL:baishaf@163.com