Kernel32WriteFile(SafeFileHandle, IntPtr, UInt32, UInt32, IntPtr) Method

Writes data to the specified file or input/output (I/O) device..

This function is designed for both synchronous and asynchronous operation. For a similar function designed solely for asynchronous operation, see WriteFileEx.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", ExactSpelling = true, SetLastError = true)]
public static bool WriteFile(
	SafeFileHandle hFile,
	IntPtr buffer,
	uint numberOfBytesToWrite,
	out uint refNumberOfBytesWritten,
	[OptionalAttribute] IntPtr overlapped
)

Parameters

hFile  SafeFileHandle
A handle to the file or I/O device (for example, a file, file stream, physical disk, volume, console buffer, tape drive, socket, communications resource, mailslot, or pipe).

The hFile parameter must have been created with write access. For more information, see Generic Access Rights and File Security and Access Rights.

For asynchronous read operations, hFile can be any handle that is opened with the Overlapped flag by the CreateFile(String, FileAccessRights, FileShare, IntPtr, FileMode, CreateFileFlags, IntPtr) function, or a socket handle returned by the socket or accept function.

buffer  IntPtr
A pointer to the buffer containing the data to be written to the file or device.

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.

numberOfBytesToWrite  UInt32
The number of bytes to be written to the file or device.

A value of zero specifies a null write operation.

The behavior of a null write operation depends on the underlying file system or communications technology.

refNumberOfBytesWritten  UInt32
A pointer to the variable that receives the number of bytes written when using a synchronous hFile parameter.

WriteFile(SafeFileHandle, Byte, Int32, Int32, NativeOverlapped) sets this value to zero before doing any work or error checking.

Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

This parameter can be NULL only when the overlapped parameter is not NULL.

overlapped  IntPtr  (Optional)
A pointer to an OVERLAPPED structure is required if the hFile parameter was opened with Overlapped, otherwise this parameter can be NULL.

For an hFile that supports byte offsets, if you use this parameter you must specify a byte offset at which to start writing to the file or device. This offset is specified by setting the Offset and OffsetHigh members of the OVERLAPPED structure. For an hFile that does not support byte offsets, Offset and OffsetHigh are ignored.

To write to the end of file, specify both the Offset and OffsetHigh members of the OVERLAPPED structure as 0xFFFFFFFF. This is functionally equivalent to previously calling the CreateFile(String, FileAccessRights, FileShare, IntPtr, FileMode, CreateFileFlags, IntPtr) function to open hFile using AppendData access.

Return Value

Boolean
If the function succeeds, the return value is .

If the function fails, or is completing asynchronously, the return value is .

To get extended error information, call GetLastWin32Error.

Note: The GetLastWin32Error code ERROR_IO_PENDING is not a failure; it means the write operation is pending completion asynchronously.

Remarks

See Also