Windows驱动4. 常用数据类型
Windows驱动4. 常用数据类型
字符串
在驱动中, 字符串并不建议使用C中的Ascii字符串或者C++中的std::string, 而是使用 UNICODE_STRING 结构体作为字符串
| 1 |  | 
字符串的初始化
| 1 |  | 
RtlInitUnicodeString 和 RtlCreateUnicodeString 的不同:
- RtlInitUnicodeString(&str, source)仅仅初始化一个字符串, 不会分配内存, 它只会把内存地址赋值给source, 并计算字符串的长度和最大长度存入- Length和- MaximumLength中, 其生命周期由完全由- source决定
- RtlCreateUnicodeString(&str, source)会分配内存, 并把source的内容拷贝到新分配的内存中, 需要手动调用- RtlFreeUnicodeString释放内存
| Warning | 调用 RtlInitUnicodeString如果字符串是一个常量字符串, 则不需要手动释放内存, 否则会导致蓝屏 | 
字符串的操作
| 函数 | 说明 | 
|---|---|
| RtlInitUnicodeString | 初始化字符串 | 
| RtlCreateUnicodeString | 创建字符串 | 
| RtlAppendUnicodeStringToString | 追加字符串 | 
| RtlCopyUnicodeString | 复制字符串 | 
| RtlEqualUnicodeString | 比较字符串 | 
| RtlFreeUnicodeString | 释放字符串 | 
基础类型
基础类型尽量使用大写形式的类型, 在打印时其占位符如下:
| 类型 | 占位符 | 
|---|---|
| SHORT | %hd | 
| INT | %d | 
| LONG | %ld | 
| HEX | %x(小写) 或 %X(大写) | 
| char* | %s | 
| wchar_t* | %S | 
| ANSI_STRING | %Z | 
| UNICODE_STRING | %wZ | 
Windows驱动4. 常用数据类型
https://simonkimi.githubio.io/2024/08/03/Windows驱动4-常用数据类型/