以下代码在用户模式下正常工作:
#include <stdio.h>
#include <ctype.h>
int main()
{
//
// 0x7f51 is the unicode code of Chinese character '网'
//
int n = tolower(0x7f51); // n will equal 0x7f51
}
但是,如果我们处于内核模式,n
将等于0x7f71
!!!
最简单的示例代码:
#include <ntifs.h>
ULONG NTAPI DriverEntry(PDRIVER_OBJECT, PUNICODE_STRING)
{
int n = tolower(0x7f51); // n will equal 0x7f71 !!!
return 0;
}
这是ntoskrnl.exe中tolower
实施中的一个大错误吗?
答案 0 :(得分:5)
tolower(int c)
仅针对整数c
定义,它们是EOF或可表示为unsigned char。 0x7f51
既不是。因此,tolower(0x7f51)
的行为未定义。
答案 1 :(得分:1)
tolower()是针对ANSI字符而设计的,而不是针对Unicode的。 C程序的语言环境设置会影响其行为。 最好在NT内核中使用Windows特定的转换函数。