64位计算机上的DWORD和DWORD_PTR [英] DWORD and DWORD_PTR on 64 bit machine

查看:744
本文介绍了64位计算机上的DWORD和DWORD_PTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为支持Win64的64位寻址,很少将*_PTR类型添加到Windows API中.

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而不适合 .

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆