/* (C) 2003-2007 Willem Jan Hengeveld * Web: http://www.xs4all.nl/~itsme/ * http://wiki.xda-developers.com/ * * $Id: rapiinit.cpp 1702 2008-02-25 13:07:00Z itsme $ */ #include #include #include "rapiinit.h" #include "stringutils.h" #if 0 #include #include typedef std::vector ByteVector; #define vectorptr(v) (&(v)[0]) std::Wstring ToWString(char* buf) { WCHAR *wbuf= new WCHAR[strlen(buf)+1]; _snwprintf(wbuf, strlen(buf), L"%hs", buf); wbuf[strlen(buf)]= 0; std::Wstring wstr(wbuf, wbuf+strlen(buf)); delete wbuf; return wstr; } std::Wstring ToWString(const std::string& str) { return ToWString((char*)str.c_str()); } #endif bool WaitForDevice() { HRESULT hr= CeRapiUninit(); char *szTitle= "ShipPhone"; RAPIINIT rinit; rinit.cbSize= sizeof(RAPIINIT); rinit.heRapiInit= NULL; rinit.hrRapiInit= -1; if (CeRapiInitEx(&rinit)) { // ERROR initializing rapi return false; } while (true) { // msgwait is used to be able to handle windows messages while waiting. DWORD res= MsgWaitForMultipleObjects(1, &rinit.heRapiInit, false, 300000, 0); if (res==WAIT_OBJECT_0) break; else if (res==WAIT_OBJECT_0+1) { // MsgWaitForMultipleObjects // returns WAIT_OBJECT_0+nCount, when a systemevent happened during the wait MSG msg; while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { debug("systemevent %08lx(%08lx %08lx)\n", msg.message, msg.lParam, msg.wParam); DispatchMessage(&msg); } } else if (res!=WAIT_TIMEOUT) { error("MsgWaitForMultipleObjects: res=%08lx", res); // ERROR waiting for rapi init return false; } else { res= MessageBox(0, "Please connect your XDA to activesync", szTitle, MB_RETRYCANCEL); if (res==IDCANCEL) { // User decided not to connect return false; } } } if (rinit.hrRapiInit) { // ERROR connecting to XDA return false; } return true; } bool CeCopyFileToDevice(const std::string& srcfile, const std::string& dstfile, bool bOverwrite) { WIN32_FIND_DATA wfd; HANDLE hFind = FindFirstFile( srcfile.c_str(), &wfd); if (INVALID_HANDLE_VALUE == hFind) { // ERROR: Source/host file does not exist return false; } FindClose( hFind); if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // ERROR: Source/host file specifies a directory return false; } std::string dstname= dstfile; bool bExists= false; DWORD dwAttr = CeGetFileAttributes( ToWString(dstname).c_str()); if (0xFFFFFFFF != dwAttr) { if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) { dstname += "\\"; dstname += wfd.cFileName; dwAttr = CeGetFileAttributes( ToWString(dstname).c_str()); // File already exists. -> true // dstname is directory -> false ( meaning something like \windows\dllname.dll is a directory ) if ((dwAttr != 0xFFFFFFFF)) if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) { debug("ERROR: CeCopyFileToDevice to directory: %s\n", dstname.c_str()); return false; } else bExists= true; } else { bExists= true; } } if (!bOverwrite && bExists) { debug("NOTE: CeCopyFileToDevice not overwriting file %s\n", dstname.c_str()); return true; } HANDLE hSrc = CreateFile( srcfile.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hSrc) { // ERROR: Unable to open source/host file return false; } HANDLE hDest = CeCreateFile( ToWString(dstname).c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hDest ) { CloseHandle (hSrc); // ERROR: Unable to open WinCE file return false; } // Copying srcfile to WCE: dstname ByteVector buffer; buffer.resize(32768); DWORD dwNumRead; do { if (ReadFile( hSrc, vectorptr(buffer), (DWORD)buffer.size(), &dwNumRead, NULL)) { DWORD dwNumWritten; if (!CeWriteFile( hDest, vectorptr(buffer), dwNumRead, &dwNumWritten, NULL)) { // ERROR: Writing WinCE file CeCloseHandle( hDest); CloseHandle (hSrc); return false; } } else { // ERROR: Reading source file CeCloseHandle( hDest); CloseHandle (hSrc); return false; } } while (dwNumRead); CeCloseHandle( hDest); CloseHandle (hSrc); return true; }