为什么 MAKEINTRESOURCE() 有效?

2021-12-18 00:00:00 windows c winapi c++

宏定义为:

#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
#define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i))))

为什么这可以用来指示资源 ID(16 位无符号整数)或其名称(指向字符数组的指针)?这不是有效地将地址空间(在 32 位系统上)限制为 16 位吗?否则系统怎么知道我用的是ID还是名字?

How come this can be used to indicate either a resource ID (a 16-bit unsigned int) or its name (a pointer to an array of char)? Doesn't this effectively limit the address space (on a 32-bit system) to 16-bit? Otherwise how does the system know whether I'm using an ID or a name?

推荐答案

这是可行的,因为 Windows 不允许为地址空间的前 64 KB 映射页面.捕获空指针引用.但我认为还要捕捉从 16 位版本的 Windows 转换而来的程序中的指针错误.

This works because Windows doesn't allow mapping pages for the first 64 KB of the address space. To catch null pointer references. But I think also to catch pointer bugs in programs that were converted from the 16-bit version of Windows.

一个副作用是,这允许可靠地区分打包成指针值的资源 ID,因为它们总是指向不可映射的内存.

A side-effect is that this allows to reliably distinguish resource IDs packed into a pointer value since they'll always point to non-mappable memory.

相关文章