Many of the RAPI functions are directly equivalent to their Windows CE counterparts and therefore do not warrant a detailed description. There are, however, some general points to remember when calling these functions:
The following sections list the RAPI functions by group, together with a brief description of their purpose. File and Folder ManipulationRAPI provides a complete set of functions for manipulating files and folders on a connected Windows CE device. You can refer to Chapters 2 and 3 for information on the equivalent Windows CE functions. Note that there is no equivalent Windows CE function for CeFindAllFiles this function is used to find matching files through a single function call rather than the multiple calls that CeFindFirstFile and CeFindNextFile require. This improves performance especially when using slow communications connections. Listing 10.2 shows a call to CeFindAllFiles to return all files in the root folder. The source code is located in \RAPI\FindAllFiles on the CDROM.
Listing 10.2 CeFindAllFiles call to list files in root folderint main(int argc, char* argv[]) { HRESULT hr; DWORD dwFileCount; LPCE_FIND_DATA pFindDataArray; char szFilename[MAX_PATH]; // List files in root directory of connected CE Device hr = CeRapiInit(); if(FAILED(hr)) { cout "Could not initialize RAPI:" GetLastError() endl; return 1; } if(!CeFindAllFiles(L"\\*.*", FAF_NAME | FAF_SIZE_LOW | FAF_ATTRIBUTES, &dwFileCount, &pFindDataArray)) ShowRAPIError(); else { cout "Files in root of Windows Device:" endl; for(DWORD i = 0; i < dwFileCount; i++) { wcstombs(szFilename, pFindDataArray[i].cFileName, MAX_PATH); cout szFilename ""; if(pFindDataArray[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) cout "<DIR>" endl; else cout pFindDataArray[i].nFileSizeLow endl; } CeRapiFreeBuffer(pFindDataArray); } hr = CeRapiUninit(); if(FAILED(hr)) cout "Could not un-initialize RAPI" endl; return 0; } The code in Listing 10.2 calls CeRapiInit, so the application will wait until a device is connected. A call is then made to CeFindAllFiles with flags requesting that the file name, size, and attributes be returned (Table 10.3). Notice that only the low DWORD is returned for the file size (FAF_SIZE_LOW), and this assumes that all files on the Windows CE device are less than 4 GB. The following flags can be passed to dwFlags and combined where applicable:
The CeFindAllFiles function returns a pointer to a buffer that contains a CE_FIND_DATA array for each file that matches the file specification. This buffer is owned by Windows and must be freed by calling the function CeRapiFreeBuffer. The CE_FIND_DATA structure contains members for each data item that can be returned for a file. typedef struct _CE_FIND_DATA { DWORD dwFileAttributes; FILETIME ftCreationTime; FILETIME ftLastAccessTime; FILETIME ftLastWriteTime; DWORD nFileSizeHigh; DWORD nFileSizeLow; DWORD dwOID; WCHAR cFileName[MAX_PATH]; } CE_FIND_DATA, *LPCE_FIND_DATA; Property Database RAPI FunctionsThe functions shown in Table 10.4 can be used to manipulate property databases on a connected Windows CE device. You can refer to Chapter 4 for details on the Windows CE function equivalents. The function CeFindAllDatabases does not have a Windows CE equivalent it is used to return information on all databases that match a specification. As with CeFindAllFiles, this function is used to improve performance. Note that CeFindAllDatabases does not return information on databases in volumes. The function CeFindFirstDatabaseEx should be used to search database volumes. You should try to limit the number of calls to CeWriteRecordProps and CeReadRecordProps to improve performance by reading or writing as many properties at a time as possible.
Registry RAPI FunctionsThe functions in Table 10.5 can be used to manipulate keys in the Windows CE device's registry. The Windows CE equivalent functions are described in Chapter 4.
The code in Listing 10.3 shows how to use the RAPI registry functions to obtain the device name for the connected Windows CE device. This is the name that is configured through the Control Panel's Communication applet. This information is located in HKEY_LOCAL_MACHINE\Ident\Name. You can also find the Description ("Desc" value) and original device name ("OrigName" value) from this registry key. Information about the type of device and manufacturer can be found in the HKEY_LOCAL_MACHINE\Platform key. Registry functions are described in more detail in Chapter 4. Listing 10.3 Determining device name of connected device#include "stdafx.h" #include <rapi.h> #include <iostream.h> BOOL CeGetPlatformName(LPTSTR pszPlatformName) { BOOL fSuccess = FALSE; LONG nRetVal = 0; HKEY hkey = NULL; DWORD dwType = 0; DWORD cbSize = 0; WCHAR szPlatformNameW[MAX_PATH]; nRetVal = CeRegOpenKeyEx( HKEY_LOCAL_MACHINE, L"Ident", 0, KEY_ALL_ACCESS, &hkey); if (ERROR_SUCCESS == nRetVal && hkey) { // get the registry value cbSize = sizeof(szPlatformNameW); nRetVal = CeRegQueryValueEx( hkey, "NAME", 0, &dwType, LPBYTE)szPlatformNameW, &cbSize); if (ERROR_SUCCESS == nRetVal) { szPlatformNameW[cbSize / sizeof(WCHAR)] = '\0'; if (wcstombs( pszPlatformName, szPlatformNameW, MAX_PATH)) fSuccess = TRUE; } } if (hkey) CeRegCloseKey( hkey ); return fSuccess; } int main(int argc, char* argv[]) { HRESULT hr; char szDeviceName[MAX_PATH]; // List files in root directory of connected CE Device hr = CeRapiInit(); if(FAILED(hr)) { cout "Could not initialize RAPI:" GetLastError() endl; return 1; } if(CeGetPlatformName(szDeviceName)) cout "Device Name: " szDeviceName endl; else cout "Could not retrieve device name" endl; hr = CeRapiUninit(); if(FAILED(hr)) cout "Could not un-initialize RAPI" endl; return 0; } The code in the CeGetPlatformName function is straightforward registry code. However, it is important to remember that all key and value names must be specified as Unicode strings. Further, the returned strings (for example, the device name) will be Unicode, and may need to be converted to ANSI character strings. System Information RAPI FunctionsTable 10.6 lists RAPI functions used to obtain information about the Windows CE devices.
Miscellaneous RAPI FunctionsFinally, Table 10.7 lists RAPI functions that allow access to errors, create processes, obtain information about windows, and interact with the shell.
|