Hi,
In my driver I have a timer which gets initialised in DriverEntry.
In a worker item queued by this timer I try to open and read from a file.
Using ZwCreateFile I can open the file successfully.
ntStatus = ZwCreateFile( &hFile,
FILE_READ_DATA | FILE_WRITE_DATA,
&ObjectAttributes,
&IoStatusBlock,
NULL,
FILE_ATTRIBUTE_NORMAL,
0,
FILE_OVERWRITE_IF,
FILE_WRITE_THROUGH |
FILE_SYNCHRONOUS_IO_NONALERT,
NULL,
0 );
After the file is opened I do the following to get a file object from the
file handle.
ntStatus = ObReferenceObjectByHandle( hFile, 0, NULL, KernelMode,
&pFileObject, NULL );
If the file doesn’t exist ( IoStatusBlock.Information == FILE_CREATED ).
I can then write to the file successfully.
Generic read/write functions are used to access the file and they both do
the following
to ensure a valid handle.
ntStatus = ObOpenObjectByPointer( pFileObject,
0,
NULL,
FILE_READ_DATA |
FILE_WRITE_DATA,
NULL,
KernelMode ,
&hFile );
However if the file did exist I try to read in the contents of the file
(about 20 bytes).
The file will always be a minimum of HEADER_STRUCTURE bytes in length .
ULONG dwLength;
LARGE_INTEGER li;
HEADER_STRUCTURE HeaderStructure;
li.QuadPart = 0; // read from offset 0
dwLength = sizeof( HEADER_STRUCTURE ); // about 20 bytes
ntStatus = ZwReadFile( hFile, NULL, NULL, NULL, &IoStatusBlock,
&HeaderStructure, dwLength, &li, NULL );
ZwClose(hFile)
ZwReadFile returns STATUS_END_OF_FILE. Neither the data buffer or the
IoStatusBlock are changed.
Why isn’t this isn’t working?
Rob Linegar
Software Engineer
Data Encryption Systems Limited