StringLengthFromUser 函数 (usermode_accessors.h)

The StringLengthFromUser function safely calculates the length of a null-terminated string in user-mode memory.

Syntax

SIZE_T StringLengthFromUser(
  const CHAR *String
);

Parameters

String

[in]指向用户模式内存中以 null 结尾的字符串的指针。

Return value

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

Remarks

此函数提供安全访问,用于计算位于用户模式内存中以 null 结尾的字符串的长度。 它设计用于内核模式代码,这些代码需要确定从用户模式应用程序传递的字符串的长度。

该函数确保在执行长度计算之前,字符串地址是有效的用户模式地址。 如果作失败(例如字符串地址不是有效的用户模式地址或不可访问),则会引发结构化异常。

与标准字符串函数不同,此函数包括验证,以防止访问可能无效的用户模式指针时内核崩溃。 这使得在处理用户模式字符串数据时对安全内核编程至关重要。

此函数适用于所有版本的 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

StringLengthFromMode

WideStringLengthFromUser