KdPrint(("The program will write bytes: %d\n", BUFFER_SIZE));
ntStatus = ZwWriteFile(hFile, NULL, NULL, NULL, &ioStatus,
pBuffer, BUFFER_SIZE, NULL, NULL);
if(!NT_SUCCESS(ntStatus))
{
KdPrint(("Write file unsuccessfully\n"));
}
KdPrint(("The program really writes bytes: %d\n", ioStatus.Information));
//Query file length
FILE_STANDARD_INFORMATION fsi;
ntStatus = ZwQueryInformationFile(hFile,
&ioStatus,
&fsi,
sizeof(FILE_STANDARD_INFORMATION),
FileStandardInformation);
if(!NT_SUCCESS(ntStatus))
{
KdPrint(("Query file unsuccessfully\n"));
}
KdPrint(("The program will read bytes: %d\n", fsi.EndOfFile.QuadPart));
//allocate pool for file which is to be read
PUCHAR pBuffer2 = (PUCHAR)ExAllocatePool(PagedPool, (LONG)fsi.EndOfFile.QuadPart);
//read file
ntStatus = ZwReadFile(hFile, NULL, NULL, NULL, &ioStatus,
pBuffer2, (LONG)fsi.EndOfFile.QuadPart, 0, NULL);
if(!NT_SUCCESS(ntStatus))
{
//if sataus == STATUS_END_OF_FILE, the file has reached the end.
if(ntStatus == STATUS_END_OF_FILE)
{
KdPrint(("Error: %x\n", ntStatus));
}
}
else
{
KdPrint(("The program really reads bytes: %d\n", ioStatus.Information));
}
//Free the pools
ExFreePool(pBuffer);
ExFreePool(pBuffer2);
//close file handle
ZwClose(hFile);
}
调试信息如下:
Enter DriverEntry
The program will write bytes: 1024
The program really writes bytes: 1024
The program will read bytes: 1024
Error: c0000011
Leave DriverEntry
的确,如你所说,在写入文件了以后我没有更新文件指针:
ZwWriteFile updates the current file position by adding the number of bytes written when it completes the write operation, if it is using the current file position maintained by the I/O Manager.