DWORD and DWORD_PTR on 64 bit machine(64 位机器上的 DWORD 和 DWORD_PTR)
问题描述
为了支持 Win64 的 64 位寻址,向 Windows API 添加了一些 *_PTR 类型.
There are few *_PTR types added to the Windows API in order to support Win64's 64bit addressing.
SetItemData(int nIndex,DWORD_PTR dwItemData)
当我将第二个参数作为 DWORD 传递时,此 API 适用于 64 位和 32 位机器.
This API works for both 64 and 32 bit machines when I pass second parameter as DWORD.
我想知道,如果我将第二个参数作为 DWORD 传递,这个特定的 API 在 64 位机器上是否会失败.如何测试失败场景?
I want to know, if this particular API will fail on 64 bit machine, if I pass the second parameter as DWORD. How can I test the fail scenario?
谢谢,尼基尔
推荐答案
如果你传递一个 DWORD,函数不会失败,因为它适合 DWORD_PTR.但是,在 64 位平台上,可以保证指针适合 DWORD_PTR,但 不 适合 DWORD.
The function will not fail if you pass a DWORD, because it fits into a DWORD_PTR. A pointer, however, is guaranteed to fit into a DWORD_PTR but not into a DWORD on 64-bit platforms.
因此,这段代码是正确的:
Thus, this code is correct:
int *before_ptr = new int;
yourListBox.SetItemData(index, (DWORD_PTR) before_ptr);
int *after_ptr = (int *) yourListBox.GetItemData(index);
ASSERT(before_ptr == after_ptr); // Succeeds.
delete after_ptr; // Works.
但是这段代码是错误的,它会默默地将指针截断到它的低 32 位:
But this code is wrong and will silently truncate the pointer to its lower 32 bits:
int *before_ptr = new int;
yourListBox.SetItemData(index, (DWORD) before_ptr);
int *after_ptr = (int *) yourListBox.GetItemData(index);
ASSERT(before_ptr == after_ptr); // Fails.
delete after_ptr; // Undefined behavior, might corrupt the heap.
这篇关于64 位机器上的 DWORD 和 DWORD_PTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:64 位机器上的 DWORD 和 DWORD_PTR
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
