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

查看:135
本文介绍了什么是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有意义的 only .

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天全站免登陆