64 位应用程序上的 Windows HANDLE 的范围是多少? [英] What is the range of a Windows HANDLE on a 64 bits application?

查看:38
本文介绍了64 位应用程序上的 Windows HANDLE 的范围是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WinAPI 上,HANDLE 类型被定义为 void*,因此在 64 位应用程序中,HANDLE 值的范围可以从 018446744073709551615.但在实践中真的如此吗?是否有任何文档指定了这种 HANDLE 的整数范围?

On WinAPI, the HANDLE type is defined as a void*, thus on a 64 bit application the HANDLE value may range from 0 to 18446744073709551615. But is that true in practice? Does any documentation specify the integral range of such a HANDLE?

例如,如果有人想将此 HANDLE 作为 int32_t 存储在 32 位应用程序上完全没问题,但在 64 位应用程序上怀疑仍然存在.

If for instance one wants to store this HANDLE as an int32_t on a 32 bit application that's completely fine, but on a 64 bit application the doubts sticks.

推荐答案

MSDN 声明:

32 之间的进程间通信-位和 64 位应用程序

64 位版本的 Windows 使用 32 位句柄来实现互操作性.在 32 位和 64 位应用程序之间共享句柄时,只有低 32 位很重要,因此截断句柄是安全的(将其从 64 位传递到 32 位时)或对句柄进行符号扩展(当它从 32 位传递到 64 位时).可以共享的句柄包括用户对象的句柄,如窗口 (HWND),GDI 句柄诸如钢笔和画笔(HBRUSH 和 HPEN)之类的对象,以及命名对象,例如互斥体、信号量和文件句柄.

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.

还值得注意的是在该页面上添加的这条评论:

It's also worth noting this comment added on that page:

跨进程边界共享此类句柄的正确方法是通过通过截断将 32 位句柄零扩展为 64 位,反之亦然64 位处理到 32 位丢弃最高位.

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.

注意符号扩展"句柄与零扩展"句柄之间的区别.

Note the distinction between "sign-extending" a handle versus "zero-extending" a handle.

从这个问题的已删除答案中看到的讨论来看,我认为对 32 位句柄进行符号扩展以获得 64 位句柄而不是零的重要性-扩展它是为了保留对句柄的 INVALID_HANDLE_VALUE 值的正确处理.

Judging from discussion seen in a deleted answer to this question, I suppose that the significance of sign-extending a 32-bit handle to arrive at a 64-bit handle instead of zero-extending it is to retain proper treatment of the INVALID_HANDLE_VALUE value for a handle.

这篇关于64 位应用程序上的 Windows HANDLE 的范围是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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