Click here to Skip to main content
15,883,841 members
Articles / Mobile Apps

Command-line utility for PocketPC

Rate me:
Please Sign up or sign in to vote.
3.61/5 (18 votes)
26 Jan 2004CPOL1 min read 130.5K   506   48  
RAPI based utility for copying files and folders PC->PocketPC (and vice versa). There is also - "Dir, Del, Db" commands.
//
//  Author: Alexander Shilonosov, 
//	shilo@dekart.com, Aug 2003.
//
//  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND "NO WARRANTY" terms .
//
//
//  CRAPI.h
//
//
//
/*
 How recursion works here:
 
   For example when you execute "copy()"
   copy (call) -> enumFiles (for each file/folder call) -> doActionFile, doActionEnterFolder, doActionExitFolder
  
   
*/

/*
History:

  Dec 2003
  encounter an error while printing DB string-records.

  Aug 2002 Year:
  first release
 

  */


#include "Rapi1.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


//
// convert File size to "human" string (ie 1.4Mb, or 0.9Kb)
//  to Bytes if size < 100
//  to Kb if size > 990 bytes
//  to Mb if size > 990 Kb 
LPCTSTR itoaFileSize(DWORD size, LPTSTR out = NULL)
{
	float sz = (float)size;
	static TCHAR str[50];
	LPTSTR pStr;
	TCHAR postfix[5];
	
	if (out)
		pStr = out;
	else
		pStr = str;	
		
	_tcscpy(postfix, TEXT("bytes") );
	if ( sz > 99.9) {
		_tcscpy(postfix, TEXT("Kb") );
		sz = sz / 1024; 
		if ( sz > 99.9) {
			_tcscpy(postfix, TEXT("Mb") );
			sz /= 1024; 
			if ( sz > 99.9){
				_tcscpy(postfix, TEXT("Gb") );
				sz = sz / 1024; 
			}
		}
		
		_stprintf(pStr, TEXT("%.1f %s"), sz, postfix);
		
	}  else
		_stprintf(pStr, TEXT("%d bytes"), size);

	 return pStr;
}


CRapi::CRapi()
{

}

CRapi::~CRapi()
{

	  CeRapiUninit();
}


bool CRapi::Init()
{
	HRESULT hRapiResult;

    PrintMsg( TEXT("Connecting to Windows CE..."));
    
    hRapiResult = CeRapiInit();

    if (FAILED(hRapiResult)) {
        PrintMsg( TEXT("Failed\n"));
        return false;
    }

	_tcscpy(curr_dir, _TEXT("\\") );
	m_TotalCopySize = m_TotalFiles = m_TotalFolders=0;
	m_bRecursive = true; // only DIR dont need recursion

	// Getting free space on Device...
	if ( !CeGetStoreInformation( &m_ppc_store ) )
		PrintMsg( TEXT("Erro� getting Free Space...\n"));	
	PrintMsg( TEXT("Free: %s"), itoaFileSize(m_ppc_store.dwFreeSize) );

	return true;
}

// Output to console
//
void CRapi::PrintMsg(LPCTSTR msg, ... )
{
	va_list		ArgList;
	TCHAR		szOutString[512];

	va_start(ArgList, msg);	
	_vstprintf(szOutString, msg, ArgList);	
	va_end(ArgList);	

	_tprintf(szOutString);
	printf( "\n");

	if ( !_tcsncmp(msg, "Error", 5) ) {
		printf("GetLastError = %X %X (%X)\n Current Folders: %s %s\n exit...",
			CeGetLastError(), CeRapiGetError(), GetLastError(),
			curr_dir, curr_dir1);
		ExitProcess(1);
	}
}


/*
Show files and folders

dir c:\*
dir \temp\*
dir "\my documents\*" 
  */
int CRapi::dir(LPCTSTR par1, LPCTSTR par2, LPCTSTR par3, LPCTSTR par4)
{
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];		
	

		m_bRecursive = false; // Print only files from one folder
		_splitpath(par1, drive, dir, fname, ext);

		// the ":" mean that files will be copied from Desktop to PPC
		if( par1[1]==':') {
			// path from local PC 
						
		}		
		else {
			// path from PPC
			WCHAR path[MAX_PATH];

			// construct path string
			strcpy(curr_dir, drive);
			strcat(curr_dir, dir);			
			MultiByteToWideChar(CP_ACP, 0, par1, -1, path, MAX_PATH );

			enumFiles2(path, doDirPPC);

			PrintMsg( TEXT("%d files [%s]: %d folders."), m_TotalFiles, itoaFileSize(m_TotalCopySize), m_TotalFolders);

		}	
	return 0;
	
}

//
// copy files (with subfolders, by mask) between Destop <-> PocketPC.
//
// parameters:
//  par1 - path to copy from 
//  par2 - path to copy to
//
//  The paths may be from Desktop or PockePC (but both can not be from one target)
int CRapi::copy(LPCTSTR par1, LPCTSTR par2, LPCTSTR par3, LPCTSTR par4)
{	
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];		
	
	__try {
			_splitpath(par1, drive, dir, fname, ext);			

		// the ":" mean that files will be copied from Desktop to PPC
		if( par1[1]==':') {
			// do copy PC -> PPC
			strcpy(curr_dir1, drive);
			strcat(curr_dir1, dir);
			_tcscpy(curr_dir, par2);
			if ( curr_dir[strlen(curr_dir)-1] != '\\')
				strcat(curr_dir, "\\");		
			
			enumFiles1(par1, doCountFileSizesPC);						 
			PrintMsg( TEXT("Total files: %s"), itoaFileSize(m_TotalCopySize));			
			enumFiles1(par1, doCopyToPPC);
		}		
		else {
			// PPC -> PC
			WCHAR path[MAX_PATH];

			strcpy(curr_dir, drive);
			strcat(curr_dir, dir);
			_tcscpy(curr_dir1, par2);
			if ( curr_dir[strlen(curr_dir1)-1] != '\\')
				strcat(curr_dir1, "\\");
			
			MultiByteToWideChar(CP_ACP, 0, par1, -1, path, MAX_PATH );			

			enumFiles2(path, doCountFileSizesPPC);						 
			PrintMsg( TEXT("Total files: %s"), itoaFileSize(m_TotalCopySize));						
			enumFiles2(path,doCopyToPC);			

		}
		
		return 1;
	}
	
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::copy] \n");
	}

	return 0;
}

//
// copy files PC -> PPC
//
int CRapi::CopyFile1(LPCTSTR srcFile, LPCTSTR destFile) {
	char buff[5006];
	HANDLE hSrc,hDest;
	DWORD dwSz, dwRes;

	__try {
		WCHAR destFile1[MAX_PATH];
		MultiByteToWideChar(CP_ACP, 0, destFile, -1, destFile1, MAX_PATH );
		
		
		hSrc = CreateFile( srcFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
		if (hSrc == INVALID_HANDLE_VALUE) { 
			PrintMsg("Error Create File %s", srcFile);
			return 0;
		}

		hDest = CeCreateFile( destFile1, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0);
		if (hDest== INVALID_HANDLE_VALUE) { 
			PrintMsg("Error Create File at PPC: %s", destFile);
			return 0;
		}

		dwSz=5000;

		while (1) { 
			ReadFile(hSrc, buff, dwSz, &dwRes, NULL);
			if (dwRes==0) break;
			CeWriteFile(hDest, buff, dwRes, &dwRes, NULL );
			if (dwRes!=dwSz) break;
		} 

		CloseHandle(hSrc);
		CeCloseHandle(hDest);

		
		//
		
	}
	
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::CopyFile1] \n");
	}

	return 0;
	
}



//
// copy files PPC -> PC
//
int CRapi::CopyFile2(LPCTSTR srcFile, LPCTSTR destFile) {
	char buff[5006];
	HANDLE hSrc,hDest;
	DWORD dwSz, dwRes;

	__try {
		WCHAR srcFile1[MAX_PATH];
		MultiByteToWideChar(CP_ACP, 0, srcFile, -1, srcFile1, MAX_PATH );
		
		
		hDest = CreateFile( destFile,  GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,  CREATE_ALWAYS, 0, 0);
		if (hDest == INVALID_HANDLE_VALUE) { 
			PrintMsg("Error Create File %s", destFile);
			return 0;
		}

		hSrc = CeCreateFile( srcFile1, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
		if (hSrc == INVALID_HANDLE_VALUE){ 
			PrintMsg("Error Create File at PPC %s", srcFile);
			return 0;
		}

		dwSz=5000;

		while (1) { 
			CeReadFile(hSrc, buff, dwSz, &dwRes, NULL);
			if (dwRes==0) break;
			WriteFile(hDest, buff, dwRes, &dwRes, NULL );
			if (dwRes!=dwSz) break;
		} 

		CloseHandle(hDest);
		CeCloseHandle(hSrc );
	}
	
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::CopyFile2] \n");
	}

	return 0;
	
}


//
//
// Enum files on PPC and call doActionFile(files_name)
//
//
void CRapi::enumFiles2(LPWSTR path, int action)
{
	
	DWORD      foundCount;
    LPCE_FIND_DATA findDataArray;
	TCHAR file[MAX_PATH];
	WCHAR newPath[MAX_PATH];
	TCHAR path2[MAX_PATH];

	
		__try{
	

			WideCharToMultiByte(CP_ACP, 0, path, -1, path2, MAX_PATH, NULL, NULL );
			
			if(!CeFindAllFiles(path,
				FAF_ATTRIBUTES | FAF_NAME | FAF_SIZE_LOW,
				&foundCount,
				&findDataArray))
			{
				PrintMsg( "Erro Dir Files at PPC: %s", path2);
				return;
			}
			
			if(!foundCount)
				return;
			
			for(UINT i = 0; i < foundCount; i++)
			{
				
				if(findDataArray[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{					
					//wcscpy(newPath, path);
					MultiByteToWideChar(CP_ACP, 0, curr_dir, -1, newPath, MAX_PATH );
					wcscat(newPath, findDataArray[i].cFileName);
					wcscat(newPath, L"\\*");

					WideCharToMultiByte(CP_ACP, 0, findDataArray[i].cFileName, -1, file, MAX_PATH, NULL, NULL );
					m_TotalFolders++;
					doActionEnterFolder(file, action, &findDataArray[i]);
					if ( m_bRecursive)
						enumFiles2(newPath, action);
					doActionExitFolder(file,action);
					
				}
				else{
					//wcscpy(newPath, curr_dir);
					MultiByteToWideChar(CP_ACP, 0, curr_dir, -1, newPath, MAX_PATH );
					wcscat(newPath,  findDataArray[i].cFileName);	

					WideCharToMultiByte(CP_ACP, 0, newPath, -1, file, MAX_PATH, NULL, NULL );
					m_TotalFiles++;
					doActionFile(file, action, &findDataArray[i]);
					
				}
			}
			
			if (findDataArray)
				RapiFreeBuffer(findDataArray);

		
			
		}
		
		__except ( EXCEPTION_EXECUTE_HANDLER ) {
			PrintMsg("\n\n [interanl Error  CRapi::enumFiles1] \n");
		}
		
}

//
//
// Enum files on PC and call doActionFile(files_name)
//
//

void CRapi::enumFiles1(LPCTSTR path, int action)
{
	WIN32_FIND_DATA fnd;
	TCHAR file[MAX_PATH];

	__try{	
	
		HANDLE hFnd = FindFirstFile(path, &fnd);

		if ( hFnd == INVALID_HANDLE_VALUE) { 
			PrintMsg("Error Dir at %s", path);
			return;
		}

		do {
			if ( fnd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY /*&& fnd.cFileName[0]!='.' */) {
				if ( !strcmp(fnd.cFileName, ".") || !strcmp(fnd.cFileName, "..") )
					continue;
				strcpy(file, curr_dir1);
				strcat(file, fnd.cFileName);
				strcat(file, "\\*");
				m_TotalFolders ++;
				doActionEnterFolder(fnd.cFileName, action, &fnd);
				if (m_bRecursive) // recursion allowed ?
					enumFiles1(file, action);
				doActionExitFolder(fnd.cFileName, action);
			} else {
				strcpy(file, curr_dir1);
				strcat(file, fnd.cFileName);
				m_TotalFiles++;
				doActionFile(file, action, &fnd);
			}

		} while ( FindNextFile(hFnd, &fnd) );

		FindClose(hFnd);
	
	}
	
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::enumFiles1] \n");
	}

}

// called by enumFiles1, enumFiles2
// do some %action% with %file% (file from PC or PPC)
// 
void CRapi::doActionFile(LPCTSTR file, int action, void *fndData = NULL)
{
	
	TCHAR dest[MAX_PATH];
	char drive[_MAX_DRIVE];
	char dir[_MAX_DIR];
	char fname[_MAX_FNAME];
	char ext[_MAX_EXT];

	switch (action) {

		// copy files
	case doCopyToPPC:
		{		
			PrintMsg("%s [%s]", file, itoaFileSize(((LPWIN32_FIND_DATA)fndData)->nFileSizeLow) );
			_splitpath(file, drive, dir, fname, ext);
			strcpy(dest, curr_dir);
			strcat(dest, fname);
			strcat(dest, ext);

			CopyFile1(file, dest);
		}
		break;

	case doCopyToPC:
		{
			PrintMsg("%s [%s]", file, itoaFileSize(((LPCE_FIND_DATA)fndData)->nFileSizeLow) );
			_splitpath(file, drive, dir, fname, ext);
			strcpy(dest, curr_dir1);
			strcat(dest, fname);
			strcat(dest, ext);

			CopyFile2(file, dest);
		}
		
		break;
		
	case doDelete:
		{
			PrintMsg(file);

			WCHAR destFile1[MAX_PATH];
			MultiByteToWideChar(CP_ACP, 0, file, -1, destFile1, MAX_PATH );
			DWORD attr;
			attr = CeGetFileAttributes(destFile1);
			attr &=~ FILE_ATTRIBUTE_READONLY;
			CeSetFileAttributes(destFile1, attr);

			if (! CeDeleteFile(destFile1) )
				PrintMsg("Error Del at PPC: %s",file);


		}
		break;

	case doCountFileSizesPC: 				
		m_TotalCopySize += ((LPWIN32_FIND_DATA)fndData)->nFileSizeLow;				
		
		break;
		

	case doCountFileSizesPPC: {
		m_TotalCopySize += ((LPCE_FIND_DATA)fndData)->nFileSizeLow;		
		}
		break;

	case doDirPPC: {
		LPCE_FIND_DATA lpData = (LPCE_FIND_DATA)fndData;
		m_TotalCopySize += lpData->nFileSizeLow;	
		wprintf( L"%-30s %-5s %d\n", 
			lpData->cFileName, 
			L"", 
			lpData->nFileSizeLow );
				}
		break;							  
	}
	
}

// called by enumFiles1, enumFiles2
//  Here we create new folders while copying sub folders to PC or PPC 
//
void CRapi::doActionEnterFolder(LPCTSTR file, int action, void *fndData)
{
	__try {
		_tcscat(curr_dir, file);
		_tcscat(curr_dir1, file);
		
		switch (action) {
			// copy files
		case doCopyToPPC:
			{			
				WCHAR path[MAX_PATH];
				MultiByteToWideChar(CP_ACP, 0, curr_dir, -1, path, MAX_PATH );
				if ( !CeCreateDirectory(path, NULL) )
					PrintMsg("Error Create Folder at PPC: %s",curr_dir);

			}
			break;
		case doCopyToPC:
			{
			if ( !CreateDirectory(curr_dir1, NULL) )
				PrintMsg("Error Create Folder: %s",curr_dir1);

			}			
			break;
			
		case doDirPPC: {
			LPCE_FIND_DATA lpData = (LPCE_FIND_DATA)fndData;
			wprintf( L"%-30s %-5s\n", 
				lpData->cFileName, 
				L"<DIR>"				);
					   }
			break;
		}
		
		_tcscat(curr_dir, "\\"); // 

		// ��� ��
		// *********************************************************
		
		_tcscat(curr_dir1, "\\"); // 

	}
	
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::doActionEnterFolder] \n");
	}
	
}


// called by enumFiles1, enumFiles2
//  Here we delete folders while deleting folders or PPC 
//
void CRapi::doActionExitFolder(LPCTSTR file, int action)
{
	// Enum ����� �� ����� ������ � ����� ����� �� PPC ���� �� ������� �����
	
	__try {
		
		char* s = strrchr(curr_dir, '\\'); 
		if (s) {				
			// here is path == "temp\dir1\"
			if (s)	*(s)=0;				// delete last slash 
			
			// here is path == "temp\dir1"			
			switch (action) {

				// remove folder while "del" command
			case doDelete:
				{
					WCHAR path[MAX_PATH];
					MultiByteToWideChar(CP_ACP, 0, curr_dir, -1, path, MAX_PATH );
					if ( !CeRemoveDirectory(path) )
						PrintMsg("Error Delete folder %s", curr_dir);
				}
				break;
			}
			
			
			s = strrchr(curr_dir, '\\');	// ���������� ����� - ��������� ���������
			if (s)	*(s+1)=0;		
			// here is path == "temp\"
		}
		

		// actions for PC
		// *********************************************************

		s = strrchr(curr_dir1, '\\'); 
		if (s) {				
			// path == "c:\temp\dir1\"
			if (s)	*(s)=0;				// delete last slash
			
			// path == "c:\temp\dir1"
			s = strrchr(curr_dir1, '\\');	// delete last folder in path 
			if (s)	*(s+1)=0;		
			// path == "c:\temp\"
		}
		
	}
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::doActionExitFolder] \n");
	}
	

}




/*
Delete files by mask

  */
int CRapi::del(LPCTSTR par1, LPCTSTR par2, LPCTSTR par3, LPCTSTR par4)
{

	WCHAR path[MAX_PATH];

	__try {
		
		char drive[_MAX_DRIVE];
		char dir[_MAX_DIR];
		char fname[_MAX_FNAME];
		char ext[_MAX_EXT];			
		
		_splitpath(par1, drive, dir, fname, ext);
		strcpy(curr_dir, dir);
		
		MultiByteToWideChar(CP_ACP, 0, par1, -1, path, MAX_PATH );
		enumFiles2(path, doDelete);
		
		return 1;
	}
	
	__except ( EXCEPTION_EXECUTE_HANDLER ) {
		PrintMsg("\n\n [interanl Error  CRapi::del] \n");
	}
	
	return 0;
	
}

// Rinning application in PPC 
//
int CRapi::run(LPCTSTR par1, LPCTSTR par2, LPCTSTR par3, LPCTSTR par4)
{
	
	WCHAR path[MAX_PATH];	
	WCHAR param[MAX_PATH];
	PROCESS_INFORMATION pi;

	MultiByteToWideChar(CP_ACP, 0, par1, -1, path, MAX_PATH );
	MultiByteToWideChar(CP_ACP, 0, par2, -1, param, MAX_PATH );

	if ( 0 == CeCreateProcess(path, param,NULL,NULL,FALSE, 0, NULL, NULL, NULL, &pi ) )
		PrintMsg("\nError run %s %s... %Xh %Xh\n", path, param, CeGetLastError(), CeRapiGetError() );
	else
		PrintMsg("\n Run OK ...\n" );

	return 0;
}



//
//  FUNCTION:   ShowPropDesc(CEPROPID PropId, TCHAR * pDBName, LONG dwCurrectRecord )
//
//  PURPOSE:    Display database property information in edit control
//
//  PARAMETERS:
//
//  RETURN VALUE:
//
//  COMMENTS:
//
void CRapi::db_ShowPropDesc(CEPROPID PropId, DWORD dwOpenHandle, LONG dwCurrentRecord)
{
	TCHAR		szBuf[2000];
	//TCHAR		szBuf2[2000];
	WORD		cPropID = 1;
	LPBYTE 		bData = NULL;
	DWORD		cbData = 0;
	SYSTEMTIME	SystemTime;
	CEOID      CeOID = 0;
	DWORD		dwIndex;

	if ( (HANDLE) dwOpenHandle == INVALID_HANDLE_VALUE)
	{
		wsprintf(szBuf, TEXT("ERROR: CeOpenDatabase failed to open database.(Error: %ld)\r\n"), GetLastError());
		OutputDebugString(szBuf);
		MessageBox(NULL, szBuf, TEXT("ERROR"), MB_OKCANCEL );
	}
	else
	{
		if ( dwCurrentRecord == -1 )
		{
			if ( !CeSeekDatabase((HANDLE) dwOpenHandle, CEDB_SEEK_CURRENT, 0, &dwIndex) )
			{
				dwIndex = 0;
				wsprintf(szBuf, TEXT("ERROR: CeSeekDatabase failed to get record index.(Error: %ld)\r\n"), GetLastError());
				PrintMsg(szBuf);
				
				return;
			}
		}
		else
			dwIndex = dwCurrentRecord;

		// check for eof by reading after a seek failure
		if( !CeReadRecordProps((HANDLE)dwOpenHandle, CEDB_ALLOWREALLOC, &cPropID,&PropId,&bData,&cbData) )
		{
			DWORD dwError;

			if ( (dwError=GetLastError()) != ERROR_NO_MORE_ITEMS )
			{
				wsprintf(szBuf, TEXT("ERROR: CeReadRecordProps failed to read database.(Error: %ld)\r\n"), dwError);
				PrintMsg(szBuf);
				
			}
			else
				PrintMsg("End of file");
			//	SendMessage(hwndEdit,WM_SETTEXT, 0, (LPARAM) (LPTSTR) TEXT("End-OF-File"));

			return;
		}
		
	}

	if( bData == NULL )
	{
		wsprintf(szBuf, TEXT("ERROR: CeReadRecordProps failed to read database.(Error: %ld)\r\n"), GetLastError());
		//MessageBox(NULL, szBuf, TEXT("ERROR"), MB_OKCANCEL );
		PrintMsg(szBuf);
		return;
	}


	switch(TypeFromPropID(PropId))
	{
		case CEVT_I2:
			wsprintf(szBuf, TEXT("Record: %ld\r\nProperty: Short\r\nDATA:\r\n%d"), dwIndex, bData?((PCEPROPVAL) bData)->val.iVal: 0) ;
			break;
		case CEVT_UI2:
			wsprintf(szBuf, TEXT("Record: %ld\r\nProperty: Unsigned Short\r\nDATA:\r\n%d"), dwIndex, bData?((PCEPROPVAL) bData)->val.uiVal: 0) ;
			break;
		case CEVT_I4:
			wsprintf(szBuf, TEXT("Record: %ld\r\nProperty: Long\r\nDATA:\r\n%d"), dwIndex, bData?((PCEPROPVAL) bData)->val.lVal: 0) ;
			break;
		case CEVT_UI4:
			wsprintf(szBuf, TEXT("Record: %ld\r\nProperty: Unsigned Long\r\nDATA:\r\n%d"), dwIndex, bData?((PCEPROPVAL) bData)->val.ulVal: 0) ;
			break;
		case CEVT_FILETIME:
			if ( bData != NULL )
			{
				FileTimeToSystemTime(&(((PCEPROPVAL) bData)->val.filetime), &SystemTime);

				wsprintf(szBuf, TEXT("Record: %ld\r\nProperty: File Time\r\nDATA:\r\nYear:%d\r\nMonth:%d\r\n"
					TEXT("Day:%d\r\nDay of Week:%d\r\nHour:%d\r\nMinute:%d\r\nSecond:%d\r\nMilliseconds:%d")),
					dwIndex, SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay, SystemTime.wDayOfWeek,
					SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds) ;
			}
			else
				wsprintf(szBuf,TEXT("Record: %ld\r\nProperty: File Time"), dwIndex);

			break;
		case CEVT_LPWSTR: {
			// here is strange bug
			wsprintf(szBuf, TEXT("Record: %ld\r\nProperty: String\r\nDATA:\r\n%-70s"), dwIndex,
				bData?((PCEPROPVAL) bData)->val.lpwstr: NULL) ;

//			if ( bData && lstrlen(((PCEPROPVAL) bData)->val.lpwstr) > 70 )
//				lstrcat(szBuf, TEXT("..."));
						  }

			break;
		case CEVT_BLOB:
		{
			DWORD dwMaxShow = 15;
			TCHAR szTempBuf[6];
			DWORD i;
			wsprintf(szBuf,TEXT("Record: %ld\r\nProperty: BLOB\r\nDATA:\r\n"), dwIndex);
			if( bData )
			{
				dwMaxShow = min(dwMaxShow, ((PCEPROPVAL) bData)->val.blob.dwCount);
				for (i = 0; i < dwMaxShow; i++)
				{
				  wsprintf (szTempBuf, TEXT("%#2x,"), (BYTE)((PCEPROPVAL) bData)->val.blob.lpb[i]);
				  lstrcat (szBuf, szTempBuf);
				}
				if (dwMaxShow < ((PCEPROPVAL) bData)->val.blob.dwCount )
					lstrcat (szBuf, TEXT("..."));
			}
			break;
		}
		default:
			wsprintf(szBuf, TEXT("Record: %ld\r\nUnknown record property"),dwIndex);
			break;
	}
	if ( bData )
		LocalFree(bData);

	PrintMsg(szBuf);

	//SendMessage(hwndEdit,WM_SETTEXT, 0, (LPARAM)(LPCTSTR)szBuf);
}

// enumerates DataBases in PPC
//
//
int CRapi::db(LPCTSTR par1, LPCTSTR par2, LPCTSTR par3, LPCTSTR par4)
{
	// enum DBs
	if ( par1 == NULL || _tcslen(par1)==0) {
		WORD foundCount;
		LPCEDB_FIND_DATA findDataArray;
		int cnt_files, cnt_dirs; 
		double size_mb, x2;
		
		if ( !CeFindAllDatabases(0, FAD_NAME | FAD_TYPE |FAD_NUM_RECORDS , &foundCount, &findDataArray ) ) {
			PrintMsg("\n Error Enum DataBases\n" );
			return 0;
		}
		
		if (foundCount==0) {
			PrintMsg("\n No DataBases was found\n" );
			return 0;
		}
		
		cnt_files= cnt_dirs= size_mb = 0;
		
		for (int i=0; i<foundCount; i++){
			
			wprintf( L"%-30s %-8X %-4d %d bytes\n", 
				findDataArray[i].DbInfo.szDbaseName, 
				findDataArray[i].DbInfo.dwDbaseType , 
				findDataArray[i].DbInfo.wNumRecords, 
				findDataArray[i].DbInfo.dwSize   );
			cnt_files++;
			x2 = findDataArray[i].DbInfo.dwSize  ;
			size_mb += x2 / 1048576;
		}
		
		if (findDataArray)
			RapiFreeBuffer(findDataArray);
		
		printf("\n %d DBs [%.3f Mb]\n", cnt_files, size_mb);
		return 0;
	}

	if ( !_tcscmp(par1, "exp") && _tcslen(par2) ) {
		WCHAR dbName[MAX_PATH];
		CEOID Oid =0;

		MultiByteToWideChar(CP_ACP, 0, par2, -1, dbName, MAX_PATH );
		HANDLE hDatabase = CeOpenDatabase(&Oid, dbName, NULL, CEDB_AUTOINCREMENT , NULL);
		
		
		
		TCHAR		szBuf[100];
		CEOID		CeRecObj;
		CEOID		CeOID = 0;
		DWORD		cbRecProps = 0;
		LPBYTE		lpRecProps = NULL;
		WORD		wNumRecProps = 0;
		WORD		i;

		
		PCEPROPVAL pCePropVal;
		
		if ( (CeRecObj = CeReadRecordProps((HANDLE) hDatabase, CEDB_ALLOWREALLOC,
			&wNumRecProps, NULL, &lpRecProps, &cbRecProps)) )
		{
			if ( lpRecProps )
			{
				if ( wNumRecProps )
				{
					pCePropVal = (PCEPROPVAL)lpRecProps;
					
					// add each property to treeview
					for ( i= 0 ; i < wNumRecProps; i++)
					{
						wsprintf(szBuf, TEXT("%#lx"), pCePropVal[i].propid);//HIWORD(pCePropVal[i].propid));
						db_ShowPropDesc(pCePropVal[i].propid, (DWORD)hDatabase, 0);
						
					}
					
				}
				LocalFree(lpRecProps);
			}
			
			
		}

		 CeCloseHandle(hDatabase);

		 return 0;
	}

	return 0;
	
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Moldova (Republic of) Moldova (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions