`
`____________rtiL
`
`bandexe
`
`bufton exe
`
`crndbar.exe
`
`custdraw .exe
`
`datetirne.exe
`
`dbase.exe
`
`dbview.exe
`
`files .exe
`
`generlc.exe
`
`5tart Windows CE File
`
`13 1854 A1
`
`Figure 61
`
`The File System Explorer application
`
`The FILESYS.EXE application allows users to create delete and
`rename files and directories It also provides very primitive file edit
`ing File properties such as file attributes and file size can also be
`determined using the application
`
`Most of these features are accessed through the Options menu shown
`in Figure 6.2
`
`t7Is
`
`Edit File
`
`DeIet
`
`Properties.
`
`hles.exe
`
`generic cxc
`
`5tart Windows CE File S.
`
`1240 PM
`
`Figure 6.2
`
`The File System Explorer Options menu
`
`Page 00176
`
`
`
`156
`
`Creating and Deleting Files and Directories
`
`new directory with the FILESYS.EXE application first se
`To create
`lect the parent directory under which the new directory is to be located
`The term parent directory is used to define the directory under which
`some specific directory or file is located To select
`file or directory
`tap the name or icon of that file or directory in the tree view display
`After selecting the directory that is to contain the new directory select
`the New Directory option from the Options menu see Figure 6.2 For
`new directory under the root directory select the
`example to create
`root directory icon and select the New Directory menu option This
`operation results in the creation of new directory called Empty
`Folder as shown in Figure 6.3 Note the Empty Folder icon
`New files are created in much the same way To create
`file under
`particular directory select the directory and then choose the New File
`option under the Options menu
`file called Empty File will appear
`Deleting files and directories is straightforward Simply select the file
`or directory you wish to delete and then select the Delete option from
`the Options menu
`
`Renaming Files and Directories
`
`Another common file system operation is renaming files or directories
`The names Empty Folder and Empty File are not very useful for real
`
`LUJL.....iJLU WULiLLULHLIL..UJLrn
`
`EU
`
`bands.exe
`
`button .exe
`
`cmdbar.exe
`
`EJ custdra.exe
`datetirneexe
`
`dbase.exe
`
`dbview.exe
`
`Empty Folder
`II files.exe
`
`tart Windows CEQe 5.
`
`Figure 6.3
`
`Creating
`
`new directory
`
`950 AM
`
`Page 00177
`
`
`
`157
`
`world directories and files To rename file or directory in the File Sys
`tem Explorer application double-tap on the file or directory to be
`renamed
`small text entry field appears containing the name of the
`selected file or directory You can then type the new file or directory
`name in this field Press the Enter key to make the name change take
`the application is running on Windows CE device that does
`effect If
`different part of the screen and your
`keyboard simply tap
`take effect
`
`not have
`
`change will
`
`As an example lets say that you want to change the name of the
`Empty Folder directory in Figure 6.3 to Acme Corp Simply double-tap
`on the name Empty Folder and type Acme Corp in the edit field The
`File System Explorer display should then look as shown in Figure 6.4
`
`Files are renamed the same way Double-tap the file you wish to
`rename and type the new name in the edit field that appears
`file called Expenses under the Acme Corp
`For example lets create
`directory Tap the Acme Corp directory to select it and then choose
`New File from the Options menu
`new file called Empty File
`appears under the Acme Corp directory as shown in Figure 6.5
`
`Rename this file by tapping it twice and then typing the name
`Expenses into the edit field that appears The contents of the Acme
`Corp directory will then appear as shown in Figure 6.6
`
`bands.exe
`
`button.exe
`
`cmdbar exe
`
`custdraw.exe
`
`dateirne.exe
`
`dbase.exe
`
`dbvie.exe
`
`IAcrne Corp
`files.exe
`
`5tar Windows CE Fite
`
`939 AM
`
`Figure 6.4
`
`Renaming
`
`directory
`
`Page 00178
`
`
`
`158 DIJS1IISI1JaIIIIII11INt$UIuIIaIuI1IaNItIS1I
`
`_______
`
`bandsexe
`
`button exe
`
`cmdbar .exe
`
`custdraw cxc
`
`datetime .exe
`
`dbase cxc
`
`dbvie.exe
`Acme Corp
`1I Empty File
`T1 files.exe
`
`15tart Windows CE File 5.
`
`943 AM
`
`Figure 6.5
`
`Creating
`
`new file
`
`Editing Files
`
`Another important set of file system functions we will cover in this
`lets your applications write data to and read data from files
`chapter
`To demonstrate these features the File System Explorer application
`provides very rudimentary file editing capabilities You will not be
`tempted to delete your current word processing software from your
`Handheld PC when you see this feature However after finishing this
`
`i_jilt
`
`ii
`
`__________
`OptIons
`
`banclsexe
`
`button.exe
`
`cmdbar.exe
`
`custdraw.exe
`
`datetime .exe
`
`dbase.exe
`
`dbview.exe
`Acme Corp
`
`Experies
`f1 files.exe
`
`5tartj Windows CE File 5.
`
`Figure 6.6
`
`Renaming
`
`file
`
`955 AM
`
`Page 00179
`
`
`
`chapter you will know how to use the file system API to read and
`write files
`
`file in the FILESYS.EXE application select the file you wish to
`To edit
`edit by tapping it once Then press the Enter key on your keyboard
`For devices that do not include
`keyboard you can use the Edit File
`menu option in the Options menu to invoke this feature The dialog
`box shown in Figure 6.7 appears
`
`Text entered in the edit field of this dialog is written to the file if the
`user presses the Save button Pressing Cancel aborts file editing
`
`Examining File Properties
`
`The final notable feature of the File System Explorer application is in-
`yoked by the Properties option of the Option menu Choosing this
`menu option retrieves and displays various features of the currently
`selected file The dialog box shown in Figure 6.8 appears showing
`which file attributes are set for the selected file as well as the size of
`the file in bytes
`
`File Handles
`
`The Windows CE file system API functions access files and directories
`file handle Like any other Windows CE handle type
`by means of
`
`kb1
`
`Mfe spent
`
`too much money this quarter
`
`Save
`
`tancel
`
`Start WndosCEFile5J
`
`959 AM
`
`Figure 6.1
`
`Editing
`
`file with the file system explorer
`
`Page 00180
`
`
`
`160
`
`Attributes
`
`flReadOnly
`
`fl Hidden
`
`Archive
`
`fl System
`
`Size 76
`
`5trt
`
`B23 PM
`
`Figure 6.8
`
`Displaying the properties of
`
`file
`
`such as window handle
`file handle is an identifier for referencing
`an object managed by the Windows CE kernel In this case the object is
`file or directory
`
`As we will see the function used for opening and creating files returns
`handle to the specified file Functions for reading and writing files
`file handle in order to access the right file In short any oper
`require
`ation that Windows CE application may perform on
`file or direc
`valid file handle
`tory requires
`
`file Attributes
`
`Every file in the Windows CE file system has one or more attributes
`These attributes are used to distinguish files in terms of characteristics
`such as how they can be used and by whom Under Windows CE files
`may have one or more of the attributes listed in Table 6.1
`
`FILE ATTRIBUTES
`
`Under Windows CE the FILE_ATTRIBUTE_OFFLINE
`file attributes are not supported
`
`and FILE_ATTRIBUTE_TEMPORARY
`
`file when the file is cre
`An application typically sets the attributes of
`ated However
`it may be necessary to change or determine the attrib
`
`Page 00181
`
`
`
`Table 6.1 Windows CE File Attributes
`
`ii1IIIII
`
`FILE_ATTRIBUTE_ARCHIVE
`
`FILE ATTRIBUTE COMPRESSED
`
`FILE_ATTRIBUTE_HIDDEN
`
`FILE_ATTRIBUTE_NORMAL
`
`FILE_ATTRIBUTE_READONLY
`
`FILE_ATTRIBUTE_DIRECTORY
`
`FILE_ATTRIBUTE_SYSTEM
`
`FILE_ATTRIBUTE_INROM
`
`FILE_ATTRIBUTE_ROMMODULE
`
`Used by applications to mark
`been backed up
`
`file that has not
`
`File or directory is compressed For files this
`means that all of the data in the file is com
`pressed For directories this means that by
`created in this
`default all files or subdirectories
`directory are created with the compressed
`attribute
`
`The file is marked as hidden
`
`This attribute cannot be used with any other
`attribute Hence if set it means that no other
`attribute is set
`
`Applications can only read this file They cannot
`write to or delete it
`
`Indicates that the particular
`directory
`file is
`Note This attribute cannot be set It can be
`returned by GetFileAttributes
`
`Indicates the file is
`system file i.e it
`intended to be used only by the operating
`system
`
`is
`
`Indicates the file is
`read-only operating
`system file stored in ROM
`Indicates the file is an in-ROM DLL or EXE
`
`file after it has been created Applications might even need to
`utes of
`determine the attributes of files they did not create
`The Windows CE file system API provides two functions to read and
`file GetFileAttributes and SetFileAttributes
`modify the attributes of
`The first of these functions has the following form
`
`GetFileAttributes lpFileName
`
`This function takes the Unicode string name of the file of interest in
`it returns DWORD contain
`the parameter lpFileName If successful
`ing the file attributes that are set for the file The return value is the bit
`wise OR of one or more of the file attribute values specified in Table
`6.1 Tn addition Windows CE provides for two additional return val
`ues for this function FILE_ATTRTBUTE_INROM
`and FILE_AT
`TifiBUTE_ROMMODULE
`The first of these indicates that the file in
`
`Page 00182
`
`
`
`162
`
`read-only operating system file stored in ROM The sec
`question is
`ond indicates that the file is DLL or executable .EXE file stored in
`ROM and intended to execute in place This means that files with the
`FILE_ATTRIBUTE_ROMMODULE attribute do not need to be copied
`into RAM in order to rim Files of this type are typically libraries and
`applications that ship with the Windows CE operating system
`
`The attributes of
`
`file can be set using the SetFileAttributes function
`
`SetFileAttributes lpFileNaxne dwFileAttributes
`
`This function returns TRUE if the attributes are successfully set and
`FALSE if
`the function is unsuccessful
`
`As an example lets assume that we want to mark as hidden all files
`that are read-only The piece of code responsible for testing if
`read-only and then setting the hidden file attribute would look some
`thing like this
`
`file is
`
`name is in lpFileName
`//File
`DEORD dwAttributes
`dwAttributes
`CetFileAttributeslpFileName
`FILE_ATTRIBUTE_READONLY
`
`if dwAttributes
`
`dwAttributes
`FILE_ATTRIBUTE_HIDDEN
`SetFileAttributes lpFileNarne dwAttributes
`
`Note that as in Windows NT and Windows 98 SetFileAttributes cannot
`be used to set the FILE_ATTmBUTE_COMPRESSED
`attribute of
`If this attribute is not set when the file is created you must use the
`DeviceloControl
`ftmction to set it
`
`file
`
`Searching for Files
`
`It may seem little strange to discuss searching for files in the Win
`dows CE file system this early in the chapter Certainly operations
`such as creating and deleting files must be more fundamental
`than file
`searching
`
`While this may be true in some sense it
`is also the case that many file
`operations are iterative For example deleting
`directory requires
`files and subdirectories contained by the direc
`recursively deleting all
`
`Page 00183
`
`
`
`fl
`
`tory to be deleted Such an iterative process entails file searching oper
`ations to look for files to delete
`
`As another example consider how an application might determine if
`particular directory is empty There is no Windows CE API function
`IsDirectoryEinpty Writing such an operation from scratch involves
`searching the directory in question to see if
`it contains any files
`
`file searching is so fundamental
`to many file system operations
`In fact
`that understanding how these features are implemented requires that
`we first understand file searching under Windows CE
`Windows CE provides three functions for such operations FindFirst
`File FindNextFile and FindClose
`
`two of these functions return
`The first
`information about
`
`data structure containing
`the files that they retrieve Before discussing the
`look at this structure
`find functions in detail lets first
`
`FINDFIRSTFILEEX
`
`The function FindFirstFileEx is not supported under Windows CE
`
`The W1N32_FIND_DATA Structure
`
`structure is used by Windows CE to provide
`The W1N32_FIND_DATA
`information about
`file located by one of the find functions
`
`typedef struct _W1N32_FIND_DATA
`
`DWORD dwFileAttributes
`FILETIME ftcreationTime
`FILETIME ftLastAccessTime
`FILETIME ftLastwriteTime
`DWORD nFileSizeHigh
`DWORD nFileSjzeLow
`DWORD
`dwOID
`TCHAR cFileName MAX_PATH
`W1N3 2_FIND_DATA
`
`The members of this structure provide all descriptive information
`about the file either directly or by providing means for extracting
`more information such as through the dwOID member
`
`Page 00184
`
`
`
`dwFileAttributes
`
`contains the attributes of the file as described previ
`and fiLast WriteTiine
`ously in Table 6.1 ftCreationTimeftLastAccessTime
`represent the times the file was created last accessed and last written
`to respectively nFileSizeHigh and nFileSizeLow are the high-order and
`low-order words of the total size of the file The dwOID member con
`tains the object identifier of the file This means that whenever an
`application can get W1N32_FIND_DATA
`about
`file it can also get
`any of the CEFILEINFO data about the file with
`simple call to
`CeOidGetlnfo Finally cFileName is null-terminated string containing
`the name of the file
`
`We will primarilybe interested in how to use the FindFirstFile and
`FindNextFile functions to get W1N32_FIND_DATA
`information It
`should be noted that Windows CE provides some additional
`functions
`for quickly accessing some of the data provided by this structure In
`particular GetFileTime retrieves the same information as provided by
`the FILETIME members of the W1N32_FIND_DATA
`structure
`GetFileSize returns the size of
`specified file
`
`SetFileTime function to allow applications to
`In addition there is
`modify the creation time last access time and last write time of
`
`file
`
`structure some
`Because of its similarity to the W1N32_FIND_DATA
`what parenthetically mention the BY_HANDLE_FILE_INFORMA
`TION structure This is another data structure that contains much the
`file as W1N32_FIND_DATA Given
`same information about
`handle
`to an open file an application can get BY_HANDLE_FILE_INFOR
`MATION structure by calling GetFilelnformationByHandle
`
`The FindFirstFile and FindNextFile Functions
`
`The FindFirstFile function is used to locate
`can also be used to find the first
`
`specific file or directory It
`file in specified directory
`
`FindFirstFilelpFileName lpFindFileData
`
`path and file name or directory
`The lpFileName parameter contains
`name lpFindFileData is used as
`return value by the function It is
`pointer to W1N32_FIND_DATA
`structure containing information
`about
`the located file
`
`search handle This handle refer
`If successful FindFirstFile returns
`ences an internal structure that is responsible for keeping track of the
`
`Page 00185
`
`
`
`file search We will see the utility of this search handle
`progress of
`bit later when we discuss the FindNextFile function If FindFirstFile
`fails it returns INVALID_HANDLE_VALUE
`The lpFileName parameter accepts wildcards This is how you can tell
`FindFirstFile to differentiate between finding the first
`file in specified
`directory and finding the directory itself
`
`For example this line of code will try and find
`\MyFiles
`
`directory called
`
`HANDLE hFile
`fd
`WIN3 2_FIND_DATA
`memsetfd
`sizeoffdfl
`FindFirstFileTEXT\\MyFiles fd
`hFile
`
`On the other hand one subtle change to the FindFirstFile call will find
`the first
`file in the \MyFiles directory
`
`hFile
`
`FindFirstFileTEXT\\MyFiles\\
`
`fd
`
`FindNextFile is used to continue
`
`search started by FindFirstFile For
`file in specified directory Find
`example if FindFirstFile finds the first
`NextFile will attempt to find the next file in that directory Each succes
`sive call to FindNextFile uses the search handle that is updated with
`each find operation to keep track of the search progress The syntax of
`FindNextFile is
`
`FindNextFilehFindFile lpFindFileData
`
`The first parameter is the search handle returned by previous call to
`FindFirstFile The second parameter is the W1N32_FIND_DATA
`return
`value just as in FindFirstFile
`
`FindNextFile returns TRUE if successful and FALSE if
`it fails As with
`call to GetLastError can be used to get
`all of the file system functions
`information about why the function call failed if the return
`additional
`value is FALSE
`
`Creating and Opening Files and Directories
`
`As under Windows NT creating files and directories under Windows
`CE is done using the CreateFile and CreateDirectory functions Under
`Windows CE files are also opened using the CreateFile function as we
`will soon see
`
`Page 00186
`
`
`
`i6
`
`Creating and Opening Files
`
`To create
`
`file your application calls the CreateFile function
`
`CreateFile lpFileName dwDesiredAccess dwShareMode
`lpSecurityAttributes dwCreatioriDistribution
`dwFlagsAndAttributes
`hTemplateFile
`
`lpFileName is the null-terminated Unicode string file name of the file to
`be created Long file names are supported
`
`is used to indicate the access or read-write mode of
`dwDesiredAccess
`the file It can be any combination of the following values
`
`Specifies device query access This allows an application to query
`device attributes
`GENERIC_READ Specifies that the file is created/opened with read
`access
`GENERIC_WRITE Specifies that the file is created/opened with write
`access
`
`file called myfile.txt with read-write access
`For example to open
`an application would do the following
`
`CreateFileTEXTmyfile.txt
`GENERIC_READ GENERIC_WRITE..
`
`The third parameter to CreateFile dwShareMode is used to specify if
`and how the file can be shared It can be
`combination of one or more
`of the following values
`
`if
`
`Indicates that the file cannot be shared
`FILESHARED_READ Subsequent open operations on the file will
`only succeed
`read access is requested via the dwDesiredAccess
`parameter
`FILE_SHARED_WRITE
`Subsequent open operations on the file will
`only succeed if write access is requested
`Under Windows CE file security attributes are ignored The ipSecurity
`Attributes parameter should therefore be set to NULL
`The dwCreationDistribution parameter controls how the CreateFile func
`tion behaves when attempting to create existing files as well as what
`to do when the function tries to open
`nonexistent file This parame
`ter can be one of the following
`
`Page 00187
`
`
`
`the specified file
`
`CREATE_NEW The function creates
`new file If
`already exists the CreateFile function fails
`CREATEALWAYS The function creates
`new file If the specified file
`is overwritten by the CreateFile operation
`already exists it
`OPEN_EXISTING The function opens an existing file If
`file does not exist CreateFile fails
`OPEN_ALWAYS The function opens an existing file If
`is created
`file does not already exist it
`
`the specified
`
`the specified
`
`TRUNCATE_EXISTING
`The function opens the file but truncates it
`to zero length The file must be opened with GENERIC_WifiTE
`access CreateFile fails if
`the specified file does not exist
`
`look at the allowed dwCreationDistribution values we can
`By taking
`see how CreateFile can be used to both create new files and open exist
`is cominon to want to open
`ing files For example it
`file or have the
`file of that name if
`it does not exist as fol
`operating system create
`lows
`
`CreateFile TEXT myfile .txt
`GENERIC_READ GENERIC_WRITE
`NULL OPEN_ALWAYS..
`
`determines the file attributes and several operat
`dwFlagsAndAttributes
`ing modes We have already discussed file attributes The flags portion
`can be any combination of the following values
`FILE_FLAG_WRITE_THROUGH
`Instructs Windows CE to write di
`rectly to the object store when writing to the specified file as op
`posed to writing through any intermediate cache
`FILE_FLAG_RANDOM_ACCESS
`The file supports random access
`Most of the flags that are supported under Windows NT are not sup
`ported under Windows CE Also note that the SECURITY_SQOS_
`PRESENT flag or any of the other values that can be used with it
`under Windows NT are not supported under Windows CE
`Finally the hTemplateFile parameter is ignored under Windows CE and
`should be set to NULL
`
`handle to the open file is returned If it fails
`If CreateFile is successful
`the return value is INVALID_HANDLE_VALUE
`
`Open files are closed using the CloseHandle function
`
`Page 00188
`
`
`
`CloseHandlehObject
`
`where hObject
`
`is the handle of the file to close
`
`Creating Directories
`
`directory is accomplished with the CreateDirectory function
`Creating
`The CreateDirectoryEx function available under Windows NT is not
`supported in Windows CE The CreateDirectory function syntax is
`
`CreateDirectorylpPatbName lpSecurityAttributes
`
`lpPathName is null-terminated Unicode string specifying the path of
`the directory to be created The maximum allowed length of this name
`is the operating system-defined value MAX_PATH The second pa
`rameter to this function is ignored as Windows CE does not support
`therefore should be set to
`file security attributes lpSecurityAttributes
`NULL
`
`it returns TRUE If unsuccessful
`If CreateDirectory is successful
`returns FALSE An application can get more detailed information
`about why the function failed by calling GetLastError
`
`it
`
`An Example
`
`look at how the File System Explorer appli
`As an example lets take
`cation creates new files and directories These features are triggered by
`the New Directory arid New File menu options so the first code to
`look at is the command handlers in the main window procedure for
`these two menu options see Figure 6.2
`
`The pertinent sections of the window procedure are shown below
`Note that tviCurSel contains the currently selected tree view item
`TV_ITEM structure The iParam member of this structure always con
`tains the CEOID object
`identifier of the file or directory corresponding
`to the currently selected tree view item
`
`CALLBACK WndProc
`LRESULT
`HWNJ hwnd
`UINT message
`WPARAM wParam
`LPARAN
`iParam
`
`CEOID oid
`CEOIDINFO oidlnfo
`
`Page 00189
`
`
`
`TCHAR pszFi1eNe pszDjrectoryNjj1e
`switch message
`
`case WM_COMMAND
`DINT nID
`nID
`LOWORDwPararn
`switch nID
`
`case IDCNEWDIRECTORY
`new directory
`I/Create
`CEOIDtviCurSel.lParam
`old
`CeOidGetlnfo old oidlnfo
`if OBJTYPE_DIRECTORYoidlnf
`
`.wObjType
`
`NULL
`pszFileName
`pszDirectoryName
`
`TEXTEmpty Folder
`
`else
`
`have children
`TEXTFiles cannot
`MessageBoxNULL
`TEXT New Folder Error MB_OKIMB_ICONEXCLANATION
`return
`
`OnNew pszFileName pCzDirectoryNaxue tviCurSel .hltem
`TRUE
`oidlnf
`break
`case IDCNEWFILE
`new file
`/Create
`CEOIDtviCursel.lparam
`old
`CeOidGetlnfo old oidlnfo
`if OEJTYPE_DIRECTORYoidlnfo
`
`.wObj Type
`
`oidlnfo inf Directory szDirName
`pszDirectoryName
`TEXTEmpty File
`pszFileName
`
`else
`
`have children
`TEXTFiles
`MessageBoxNULL
`cannot
`TEXTNew
`Folder Error MB_OKIMB_ICONEXCLAMATION
`return
`
`OnNewpszFileName pszDirectoryName
`tviCurSel.hltem oidlnfo FALSE
`break
`
`new directory the
`new file and creating
`In both the case of creating
`application first extracts the CEOIDINFO for the currently selected file
`new file or directory will force the
`or directory
`to create
`request
`application to try and create the file or directory with the currently
`selected item as its parent
`
`Page 00190
`
`
`
`If
`
`Obviously this only makes sense if the currently selected object is
`directory Files cannot contain other files Only directories can contain
`other files or directories For this reason both the IDC_NEWDIREC
`TORY and IDC_NEWFILE case statement code blocks check the
`wObj Type member of the object information structure In either of
`these cases if the currently selected file system object is not
`directory
`warning message is displayed and the operation is aborted
`new file or directory under an existing
`the user is trying to create
`the appropriate default name is assigned to
`directory however
`and the application defined OnNew
`pszFileName or pszDirectoryName
`new directory the
`function is called In the case of
`to create
`request
`value Empty Folder is assigned to pszDirectory In the case of new
`the name Empty File is assigned to pszFileName
`file creation request
`The OnNew function contains
`lot of code for adding new items to the
`tree view control in response to new file and directory creations This
`code is left out so that we can concentrate on the parts of the function
`that relate directly to the file system API Also only the part of this
`function which creates new files is shown Since the section that cre
`ates new directories is very similar it was left out for the sake of
`brevity
`
`BOOL OnNewTCHAR
`pszFileName
`TCHAR pszDirectoryName
`HTREEITEM hParent
`CEOIDINFO oidlrif
`BOOL blsDirectory
`
`TCHAR pszFullName
`HANDLE hFile
`wsprintfpszFullName TEXT%s\\%s
`pszDirectoryNarne
`pszFileNaxne
`FindFirstFilepszFullNeine fd
`hFile
`INVALID_HANDLE_VALUEIhFile
`
`if
`
`/File is new
`FindClose hFile
`CreateFilepszFullNaxne
`hFile
`NULL
`GENERIC_READIGENERIC_WRITE
`CREATE_NEW
`FiLE_ATTRiBUTE_ARCHIVE
`
`NULL
`
`else
`
`/File already exists
`MessageBox NULL
`TEXTFile \Empty File\ Already Exists
`
`Page 00191
`
`
`
`New File Error
`TEXTCreate
`MB_ICONEXCLAMATION NB OK
`return FALSE
`
`return TRUE
`
`The arguments pszFileName and pszDirectoryName are the name of the
`file to be created and the parent directory name respectively hParent is
`the tree view item corresponding to the parent directory in the user
`interface oidlnfo is the CEOIDINFO structure containing information
`new file or
`about the parent directory bisDirectory indicates whether
`new directory is to be created by the function
`
`directory or file CreateFile must be passed the complete
`To create
`path name of the directory or file to be created OnNew therefore first
`constructs the full path name in the variable pszFullName To do this
`OnNew only needs to concatenate the directory name contained in
`pszDirectoryNaine with the file name in pszFileName This is the pur
`charac
`pose of the wsprintf call at the beginning of the function
`ter is inserted between the parent directory name and the file name
`
`After the complete new file path name has been constructed OnNew
`checks to see if the specified file already exists It does so by calling
`FindFirstFile
`
`hFile
`
`FindFirstFilepszFullName fd
`
`pszFullName contains the full path name of the file to be created If this
`file does not exist FindFirstFile will return INVALID_HANDLE_
`VALUE Otherwise it returns the handle of the existing file
`
`If
`
`the file does not exist i.e if hFile equals INVALID_HANDLE_
`VALUE OnNew closes the search handle hFile and creates the new
`the file already exists message to this effect is displayed for
`the user and the function OnNew returns without creating
`new file
`
`file If
`
`Ieading and Writing File Data
`
`File read and write operations are closely linked to the concept of the
`file pointer marks the current position in given file
`file pointer
`Read operations read data from the files current position Write opera
`tions write data to the file at the position indicated by the file pointer
`
`Page 00192
`
`
`
`Files also have an end offile marker This marker indicates the last byte
`of data in the file As such the end of file marker also determines the
`size of the file As file write operations increase the size of
`file they
`move this end of file marker Hence there is no such thing as writing
`the end of
`file files grow to accommodate the data being writ
`past
`ten to them
`
`Files access can be either sequential or random Sequential access means
`that data is read from the file in order Random access means that data
`can be read from the file in any order as determined by the application
`reading the file For random access to be possible there must be way
`for applications to manually set the file pointer without requiring read
`or write operations to occur We will
`introduce such functions later in
`this chapter
`
`ASYNCHRONOUS FILE ACCESS
`
`Under Windows NT file access operations can be synchronous or asynchronous
`Windows CE however does not support asynchronous access
`
`The ReadFiIe Function
`
`Data is read from file using the ReadFile function
`
`ReaciFile hFile 1puffer
`nNwnberOfBytesToRead
`lpNumberOfBytesRead
`ipOverlapped
`
`hFile is the handle of the open file from which the data is to be read
`pointer to the data buffer which receives the data nNum
`lpBuffer is
`specifies the number of bytes of data to read from the
`berOfBytesToRead
`pointer to DWORD used by ReadFile to
`file lpNumberOfBytesRead is
`return the actual number of bytes of data read As Windows CE does
`not allow files to be created with the FiLE_FLAG_OVERLAPPED flag
`ipOverlapped is not used ipOverlapped should be set to NULL
`ReadFile returns TRUE if the operation is successful and FALSE if the
`operation fails An application can get additional error information in
`this case by calling GetLastError
`
`ReadFile returns once the number of bytes specified in nNumberOfBytes
`ToRead have been read or when an error occurs If an application spec
`ifies that more bytes be read than the file actually contains ReadFile
`
`Page 00193
`
`
`
`will simply read as many as it can and return the actual number of
`bytes read in lpNumberOfBytesRead
`
`Random Access Files
`
`The ReadFile function advances the file pointer lpNumberOfBytesRead
`This accounts for the default sequential nature of file access To imple
`ment random file access your applications must be able to control
`the
`position of the file pointer manually This can be done using the Set
`ftmction SetFilePointer can be used to move
`file pointer by
`FilePointer
`64-bit number representing the number of bytes the
`specifying
`pointer is to be moved
`The syntax of SetFilePointer is
`SetFilepointer hFile 1DistanceToIove
`dwMoveMethod
`lpDistanceToMoveHigh
`hFile is the handle of the open file whose pointer is to be moved lDis
`tanceToMove specifies the low order word of the number of bytes to
`move the file pointer This value can be negative in which case the file
`pointer is moved backward lpDistanceToMoveHigh is
`to the
`pointer
`high order word of the number of bytes to move the file pointer This
`parameter is also used by SetFilePointer to return the high order word
`of the new file pointer position
`
`The dwMoveMethod parameter indicates the starting point of the move
`operation It can be one of the following three values
`
`FILE_BEGIN The starting point is the beginning of the file In this case
`the distance to move is interpreted as the unsigned pointer location
`FILE_CURRENT The starting point
`is the current file pointer position
`FILE_END The starting point is the end of file position
`
`If
`
`the function succeeds it returns the low order word of the new file
`lpDistanceToMoveHigh was not NULL this parame
`pointer position If
`ter will return the high order word of the new position If SetFilePointer
`and lpDistanceToMoveHigh is NULL
`fails the return value is
`Random access of Windows CE files can therefore be accomplished by
`first specifying FILE_FLAG_RANDOM_ACCESS as one of the
`values when creating or opening the file with
`dwFlagsAndAttributes
`CreateFile SetFilePointer is then called to manually position the file
`pointer for read and write operations
`
`Page 00194
`
`
`
`An application may also want to change the position of the end of file
`marker of particular file This is done using the SetEndOfFile ftmc
`lion
`
`SetEndOfFilehFile
`
`This ftmctions moves the end of file marker of the file specified by the
`file handle hFile to the current file pointer position of that file If suc
`this function returns TRUE Otherwise it returns FALSE
`cessful
`
`For example to move the end of file marker to the beginning of
`an application could do this
`
`file
`
`I/Set
`
`to beginning of
`file pointer
`file
`FILE_BEGIN
`SetFilePointerhNyFile
`file marker
`set end of
`I/Now
`SetEndOfFile bNyFile
`
`The WriteFile Function
`
`file in Windows CE is done with the WriteFile func
`Writing data to
`tion The syntax and use of this function is very similar to ReadFile
`WriteFile hFile lpBuffer niSuxnierOfBytesToWrite
`lpNuinberOfBytesWritten
`lpOverlapped
`
`The WriteFile parameters have the same meanings as the correspond
`ing ReadFile parameters except for nNumberOfBytesTo Write and
`lpNumberOfBytesWritten It isnt much of
`stretch to realize that
`nNumberOfBytes To Write specifies the number of bytes of data to write
`to the file indicated by hFile Similarly WriteFile returns the actual
`number of bytes written through the lpNuin berOfBytes Written parame
`ter As with ReadFile the ipOverlapped parameter is ignored and should
`be set to NULL
`
`file the file must have been opened with
`To write data to
`GENERIC WRITE access
`
`Data can be written to any position in random access file much as
`data can be randomly read from random access file The file must be
`created or opened with the FILE_FLAG_RANDOM_ACCESS flag set
`Then SetFilePointer can be used to specify the file location to which
`data is written
`The File System Explorer example application of this chapter demon
`strates ReadFile and WriteFile and SetFilePointer operations by means
`of its very rudimentary file editing feature
`
`Page 00195
`
`
`
`An Example
`
`into how to use the ReadFile and WriteFile func
`To gain further insight
`tions lets look at how the File System Explorer application imple
`ments its rudimentary file editing capabilities See Figure 6.7 for
`look
`at the basic file editor
`
`The two user operations which invoke the editor are selecting the Edit
`File menu option and pressing the enter key after selecting
`file Both
`of these operations cause th