WriteFileEx
The WriteFileEx
function writes data to a file. It is designed solely for asynchronous
operation, unlike WriteFile
WriteFileEx reports its completion status asynchronously, calling
a specified completion routine when writing is completed and the calling thread
is in an alertable wait state.
BOOL WriteFileEx(
HANDLE hFile, |
// handle to output
file |
LPCVOID lpBuffer, |
// pointer to input
buffer |
DWORD nNumberOfBytesToWrite, |
// number of bytes
to write |
LPOVERLAPPED lpOverlapped, |
// pointer to
async. i/o data |
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine |
// ptr. to
completion routine |
); |
|
Parameters
hFile
An open
handle that specifies the file entity to be written to. This file handle must
have been created with the FILE_FLAG_OVERLAPPED flag and with GENERIC_WRITE
access to the file.
Windows NT: hFile can be any handle opened with the
FILE_FLAG_OVERLAPPED flag by the CreateFile
Windows 95: hFile can be a communications resource, mailslot, or named
pipe handle opened with the FILE_FLAG_OVERLAPPED flag by CreateFile, or
a socket handle returned by the socket or accept functions.
Windows 95 does not support asynchronous operations on disk files.
lpBuffer
Points to the
buffer containing the data to be written to the file.
This buffer must remain valid for the duration of the write operation.
The caller must not use this buffer until the write operation is completed.
nNumberOfBytesToWrite
Specifies the
number of bytes to write to the file.
If nNumberOfBtyesToWrite is zero, this function does nothing; in
particular, it does not truncate the file. For additional discussion, see the
following Remarks section.
lpOverlapped
Points to an OVERLAPPED
For files that support byte offsets, you must specify a byte offset at
which to start writing to the file. You specify this offset by setting the Offset
and OffsetHigh members of the OVERLAPPED structure. For files
that do not support byte offsets named pipes, for example you must set Offset and OffsetHigh to zero, or WriteFileEx
fails.
The WriteFileEx function ignores the OVERLAPPED
structure s hEvent member. An application is free to use that member for
its own purposes in the context of a WriteFileEx call. WriteFileEx
signals completion of its writing operation by calling, or queueing a call to,
the completion routine pointed to by lpCompletionRoutine, so it does not
need an event handle.
The WriteFileEx function does use the Internal and InternalHigh
members of the OVERLAPPED structure. You should not change the value of
these members.
The OVERLAPPED
lpCompletionRoutine
Points to a
completion routine to be called when the write operation has been completed and
the calling thread is in an alertable wait state. For more information about
this completion routine, see FileIOCompletionRoutine
Return Values
If the
function succeeds, the return value is nonzero.
If the
function fails, the return value is zero. To get extended error information,
call GetLastError
If the WriteFileEx
function succeeds, the calling thread has an asynchronous I/O
(input/output) operation pending: the overlapped write operation to the file.
When this I/O operation finishes, and the calling thread is blocked in an
alertable wait state, the operating system calls the function pointed to by lpCompletionRoutine,
and the wait completes with a return code of WAIT_IO_COMPLETION.
If the
function succeeds and the file-writing operation finishes, but the calling
thread is not in an alertable wait state, the system queues the call to *lpCompletionRoutine,
holding the call until the calling thread enters an alertable wait state.
See Synchronization
Remarks
If part of
the output file is locked by another process, and the specified write operation
overlaps the locked portion, the WriteFileEx function fails.
Applications
must not read from nor write to the output buffer that a write operation is
using until the write operation completes. Premature access of the output
buffer may lead to corruption of the data written from that buffer.
The WriteFileEx
function may fail, returning the messages ERROR_INVALID_USER_BUFFER or
ERROR_NOT_ENOUGH_MEMORY if there are too many outstanding asynchronous I/O
requests.
To cancel all
pending asynchronous I/O operations, use the CancelIO
If hFile
is a handle to a named pipe, or other file entity that doesn t support byte
offsets, the Offset and OffsetHigh members of the OVERLAPPED
structure pointed to by lpOverlapped must be zero, or the WriteFileEx
function fails.
An
application uses the WaitForSingleObjectEx, WaitForMultipleObjectsEx,
MsgWaitForMultipleObjectsEx, SignalObjectAndWait, and SleepEx
functions to enter an alertable wait state. Refer to Synchronization
Windows
95: On this platform, neither WriteFileEx
nor ReadFileEx can be used by the comm ports to communicate. However,
you can use WriteFile and ReadFile to perform asynchronous
communication.
See Also