Edit

WinI3CInitializeTarget function (wini3c.h)

Readies a particular WinI3C-Target to be used by the WinI3C library, and returns a library-managed WinI3CTarget handle.

WinI3CInitializeTarget is a Library-managed WinI3C API (the recommended way to use WinI3C). Initialization by the WinI3C library opens and manages a file handle to the device-interface, so the application can use the simpler, blocking library-managed WinI3C APIs (such as WinI3CWrite and WinI3CRead) instead of managing overlapped I/O itself.

Syntax

_Must_inspect_result_
HRESULT WINAPI WinI3CInitializeTarget(
    _In_z_ PCWSTR targetDeviceInterfacePath,
    _In_opt_ void* initializedCallbackContext,
    _In_ WINI3CTARGETINITIALIZEDCALLBACK initializedCallback,
    _In_opt_ void* removedCallbackContext,
    _In_ WINI3CTARGETREMOVEDCALLBACK removedCallback);

Parameters

[in] targetDeviceInterfacePath

A device-interface-path describing a WinI3C-Target.

The vendor-defined device-interface-class of targetDeviceInterfacePath shall be one that was specified by a WinI3C Vendor Extension INF.

The path may have been enumerated by a WinI3CRegisterForTargetAdded callback, but this is not required — any standard enumeration mechanism (e.g CM_Get_Device_Interface_List) may be used.

[in, optional] initializedCallbackContext

Optional client context passed back to the WINI3CTARGETINITIALIZEDCALLBACK.

Specify NULL to not supply a context.

[in] initializedCallback

The client-defined WINI3CTARGETINITIALIZEDCALLBACK to invoke when the WinI3C-Target has successfully initialized.

The callback is always invoked on the thread that called WinI3CInitializeTarget, and is invoked before WinI3CInitializeTarget returns S_OK. It is not invoked if WinI3CInitializeTarget fails.

[in, optional] removedCallbackContext

Optional client context passed back to the WINI3CTARGETREMOVEDCALLBACK.

Specify NULL to not supply a context.

[in] removedCallback

The client-defined WINI3CTARGETREMOVEDCALLBACK to invoke when the WinI3C-Target is removed (e.g. the WinI3C-Target was disabled via DeviceManager, the HostController-Instance experienced a fatal error).

All outstanding I/O for removedTarget is canceled before this callback is invoked.

All new I/O for removedTarget shall fail with HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_CONNECTED).

Calling WinI3CRelease does not invoke this callback.

The order in which WinI3C-Targets are reported removed is not defined and may change.

There is no guarantee as to which thread the callback is invoked on, and it may be invoked before WinI3CInitializeTarget returns.

The client shall not throw exceptions from this callback; all exceptions are caught and ignored by the library.

Return value

On success this function returns S_OK.

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

Return code Description
S_OK Successfully initialized a WinI3C-Target. Destroy it with WinI3CRelease when it is no longer needed.
E_INVALIDARG One or more parameters are invalid.
E_OUTOFMEMORY Available memory is insufficient to execute this function.
HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED) A WinI3CTarget with this device-interface-path already exists.
HRESULT_FROM_WIN32(ERROR_DEVICE_IN_USE) The library could not open a handle to the WinI3C-Target because another process has exclusive access to it.
HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_CONNECTED) The WinI3C-Target described by the device-interface-path is not connected.

Remarks

After initialization, the library is responsible for managing the file handle to the underlying device-interface. For more granular (and advanced) control of the file handle and I/O, do not use WinI3CInitializeTarget; instead use the non-library-managed ...WithFileHandle APIs, where the client maintains control of the file handle.

Only a single WinI3CTarget handle may exist at any time for a specific WinI3C-Target. A client can repeatedly initialize (with WinI3CInitializeTarget) and destroy (with WinI3CRelease) handles to a WinI3C-Target.

Once a WinI3C-Target has been removed (as notified by removedCallback), the WinI3CTarget handle becomes invalid for new I/O (returning HRESULT_FROM_WIN32(ERROR_DEVICE_NOT_CONNECTED)) and shall be destroyed with WinI3CRelease, which may be called from within removedCallback. If a WinI3C-Target is removed and re-added, a new handle shall be created; the old handle cannot be reused.

A WinI3C-Target may be removed between being enumerated and being initialized, so WinI3CInitializeTarget is not guaranteed to succeed for any particular device-interface-path.

This function is thread-safe.

Example

void main()
{
    ...

    if (FAILED(WinI3CInitializeTarget(
        addedTargetDeviceInterfacePath,
        context,
        EvtTargetInitialized,
        context,
        EvtTargetRemoved)))
    {
        // Handle failure.
    }

    ...
}

void CALLBACK EvtTargetInitialized(
    _In_opt_ void* initializedCallbackContext,
    _In_ WinI3CTarget initializedTarget)
{
    ...
}

void CALLBACK EvtTargetRemoved(
    _In_opt_ void* removedCallbackContext,
    _In_ WinI3CTarget removedTarget)
{
    ...
}

Requirements

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

See also

WinI3CRegisterForTargetAdded

WinI3CRelease

WINI3CTARGETINITIALIZEDCALLBACK

WINI3CTARGETREMOVEDCALLBACK