1 /* $Id: rw.c,v 1.12 2000/05/13 13:50:56 dwelch Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/rw.c
6 * PURPOSE: Read/write functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
12 /* INCLUDES ****************************************************************/
14 #include <ddk/ntddk.h>
20 #include <kernel32/kernel32.h>
23 /* FUNCTIONS ****************************************************************/
25 WINBOOL STDCALL
WriteFile(HANDLE hFile
,
27 DWORD nNumberOfBytesToWrite
,
28 LPDWORD lpNumberOfBytesWritten
,
29 LPOVERLAPPED lpOverLapped
)
34 IO_STATUS_BLOCK IIosb
;
35 PIO_STATUS_BLOCK IoStatusBlock
;
36 PLARGE_INTEGER ptrOffset
;
38 DPRINT("WriteFile(hFile %x)\n",hFile
);
40 if (IsConsoleHandle(hFile
))
42 return(WriteConsoleA(hFile
,
44 nNumberOfBytesToWrite
,
45 lpNumberOfBytesWritten
,
49 if (lpOverLapped
!= NULL
)
51 Offset
.u
.LowPart
= lpOverLapped
->Offset
;
52 Offset
.u
.HighPart
= lpOverLapped
->OffsetHigh
;
53 lpOverLapped
->Internal
= STATUS_PENDING
;
54 hEvent
= lpOverLapped
->hEvent
;
55 IoStatusBlock
= (PIO_STATUS_BLOCK
)lpOverLapped
;
61 IoStatusBlock
= &IIosb
;
65 errCode
= NtWriteFile(hFile
,
71 nNumberOfBytesToWrite
,
74 if (!NT_SUCCESS(errCode
))
76 SetLastError(RtlNtStatusToDosError(errCode
));
77 DPRINT("WriteFile() failed\n");
80 if (lpNumberOfBytesWritten
!= NULL
)
82 *lpNumberOfBytesWritten
= IoStatusBlock
->Information
;
84 DPRINT("WriteFile() succeeded\n");
90 WINBOOL STDCALL
ReadFile(HANDLE hFile
,
92 DWORD nNumberOfBytesToRead
,
93 LPDWORD lpNumberOfBytesRead
,
94 LPOVERLAPPED lpOverLapped
)
99 IO_STATUS_BLOCK IIosb
;
100 PIO_STATUS_BLOCK IoStatusBlock
;
101 PLARGE_INTEGER ptrOffset
;
103 DbgPrint("ReadFile(hFile %x)\n", hFile
);
105 if (IsConsoleHandle(hFile
))
107 return(ReadConsoleA(hFile
,
109 nNumberOfBytesToRead
,
114 if (lpOverLapped
!= NULL
)
116 Offset
.u
.LowPart
= lpOverLapped
->Offset
;
117 Offset
.u
.HighPart
= lpOverLapped
->OffsetHigh
;
118 lpOverLapped
->Internal
= STATUS_PENDING
;
119 hEvent
= lpOverLapped
->hEvent
;
120 IoStatusBlock
= (PIO_STATUS_BLOCK
)lpOverLapped
;
126 IoStatusBlock
= &IIosb
;
129 errCode
= NtReadFile(hFile
,
135 nNumberOfBytesToRead
,
139 if (errCode
!= STATUS_PENDING
&& lpNumberOfBytesRead
!= NULL
)
141 *lpNumberOfBytesRead
= IoStatusBlock
->Information
;
144 if (!NT_SUCCESS(errCode
))
146 SetLastError(RtlNtStatusToDosError(errCode
));
152 VOID
ApcRoutine(PVOID ApcContext
,
153 struct _IO_STATUS_BLOCK
* IoStatusBlock
,
154 ULONG NumberOfBytesTransfered
)
157 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
=
158 (LPOVERLAPPED_COMPLETION_ROUTINE
)ApcContext
;
160 dwErrorCode
= RtlNtStatusToDosError(IoStatusBlock
->Status
);
161 lpCompletionRoutine(dwErrorCode
,
162 NumberOfBytesTransfered
,
163 (LPOVERLAPPED
)IoStatusBlock
);
167 WINBOOL STDCALL
WriteFileEx (HANDLE hFile
,
169 DWORD nNumberOfBytesToWrite
,
170 LPOVERLAPPED lpOverLapped
,
171 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
175 LARGE_INTEGER Offset
;
177 PIO_STATUS_BLOCK IoStatusBlock
;
178 PLARGE_INTEGER ptrOffset
;
180 DPRINT("WriteFileEx(hFile %x)\n",hFile
);
182 if (lpOverLapped
== NULL
)
185 Offset
.u
.LowPart
= lpOverLapped
->Offset
;
186 Offset
.u
.HighPart
= lpOverLapped
->OffsetHigh
;
187 lpOverLapped
->Internal
= STATUS_PENDING
;
188 IoStatusBlock
= (PIO_STATUS_BLOCK
)lpOverLapped
;
191 errCode
= NtWriteFile(hFile
,
197 nNumberOfBytesToWrite
,
200 if (!NT_SUCCESS(errCode
))
202 SetLastError(RtlNtStatusToDosError(errCode
));
203 DPRINT("WriteFileEx() failed\n");
207 DPRINT("WriteFileEx() succeeded\n");
211 WINBOOL STDCALL
ReadFileEx(HANDLE hFile
,
213 DWORD nNumberOfBytesToRead
,
214 LPOVERLAPPED lpOverLapped
,
215 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
217 LARGE_INTEGER Offset
;
219 PIO_STATUS_BLOCK IoStatusBlock
;
220 PLARGE_INTEGER ptrOffset
;
222 if (lpOverLapped
== NULL
)
225 Offset
.u
.LowPart
= lpOverLapped
->Offset
;
226 Offset
.u
.HighPart
= lpOverLapped
->OffsetHigh
;
227 lpOverLapped
->Internal
= STATUS_PENDING
;
228 IoStatusBlock
= (PIO_STATUS_BLOCK
)lpOverLapped
;
231 errCode
= NtReadFile(hFile
,
237 nNumberOfBytesToRead
,
241 if (!NT_SUCCESS(errCode
))
243 SetLastError(RtlNtStatusToDosError(errCode
));