コード例 #1
0
ファイル: detool.cpp プロジェクト: LittleForker/rhodes
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;
}
コード例 #2
0
int _tmain(int argc, _TCHAR* argv[])
{
	
	// Initialize RAPI
	HRESULT hr = CeRapiInit();

	hr = CeDeleteFile(L"\\Program Files\\shell\\avmshell.exe");


	if (argc>1) {

	// Filename will be passed in argv[0]
	HANDLE hFile = ::CreateFile(argv[1], 
								GENERIC_READ, FILE_SHARE_READ,
								NULL, OPEN_EXISTING,
								FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile==INVALID_HANDLE_VALUE)
	{
		printf("Error copying file %s to device",argv[1]);
		CeRapiUninit();
		return -1;
	}


	// Create the file on the device in the temp directory
	WCHAR ceFilename[MAX_PATH];
	DWORD dwmblen = ::MultiByteToWideChar(CP_ACP, MB_COMPOSITE, (const char *)argv[1], strlen((const char *)argv[1]), ceFilename, MAX_PATH); 
	ceFilename[dwmblen] = 0;


	WCHAR cePath[MAX_PATH];
	HANDLE ceFile = CeCreateFile(L"\\Program Files\\shell\\avmshell.exe", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);

	char localBuf[4096];
	DWORD dwBufsize=4096, dwFilesize = ::GetFileSize(hFile, NULL);
	if (dwFilesize<4096)
		dwBufsize = dwFilesize;


	DWORD dwCeBytes=0, dwRead;
	while (ReadFile(hFile, localBuf, dwBufsize, &dwRead, NULL))
	{
		hr = CeWriteFile(ceFile, (void*)localBuf, dwRead, &dwCeBytes, NULL);
		dwFilesize -= dwRead;
		if (dwFilesize<4096)
			dwBufsize = dwFilesize;
		if (dwBufsize<=0)
			break;
	}
	}
	// Uninitialize RAPI
	CeRapiUninit();
	return 0;
}
コード例 #3
0
ファイル: detool.cpp プロジェクト: Aerodynamic/rhodes
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;
}
コード例 #4
0
ファイル: detool.cpp プロジェクト: Aerodynamic/rhodes
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;
}