320 likes | 356 Views
File Management. What is File?. File I/O device: File File stream Directory Physical disk Volume Console buffer Tape drive Communications resource Mailslot Pipe. HANDLE WINAPI CreateFile( _In_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess,
E N D
What is File? • File • I/O device: • File • File stream • Directory • Physical disk • Volume • Console buffer • Tape drive • Communications resource • Mailslot • Pipe
HANDLE WINAPI CreateFile( _In_ LPCTSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes, _In_opt_ HANDLE hTemplateFile);
lpFileName • The name of the file or device to be created or opened. • Name can have either forward slashes (/) or backslashes (\) used.
dwDesiredAccess • Requested access to the file or device: • GENERIC_READ • GENERIC_WRITE • GENERIC_EXECUTE • GENERIC_ALL • Etc. • If zero, querying some metadata available
lpSecurityAttributes • A pointer to a SECURITY_ATTRIBUTES • Can be NULL (default security descriptor will be used) • Child process can’t inherit file handle
dwCreationDisposition • What action should be taken:
dwFlagsAndAttributes • FILE_ATTRIBUTE_ARCHIVE • FILE_ATTRIBUTE_ENCRYPTED • FILE_ATTRIBUTE_HIDDEN • FILE_ATTRIBUTE_NORMAL • 20 more…
hTemplateFile • Handle to a template file - supplies file attributes and extended attributes
Is there something simple? HANDLE WINAPI CreateFile2( _In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_ DWORD dwCreationDisposition, _In_opt_ LPCREATEFILE2_EXTENDED_PARAMETERS pCreateExParams);
Copy, transfer and renaming • CopyFile • CopyFile2 • CopyFileEx
CopyFile • lpExistingFileName– pointer to string • lpNewFileName– pointer to string • bFailIfExists– if TRUE, existing file will be not overwritten
CopyFileEx (2) • lpProgressRoutine– CALLBACK function, called after portion of file is copied • lpData– argument to be passed to the CALLBACK function (can be NULL) • pbCancel– if it becames TRUE during copy operation, operation is canceled • dwCopyFlags–specify how the file is to be copied
dwCopyFlags • COPY_FILE_ALLOW_DECRYPTED_DESTINATION • COPY_FILE_FAIL_IF_EXISTS • COPY_FILE_OPEN_SOURCE_FOR_WRITE • COPY_FILE_RESTARTABLE
CopyProgressRoutine (2) • TotalFileSize – file size in bytes • TotalBytesTransferred – total transfered bytes • StreamSize – current stream size in bytes • StreamBytesTransferred – transfered bytes in stream • dwStreamNumber – handle to current stream • dwCallbackReason – reason why function was called • hSourceFile – handle to source file • hDestinationFile – handle to destination file • lpData – argument from CopyFileEx function
dwCallbackReason • CALLBACK_CHUNK_FINISHED • CALLBACK_STREAM_SWITCH
CopyProgressRoutine (3) • PROGRESS_CONTINUE • PROGRESS_CANCEL • PROGRESS_STOP • PROGRESS_QUIET
ReadFile BOOL WINAPI ReadFile( _In_ HANDLE hFile, _Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead, _Inout_opt_ LPOVERLAPPED lpOverlapped);
ReadFileEx BOOL WINAPI ReadFileEx( _In_ HANDLE hFile, _Out_opt_ LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Inout_ LPOVERLAPPED lpOverlapped, _In_ LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
OVERLAPPED • Structure to contain information for asynchronous (or overlapped) I/O typedef struct _OVERLAPPED { ULONG_PTR Internal; ULONG_PTR InternalHigh; union { struct { DWORD Offset; DWORD OffsetHigh; }; PVOID Pointer; }; HANDLE hEvent; } OVERLAPPED, *LPOVERLAPPED;
WriteFile BOOL WINAPI WriteFile( _In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped);
WriteFileEx BOOL WINAPI WriteFileEx( _In_ HANDLE hFile, _In_opt_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Inout_ LPOVERLAPPED lpOverlapped, _In_ LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine );
OpenFile • Creates, opens, reopens, or deletes a file • Obsolete (use CreateFile) HFILE WINAPI OpenFile( _In_ LPCSTR lpFileName, _Out_ LPOFSTRUCT lpReOpenBuff, _In_ UINT uStyle);
Don’t forget to use • CloseHandle to release resources. • If not – some problems may occur.
Other “things” There are a lot of functions to manage files. Feel free to use MSDN library.