StringLengthFromMode 函数 (usermode_accessors.h)

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

Syntax

SIZE_T StringLengthFromMode(
  const CHAR      *String,
  KPROCESSOR_MODE Mode
);

Parameters

String

[in]指向以 null 结尾的字符串的指针。

Mode

[in]确定内存访问的执行方式的处理器模式。 Mode can be one of the following values.

Value Meaning
KernelMode String points to kernel-mode memory. 该函数执行直接字符串长度计算。 有关更多详细信息,请参阅“备注”。
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. 有关更多详细信息,请参阅“备注”。

Return value

该函数以字符为单位返回字符串的长度,不包括终止 null 字符。

Remarks

此函数提供安全访问,用于计算内存中以 null 结尾的字符串的长度。 行为取决于指定的处理器模式:

  • When Mode is UserMode, the function ensures that the string address is a valid user-mode address and performs the operation safely for user-mode memory access.
  • When Mode is KernelMode, the function operates directly on kernel-mode addresses.

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.

如果作失败,函数将引发结构化异常,例如当字符串地址不可访问或指定模式无效时。

此函数适用于所有版本的 Windows,而不仅仅是最新版本。 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. 但是,生成的驱动程序将在较旧版本的 Windows 上运行。

Requirements

Requirement Value
最低支持的客户端 See Remarks
Header usermode_accessors.h
Library umaccess.lib
IRQL 小于或等于APC_LEVEL

See also

StringLengthFromUser

WideStringLengthFromMode