bool wceRunProcess(const char *process, const char *args) { HRESULT hRapiResult; HRESULT hr; PROCESS_INFORMATION pi; WCHAR wszProgram[MAX_PATH]; WCHAR wszArgs[MAX_PATH]; #ifdef UNICODE int nResult = 0; nResult = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, process, strlen(process)+1, wszProgram, ARRAYSIZE(wszProgram)); if(0 == nResult) { return false;} if (args) { nResult = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, args, strlen(args)+1, wszArgs, ARRAYSIZE(wszArgs)); if(0 == nResult) return false; } #else hr = StringCchCopy(wszProgram, ARRAYSIZE(wszProgram), argv[1]); if(FAILED(hr)) return true; #endif if (wceConnect()) { if (!CeCreateProcess(wszProgram, wszArgs, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi)) { _tprintf( TEXT("CreateProcess failed with Errorcode = %ld\n"), CeGetLastError()); return false; } CeCloseHandle( pi.hProcess); CeCloseHandle( pi.hThread); } wceDisconnect(); return true; }
int copyExecutable (TCHAR *file_name, TCHAR *app_dir) { TCHAR exe_fullpath[MAX_PATH]; int retval = 0; HANDLE hDest, hSrc; BYTE buffer[5120]; DWORD dwNumRead, dwNumWritten; USES_CONVERSION; _tcscpy(exe_fullpath, app_dir); _tcscat(exe_fullpath, _T("\\")); _tcscat(exe_fullpath, app_name); _tcscat(exe_fullpath, _T(".exe")); hSrc = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hSrc) { _tprintf( TEXT("Unable to open host file\n")); return EXIT_FAILURE; } hDest = CeCreateFile(exe_fullpath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hDest ) { _tprintf( TEXT("Unable to open target WinCE file\n")); return CeGetLastError(); } do { if(ReadFile(hSrc, &buffer, sizeof(buffer), &dwNumRead, NULL)) { if (!CeWriteFile(hDest, &buffer, dwNumRead, &dwNumWritten, NULL)) { _tprintf( TEXT("Error !!! Writing WinCE file\n")); goto copyFailure; } } else { _tprintf( TEXT("Error !!! Reading host file\n")); goto copyFailure; } _tprintf( TEXT(".")); } while (dwNumRead); _tprintf( TEXT("\n")); CeCloseHandle( hDest); CloseHandle (hSrc); return EXIT_SUCCESS; copyFailure: CeCloseHandle( hDest); CloseHandle (hSrc); /* if (wcePutFile (T2A(file_name), T2A(exe_fullpath))) return EXIT_SUCCESS; */ return EXIT_FAILURE; }
int copyBundle (TCHAR *parent_dir, TCHAR *file, TCHAR *app_dir) { HANDLE fileHandle; WIN32_FIND_DATAW findData; //DWORD dwError; TCHAR new_app_item[MAX_PATH]; TCHAR host_file[MAX_PATH]; HANDLE hFind; CE_FIND_DATA wceFindData; USES_CONVERSION; TCHAR wildcard[MAX_PATH + 16]; TCHAR fullpath[MAX_PATH]; _tcscpy(fullpath, parent_dir); _tcscat(fullpath, _T("\\")); _tcscat(fullpath, file); //TODO: check for fullpath is a dir _tcscpy(wildcard, fullpath); _tcscat(wildcard, _T("\\*.*")); //wceConnect (); fileHandle = FindFirstFile(wildcard, &findData); if (fileHandle == INVALID_HANDLE_VALUE) { printf ("Failed to open file\n"); wceDisconnect(); return EXIT_FAILURE; } /* if (_tcscmp (_T("."), findData.cFileName) != 0 && _tcscmp (_T(".."), findData.cFileName) != 0) { printf("-- %s\n", T2A(findData.cFileName)); if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { copyBundle (fullpath, findData.cFileName, NULL); } } */ HANDLE hDest, hSrc; BYTE buffer[5120]; DWORD dwNumRead, dwNumWritten; while (FindNextFile(fileHandle, &findData)) { if (_tcscmp (_T("."), findData.cFileName) == 0 || _tcscmp (_T(".."), findData.cFileName) == 0) continue; _tcscpy(new_app_item, app_dir); _tcscat(new_app_item, _T("\\")); _tcscat(new_app_item, findData.cFileName); if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { //Check and create dir on device hFind = CeFindFirstFile(new_app_item, &wceFindData); if (INVALID_HANDLE_VALUE == hFind) { if (!CeCreateDirectory(new_app_item, NULL)) { _tprintf( TEXT("Create directory \"%s\" on device\n"), new_app_item); printf ("Failed to create new directory on device\n"); return EXIT_FAILURE; } } FindClose( hFind); copyBundle (fullpath, findData.cFileName, new_app_item); } else { _tcscpy(host_file, fullpath); _tcscat(host_file, _T("\\")); _tcscat(host_file, findData.cFileName); _tprintf( TEXT("Copy file \"%s\" to device"), new_app_item); hSrc = CreateFile(host_file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hSrc) { _tprintf( TEXT("Unable to open host file\n")); return false; } hDest = CeCreateFile(new_app_item, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hDest ) { _tprintf( TEXT("Unable to open target WinCE file\n")); return false; } do { if(ReadFile(hSrc, &buffer, sizeof(buffer), &dwNumRead, NULL)) { if (!CeWriteFile(hDest, &buffer, dwNumRead, &dwNumWritten, NULL)) { _tprintf( TEXT("Error !!! Writing WinCE file\n")); goto copyBundleFailure; } } else { _tprintf( TEXT("Error !!! Reading host file\n")); goto copyBundleFailure; } _tprintf( TEXT(".")); } while (dwNumRead); _tprintf( TEXT("\n")); CeCloseHandle( hDest); CloseHandle (hSrc); /* if(!wcePutFile (T2A(host_file), T2A(new_app_item))) { printf("Failed to copy file."); return EXIT_FAILURE; } */ } } return EXIT_SUCCESS; copyBundleFailure: CeCloseHandle( hDest); CloseHandle (hSrc); return EXIT_FAILURE; }
bool wcePutFile(const char *host_file, const char *wce_file) { TCHAR tszSrcFile[MAX_PATH]; WCHAR wszDestFile[MAX_PATH]; BYTE buffer[5120]; WIN32_FIND_DATA wfd; HRESULT hr; DWORD dwAttr, dwNumRead, dwNumWritten; HANDLE hSrc, hDest, hFind; int nResult; #ifdef UNICODE nResult = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, host_file, strlen(host_file)+1, tszSrcFile, ARRAYSIZE(tszSrcFile)); if(0 == nResult) return false; #else hr = StringCchCopy(tszSrcFile, ARRAYSIZE(tszSrcFile), argv[1]); if(FAILED(hr)) return false; #endif nResult = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, wce_file, strlen(wce_file)+1, wszDestFile, ARRAYSIZE(wszDestFile)); if(0 == nResult) return false; hFind = FindFirstFile( tszSrcFile, &wfd); if (INVALID_HANDLE_VALUE == hFind) { _tprintf(TEXT("Host file does not exist\n")); return false; } FindClose( hFind); if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { _tprintf( TEXT("Host file specifies a directory\n")); return false; } if (wceConnect()) { dwAttr = CeGetFileAttributes( wszDestFile); if (dwAttr & FILE_ATTRIBUTE_DIRECTORY) { hr = StringCchCatW(wszDestFile, ARRAYSIZE(wszDestFile), L"\\"); if(FAILED(hr)) return false; #ifdef UNICODE hr = StringCchCatW(wszDestFile, ARRAYSIZE(wszDestFile), wfd.cFileName); if(FAILED(hr)) return false; #else nResult = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, wfd.cFileName, strlen(wfd.cFileName)+1, wszDestFile+wcslen(wszDestFile), ARRAYSIZE(wszDestFile)-wcslen(wszDestFile)); if(0 == nResult) { return 1; } #endif } hSrc = CreateFile(tszSrcFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hSrc) { _tprintf( TEXT("Unable to open host file\n")); return false; } hDest = CeCreateFile(wszDestFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == hDest ) { _tprintf( TEXT("Unable to open target WinCE file\n")); return false; } //copy file do { if(ReadFile(hSrc, &buffer, sizeof(buffer), &dwNumRead, NULL)) { if (!CeWriteFile(hDest, &buffer, dwNumRead, &dwNumWritten, NULL)) { _tprintf( TEXT("Error !!! Writing WinCE file\n")); goto FatalError; } } else { _tprintf( TEXT("Error !!! Reading host file\n")); goto FatalError; } _tprintf( TEXT(".")); } while (dwNumRead); //_tprintf( TEXT("\n")); CeCloseHandle( hDest); CloseHandle (hSrc); } wceDisconnect(); return true; FatalError: CeCloseHandle( hDest); CloseHandle (hSrc); wceDisconnect(); return false; }
int rapi_run(char *program, char *parameters) { int result = 1; HRESULT hr; WCHAR* wide_program = NULL; WCHAR* wide_parameters = NULL; PROCESS_INFORMATION info; char *tmpprogram = NULL; tmpprogram = (char *) strdup(program); hr = CeRapiInit(); if (RAPI_FAILED(hr)) { fprintf(stderr, "Unable to initialize RAPI: %s\n", synce_strerror(hr)); goto exit; } convert_to_backward_slashes(tmpprogram); wide_program = wstr_from_utf8(tmpprogram); if (parameters) wide_parameters = wstr_from_utf8(parameters); memset(&info, 0, sizeof(info)); if (!CeCreateProcess( wide_program, wide_parameters, NULL, NULL, false, 0, NULL, NULL, NULL, &info )) { fprintf(stderr, "Failed to execute '%s': %s\n", tmpprogram, synce_strerror(CeGetLastError())); goto exit; } CeCloseHandle(info.hProcess); CeCloseHandle(info.hThread); result = 0; exit: wstr_free_string(wide_program); wstr_free_string(wide_parameters); if (tmpprogram) free(tmpprogram); CeRapiUninit(); return result; }
int main( int argc, char *argv[]) { RAPIINIT ri; HRESULT hRapiResult; DWORD dwRet; PROCESS_INFORMATION pi; if (2 != argc) { _tprintf( TEXT("Syntax: PRUN <WinCE EXE>")); return 1; } else #ifdef UNICODE MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, argv[1], strlen(argv[1])+1, wszProgram, sizeof(wszProgram)); #else _tcscpy( wszProgram, argv[1]); #endif _tprintf( TEXT("Connecting to Windows CE...")); ri.cbSize = sizeof(RAPIINIT); hRapiResult = CeRapiInitEx(&ri); dwRet = WaitForSingleObject(ri.heRapiInit, 500); if ((dwRet != WAIT_OBJECT_0) || !SUCCEEDED(ri.hrRapiInit)) { // Could not initialize Rapi CeRapiUninit(); _tprintf( TEXT("Failed\n")); return 1; } _tprintf( TEXT("Success\n")); if (!CeCreateProcess( wszProgram, NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi)) { _tprintf( TEXT("CreateProcess failed with Errorcode = %ld\n"), CeGetLastError()); } else { CeCloseHandle( pi.hProcess); CeCloseHandle( pi.hThread); } _tprintf( TEXT("Closing connection ...")); CeRapiUninit(); _tprintf( TEXT("Done\n")); return 0; }
int main(int argc, char** argv) { int result = 1; RapiConnection* connection = NULL; char* program = NULL; char* parameters = NULL; HRESULT hr; WCHAR* wide_program = NULL; WCHAR* wide_parameters = NULL; PROCESS_INFORMATION info; if (!handle_parameters(argc, argv, &program, ¶meters)) goto exit; if ((connection = rapi_connection_from_path(devpath)) == NULL) { fprintf(stderr, "%s: Could not find configuration at path '%s'\n", argv[0], devpath?devpath:"(Default)"); goto exit; } rapi_connection_select(connection); hr = CeRapiInit(); if (FAILED(hr)) { fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", argv[0], synce_strerror(hr)); goto exit; } convert_to_backward_slashes(program); wide_program = wstr_from_current(program); if (parameters) wide_parameters = wstr_from_current(parameters); memset(&info, 0, sizeof(info)); if (!CeCreateProcess( wide_program, wide_parameters, NULL, NULL, false, 0, NULL, NULL, NULL, &info )) { fprintf(stderr, "%s: Failed to execute '%s': %s\n", argv[0], program, synce_strerror(CeGetLastError())); goto exit; } CeCloseHandle(info.hProcess); CeCloseHandle(info.hThread); result = 0; exit: wstr_free_string(wide_program); wstr_free_string(wide_parameters); if (program) free(program); if (parameters) free(parameters); CeRapiUninit(); return result; }