Edit

WinI3CWriteReadWithFileHandle function (wini3c.h)

Initiates an overlapped operation consisting of a write to a WinI3C-Target immediately followed by a read. This corresponds to an I3C Private Write followed by an I3C Private Read. This is faster and more efficient on the bus than issuing separate write and read operations.

WinI3CWriteReadWithFileHandle is a Non-library-managed WinI3C API. It uses a client-supplied file handle and returns a WinI3CAsyncOperation for a client-managed overlapped operation. These APIs are intended only for advanced scenarios; this is generally not recommended. For most applications, use the library-managed WinI3CWriteRead instead.

Syntax

_Must_inspect_result_
HRESULT WINAPI WinI3CWriteReadWithFileHandle(
    _In_ HANDLE targetFileHandle,
    _In_ WINI3C_TARGET_MODE writePreferredMode,
    _In_reads_opt_(1) const UINT8* writeHdrCommandCode,
    _In_ UINT32 writePayloadSize,
    _In_reads_bytes_(writePayloadSize) const void* writePayload,
    _In_ WINI3C_TARGET_MODE readPreferredMode,
    _In_reads_opt_(1) const UINT8* readHdrCommandCode,
    _In_ UINT32 requestedReadPayloadSize,
    _In_ OVERLAPPED* writeReadOverlapped,
    _Out_ WinI3CAsyncOperation* writeReadOperation);

Parameters

[in] targetFileHandle

The file handle associated with the WinI3C-Target to write to and read from.

The handle shall have been opened with FILE_FLAG_OVERLAPPED in the dwFlagsAndAttributes parameter of CreateFile.

[in] writePreferredMode

The preferred WINI3C_TARGET_MODE to use for the write. This is best-effort; the actual mode may differ if the WinI3C-Target/HostController-Instance does not support the requested mode.

[in, optional] writeHdrCommandCode

If writePreferredMode is WinI3CTargetModeHdr, points to the I3C HDR Write Command Code (0x00–0x7F) to use if the transfer is sent as HDR.

Shall be NULL if writePreferredMode is not WinI3CTargetModeHdr.

[in] writePayloadSize

The number of bytes to write. The maximum write size expected by the WinI3C-Target is set with WinI3CSetMaxWriteLengthWithFileHandle.

  • Shall be > 0.
  • Shall be <= bufferSize(writePayload).
  • Shall be <= 65535 (the maximum I3C transfer size).
  • Shall be even (a multiple of 2) when writePreferredMode is WinI3CTargetModeHdr.

[in] writePayload

A client-allocated buffer that contains the payload to write.

The buffer shall not be destroyed until the overlapped operation has completed and the associated writeOperation has been destroyed with WinI3CRelease.

Shall not be NULL.

[in] readPreferredMode

The preferred WINI3C_TARGET_MODE to use for the read. This is best-effort; the actual mode may differ if the WinI3C-Target/HostController-Instance does not support the requested mode.

[in, optional] readHdrCommandCode

If readPreferredMode is WinI3CTargetModeHdr, points to the I3C HDR Read Command Code (0x80–0xFF) to use if the transfer is sent as HDR.

Shall be NULL if readPreferredMode is not WinI3CTargetModeHdr.

[in] requestedReadPayloadSize

The number of bytes to read. The maximum read size expected by the WinI3C-Target is set with WinI3CSetMaxReadAndIbiLengthWithFileHandle.

  • Shall be > 0.
  • Shall be <= 65535 (the maximum I3C transfer size).
  • Shall be even (a multiple of 2) when readPreferredMode is WinI3CTargetModeHdr.

[in] writeReadOverlapped

A client-allocated OVERLAPPED to supply to the underlying overlapped I/O call.

[out] writeReadOperation

Receives a library-created WinI3CAsyncOperation that represents this overlapped operation.

Valid only when the function returns S_OK.

After the operation completes, retrieve the payload with WinI3CGetReadPayloadFromOperation, then destroy the operation with WinI3CRelease (before destroying the associated OVERLAPPED).

Return value

On successfully initiating the operation, this function returns S_OK.

On failure this function returns an appropriate HRESULT error code, including the following.

Return code Description
S_OK Successfully initiated the operation.
E_INVALIDARG One or more parameters are invalid.
E_OUTOFMEMORY Available memory is insufficient to execute this function.
HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE) targetFileHandle is invalid, or was not opened with FILE_FLAG_OVERLAPPED.
HRESULT_FROM_WIN32(ERROR_DEVICE_HARDWARE_ERROR) A fatal hardware error occurred. Close targetFileHandle with CloseHandle.

Remarks

Use the standard Win32 I/O APIs (e.g. GetOverlappedResult, CancelIo or an I/O completion port) to manage the overlapped operation.

When the overlapped operation has completed, call WinI3CGetReadPayloadFromOperation to inspect the payload, then destroy the library-created writeReadOperation with WinI3CRelease. See WinI3CGetReadPayloadFromOperation for how write-then-read failures are reported.

This function is thread-safe.

Completion status

When the overlapped operation completes, its result (e.g. from GetOverlappedResult with HRESULT_FROM_WIN32(GetLastError())) is one of the following:

Completion code Description
S_OK Successfully completed the write-read transfer with the WinI3C-Target.
HRESULT_FROM_WIN32(ERROR_DEVICE_NACKED) The WinI3C-Target (passively) NACKed the write or the read. Consider calling WinI3CGetStatusWithFileHandle to understand the current WinI3C-Target state.
HRESULT_FROM_WIN32(ERROR_DEVICE_HARDWARE_ERROR) A fatal hardware error occurred. Close targetFileHandle with CloseHandle.

Example

#include <vector>
#include <wil\result.h>
#include <wil\resource.h>

#include <wini3c.h>

int main()
{
    ...

    // Completion event.
    // Manual-reset is the safe default for a single outstanding op used with GetOverlappedResult / WaitForSingleObject.
    OVERLAPPED writeReadOverlapped{};
    writeReadOverlapped.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
    RETURN_LAST_ERROR_IF(writeReadOverlapped.hEvent == nullptr);

    auto closeEventOnExit = wil::scope_exit([&]()
    {
        CloseHandle(writeReadOverlapped.hEvent);
    });

    WinI3CAsyncOperation writeReadOperation;
    std::vector<uint8_t> writePayload = ...;
    uint32_t requestedReadPayloadSize = ...;

    HRESULT writeReadResult = WinI3CWriteReadWithFileHandle(
        winI3cTargetHandle,
        WinI3CTargetModeSdr,
        nullptr,
        (uint32_t)writePayload.size(),
        writePayload.data(),
        WinI3CTargetModeSdr,
        nullptr,
        requestedReadPayloadSize,
        &writeReadOverlapped,
        &writeReadOperation);
    if (FAILED(writeReadResult) && writeReadResult != HRESULT_FROM_WIN32(ERROR_IO_PENDING))
    {
        // Error sending the Private Write/Read.
        return writeReadResult;
    }

    auto destroyOperationOnExit = wil::scope_exit([&]()
    {
        WinI3CRelease(writeReadOperation);
    });

    // Blocking, waiting for result (for demonstration purposes only).
    // In a real application, would use a completion port or other async mechanism to avoid blocking the thread.
    DWORD numberOfBytesTransferred;
    if (!GetOverlappedResult(winI3cTargetHandle, &writeReadOverlapped, &numberOfBytesTransferred, true))
    {
        RETURN_LAST_ERROR();
    }

    ...
}

Requirements

Requirement Value
Minimum supported client Windows 11 25H2 October update (build 26200)
Target Platform Windows
Header wini3c.h
DLL wini3c.dll

See also

WinI3CWriteRead

WinI3CGetReadPayloadFromOperation

WinI3CRelease

WINI3C_TARGET_MODE