Input - Output
Files API Files are too managed with the help of handles. When handle is set it is possible to use it in input/output operations That goes for all I/O operations
CreateFile Returns file handle? What about: streams disks communications pipes etc?
lpFileName – file name dwDesiredAccess – generic access rights dwShareMode – sharing mode lpSecurityAttributes – inheritant rights dwCreationDisposition – creation mode dwFlagsAndAttributes – flags and attributes hTemplateFile – template handle
dwDesiredAccess Shows what can be done with file: GENERIC_READ GENERIC_WRITE GENERIC_EXECUTE GENERIC_ALL
dwShareMode Sharing mode of the file or device: 0 – file cannot be shared until handle is closed FILE_SHARE_READ FILE_SHARE_WRITE FILE_SHARE_DELETE
lpSecurityAttributes – saugumo požymis Protects against unwanted access NULL – handle cannot be inherited by child processes Pointer to SECURITY_ATTRIBUTES pointer to SECURITY_DESCRIPTOR recommended not to modify structure directly
dwCreationDisposition Action to take: CREATE_ALWAYS CREATE_NEW OPEN_ALWAYS OPEN_EXISTING TRUNCATE_EXISTING requires GENERIC_WRITE
dwFlagsAndAttributes File or device flags and attributes. Some flags and attributes apply to files only 0 – device is not a file Other most common flags and attributes: FILE_ATTRIBUTE_NORMAL FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE_ARCHIVE
hTemplateFile GENERIC_READ Supplies file attributes for file being created NULL – if not used Ignored when opening existing file
CreateFile2
ReadFile
ReadFileEx
Overlapped I/O Asynchronous I/O Complexity OVERLAPPED structure FileIOCompletionRoutine callback function
WriteFile
WriteFileEx
File attributes (GetFileAttributes) Commonly used attributes: FILE_ATTRIBUTE_ARCHIVE FILE_ATTRIBUTE_HIDDEN FILE_ATTRIBUTE_NORMAL FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE_SYSTEM
CopyFile lpExistingFileName – name of an existing file lpNewFileName – name of new file bFailIfExists – if TRUE existing file will not be overwritten
CopyFile2
CopyFileEx (1)
CopyFileEx (2) lpProgressRoutine – callback function (CopyProgressRoutine) lpData – argument to be passed to callback function (can be NULL) pbCancel – operation canceled if it is set to TRUE dwCopyFlags – specify, how file is to be copied
CopyProgressRoutine (1)
CopyProgressRoutine (2) TotalFileSize – file size in bytes TotalBytesTransferred – number of bytes already transferred StreamSize – size of current stream in bytes SteramBytesTransferred – number of bytes already transferred from current stream dwStreamNumber – handle to current stream dwCallbackReason – reason this function was called hSourceFile – handle to the source file hDestinationFile – handle to the destination file lpData – data from CopyFileEx function
dwCallbackReason There are two reasons: CALLBACK_CHUNK_FINISHED – part of data was copied CALLBACK_STREAM_SWITCH – another stream created and is about to be copied
CopyProgressRoutine (3) This function returns: PROGRESS_CONTINUE – continue copying PROGRESS_CANCEL – cancel copying and delete destination file PROGRESS_STOP – stop copying (can be restarted later) PROGRES_QUIET – continue copying without invoking callback function
MoveFile[Ex]
DeleteFile & RemoveDirectory
Other features Enumeration Locking Encoding Archiving Memory-mapping etc.
CloseHandle Handles must be closed When program is closing, OS automatically closes all associated handles expensive operation handle leak For sockets – closesocket For registry keys - RegCloseKey
Handle leaks When closing handle fails: Memory isn’t freed Kernel keeps all information about handle Sluggish system Crash