加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

64位应用程序上的Windows HANDLE的范围是多少?

发布时间:2020-12-13 21:00:50 所属栏目:Windows 来源:网络整理
导读:在WinAPI上,HANDLE类型定义为void *,因此在64位应用程序上,HANDLE值的范围可以为0到18446744073709551615。 但实际上是真的吗?任何文件是否指定了这样一个手柄的整体范围? 如果一个人希望将这个HANDLE存储在一个32位应用程序上的一个int32_t,这是完全
在WinAPI上,HANDLE类型定义为void *,因此在64位应用程序上,HANDLE值的范围可以为0到18446744073709551615。
但实际上是真的吗?任何文件是否指定了这样一个手柄的整体范围?

如果一个人希望将这个HANDLE存储在一个32位应用程序上的一个int32_t,这是完全正常的,但是在64位应用程序上,这个疑问就是这样。

MSDN声明:

64-bit versions of Windows use 32-bit handles for interoperability.
When sharing a handle between 32-bit and 64-bit applications,only the
lower 32 bits are significant,so it is safe to truncate the handle
(when passing it from 64-bit to 32-bit) or sign-extend the handle
(when passing it from 32-bit to 64-bit). Handles that can be shared
include handles to user objects such as windows (HWND),handles to GDI
objects such as pens and brushes (HBRUSH and HPEN),and handles to
named objects such as mutexes,semaphores,and file handles.

也值得注意的是在该页面上添加了此评论:

The proper way to share such handles across process boundaries is by
zero-extending 32 bits handles to 64 bits,or vice versa by truncating
64 bits handles to 32 bits discarding the top bits.

请注意“句号扩展”句柄与“零延伸”句柄之间的区别。

编辑:从对这个问题的删除答案中的讨论来看,我认为扩展一个32位句柄来达到64位句柄而不是零扩展的意义在于保留对INVALID_HANDLE_VALUE的适当处理一个句柄的值。

https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读