什么是 Windows 句柄? [英] What is a Windows Handle?

查看:32
本文介绍了什么是 Windows 句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论 Windows 中的资源时,什么是句柄"?它们是如何工作的?

What is a "Handle" when discussing resources in Windows? How do they work?

推荐答案

它是对资源的抽象引用值,通常是内存或打开的文件或管道.

It's an abstract reference value to a resource, often memory or an open file, or a pipe.

正确地,在 Windows 中(通常在计算中)句柄是一种抽象,它对 API 用户隐藏真实的内存地址,允许系统对程序透明地重新组织物理内存.将句柄解析为指针会锁定内存,释放句柄会使指针无效.在这种情况下,可以将其视为指向指针表的索引……您将索引用于系统 API 调用,系统可以随意更改表中的指针.

Properly, in Windows, (and generally in computing) a handle is an abstraction which hides a real memory address from the API user, allowing the system to reorganize physical memory transparently to the program. Resolving a handle into a pointer locks the memory, and releasing the handle invalidates the pointer. In this case think of it as an index into a table of pointers... you use the index for the system API calls, and the system can change the pointer in the table at will.

或者,当 API 编写者希望 API 的用户与返回的地址指向的具体内容隔离时,可以给出一个真实的指针作为句柄;在这种情况下,必须考虑句柄指向的内容可能随时更改(从 API 版本到版本,甚至从返回句柄的 API 的调用到调用) - 因此句柄应被视为简单的不透明值对 API 有意义.

Alternatively a real pointer may be given as the handle when the API writer intends that the user of the API be insulated from the specifics of what the address returned points to; in this case it must be considered that what the handle points to may change at any time (from API version to version or even from call to call of the API that returns the handle) - the handle should therefore be treated as simply an opaque value meaningful only to the API.

我应该补充一点,在任何现代操作系统中,即使是所谓的真实指针"仍然是进程虚拟内存空间的不透明句柄,这使操作系统能够管理和重新排列内存,而不会使指针失效过程中.

I should add that in any modern operating system, even the so-called "real pointers" are still opaque handles into the virtual memory space of the process, which enables the O/S to manage and rearrange memory without invalidating the pointers within the process.

这篇关于什么是 Windows 句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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