WideStringLengthFromMode - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
// usermode_accessors.h

SIZE_T WideStringLengthFromMode(
  const WCHAR     *String,
  KPROCESSOR_MODE Mode
);
View the official Windows Driver Kit DDI reference

NtDoc

No description available.

Windows Driver Kit DDI reference (nf-usermode_accessors-widestringlengthfrommode)

Description

The WideStringLengthFromMode function calculates the length of a null-terminated wide character string, based on the specified processor mode.

Parameters

String

[in] A pointer to a null-terminated wide character string.

Mode

[in] The processor mode that determines how the memory access is performed. Mode can be one of the following values.

Value Meaning
KernelMode String points to kernel-mode memory. The function performs a direct string length calculation. See Remarks for more details.
UserMode String points to user-mode memory. The function raises an exception if String doesn't point to user-mode memory; otherwise it performs a safe string length calculation at the specified address. See Remarks for more details.

Return value

The function returns the length of the string in wide characters, not including the terminating null character.

Remarks

This function provides safe access to calculate the length of a null-terminated wide character string in memory. The behavior depends on the processor mode specified:

When the mode is UserMode, the function includes validation to prevent kernel crashes when accessing potentially invalid user-mode pointers. When the mode is KernelMode, it operates on known-safe kernel addresses for optimal performance.

The function raises a structured exception if the operation fails, such as when the string address is not accessible or is invalid for the specified mode.

This function works on all versions of Windows, not just the latest. You need to consume the latest WDK to get the function declaration from the usermode_accessors.h header. You also need the library (umaccess.lib) from the latest WDK. However, the resulting driver will run fine on older versions of Windows.

See also

WideStringLengthFromUser

StringLengthFromMode