欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > windows源码ReadFile函数的实现

windows源码ReadFile函数的实现

2024/10/25 8:14:42 来源:https://blog.csdn.net/zhyjhacker/article/details/142427109  浏览:    关键词:windows源码ReadFile函数的实现

windows源码ReadFile函数的实现

windows源码ReadFile函数的实现

文章目录

  • windows源码ReadFile函数的实现
  • ReadFile


ReadFile


BOOL
WINAPI
ReadFile(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped)/*++Routine Description:Data can be read from a file using ReadFile.This API is used to read data from a file.  Data is read from thefile from the position indicated by the file pointer.  After theread completes, the file pointer is adjusted by the number of bytesactually read.  A return value of TRUE coupled with a bytes read of0 indicates that the file pointer was beyond the current end of thefile at the time of the read.Arguments:hFile - Supplies an open handle to a file that is to be read.  Thefile handle must have been created with GENERIC_READ access tothe file.lpBuffer - Supplies the address of a buffer to receive the data readfrom the file.nNumberOfBytesToRead - Supplies the number of bytes to read from thefile.lpNumberOfBytesRead - Returns the number of bytes read by this call.This parameter is always set to 0 before doing any IO or errorchecking.lpOverlapped - Optionally points to an OVERLAPPED structure to be used with therequest. If NULL then the transfer starts at the current file positionand ReadFile will not return until the operation completes.If the handle hFile was created without specifying FILE_FLAG_OVERLAPPEDthe file pointer is moved to the specified offset pluslpNumberOfBytesRead before ReadFile returns. ReadFile will wait for therequest to complete before returning (it will not returnERROR_IO_PENDING).When FILE_FLAG_OVERLAPPED is specified, ReadFile may returnERROR_IO_PENDING to allow the calling function to continue processingwhile the operation completes. The event (or hFile if hEvent is NULL) willbe set to the signalled state upon completion of the request.When the handle is created with FILE_FLAG_OVERLAPPED and lpOverlappedis set to NULL, ReadFile will return ERROR_INVALID_PARAMTER becausethe file offset is required.Return Value:TRUE - The operation was successul.FALSE - The operation failed.  Extended error status is availableusing GetLastError.--*/{NTSTATUS Status;IO_STATUS_BLOCK IoStatusBlock;PPEB Peb;DWORD InputMode;if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {*lpNumberOfBytesRead = 0;}Peb = NtCurrentPeb();switch( HandleToUlong(hFile) ) {case STD_INPUT_HANDLE:  hFile = Peb->ProcessParameters->StandardInput;break;case STD_OUTPUT_HANDLE: hFile = Peb->ProcessParameters->StandardOutput;break;case STD_ERROR_HANDLE:  hFile = Peb->ProcessParameters->StandardError;break;}if (CONSOLE_HANDLE(hFile)) {if (ReadConsoleA(hFile,lpBuffer,nNumberOfBytesToRead,lpNumberOfBytesRead,lpOverlapped)) {Status = STATUS_SUCCESS;if (!GetConsoleMode( hFile, &InputMode )) {InputMode = 0;}if (InputMode & ENABLE_PROCESSED_INPUT) {try {if (*(PCHAR)lpBuffer == 0x1A) {*lpNumberOfBytesRead = 0;}}except( EXCEPTION_EXECUTE_HANDLER ) {Status = GetExceptionCode();}}if (NT_SUCCESS(Status)) {return TRUE;}else {BaseSetLastNTError(Status);return FALSE;}}else {return FALSE;}}if ( ARGUMENT_PRESENT( lpOverlapped ) ) {LARGE_INTEGER Li;lpOverlapped->Internal = (DWORD)STATUS_PENDING;Li.LowPart = lpOverlapped->Offset;Li.HighPart = lpOverlapped->OffsetHigh;Status = NtReadFile(hFile,lpOverlapped->hEvent,NULL,(ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,(PIO_STATUS_BLOCK)&lpOverlapped->Internal,lpBuffer,nNumberOfBytesToRead,&Li,NULL);if ( NT_SUCCESS(Status) && Status != STATUS_PENDING) {if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {try {*lpNumberOfBytesRead = (DWORD)lpOverlapped->InternalHigh;}except(EXCEPTION_EXECUTE_HANDLER) {*lpNumberOfBytesRead = 0;}}return TRUE;}elseif (Status == STATUS_END_OF_FILE) {if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {*lpNumberOfBytesRead = 0;}BaseSetLastNTError(Status);return FALSE;}else {BaseSetLastNTError(Status);return FALSE;}}else{Status = NtReadFile(hFile,NULL,NULL,NULL,&IoStatusBlock,lpBuffer,nNumberOfBytesToRead,NULL,NULL);if ( Status == STATUS_PENDING) {// Operation must complete before return & IoStatusBlock destroyedStatus = NtWaitForSingleObject( hFile, FALSE, NULL );if ( NT_SUCCESS(Status)) {Status = IoStatusBlock.Status;}}if ( NT_SUCCESS(Status) ) {*lpNumberOfBytesRead = (DWORD)IoStatusBlock.Information;return TRUE;}elseif (Status == STATUS_END_OF_FILE) {*lpNumberOfBytesRead = 0;return TRUE;}else {if ( NT_WARNING(Status) ) {*lpNumberOfBytesRead = (DWORD)IoStatusBlock.Information;}BaseSetLastNTError(Status);return FALSE;}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com