“句柄"到底是什么? [英] What exactly is "handle"?

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

问题描述

我经常听说过把手",那到底是什么?

I've often heard about "handles", what exactly are those?

例如,我听说过: 窗户把手 事件句柄 文件句柄

For instance I have heard about: windows handles event handles file handles

,依此类推.这些东西都一样吗?还是它们是一些抽象术语?

and so on. Are those things the same? Or they are some abstract terms?

推荐答案

句柄是引用操作系统或库拥有的对象的间接方法.当操作系统或库拥有一个对象但想要让客户端引用它时,它可以提供对该对象的称为句柄的引用.

A handle is an indirect way to reference an object owned by the OS or a library. When the operating system or a library owns an object but wants to let a client refer to it, it can provide a reference to that object called a handle.

句柄可以以不同的方式实现.通常,它们不是C ++或C#的引用.通常,它们是转换为某种不透明类型的指针,或者它们可能是(或包含)操作系统或库所拥有的对象表的索引.

Handles can be implemented in different ways. Typically they are not references in the C++ or C# sense. Often they are pointers cast to some opaque type, or they might be (or contain) an index into a table of objects that are owned by the operating system or library.

例如,在Windows中,如果您创建窗口,则OS会创建一个代表该窗口的对象,但不会返回指向该对象的指针.而是返回一个窗口句柄,该句柄提供了额外的间接层.当您在另一个OS调用中将窗口句柄传递回时,OS会根据该句柄知道要使用哪个窗口对象.这样可以防止您的代码直接访问窗口对象.

For example, in Windows, if you create a window, the OS creates an object that represents the window, but it doesn't return a pointer to that object. Instead, it returns a window handle, which provides an extra layer of indirection. When you pass the window handle back in another OS call, the OS knows which window object to use based on the handle. This prevents your code from directly accessing the window object.

额外的间接层允许OS或库执行诸如移动对象,对对象进行引用计数以及通常控制对象发生什么等操作.像PIMPL习惯用法一样,实现可能会完全更改,同时仍保留原始API,因此不会强迫客户端重新编译.如果您尝试为使用C之类的过程语言编写的客户端提供非面向对象的API,则该功能特别有用.

The extra layer of indirection allows the OS or library to do things like move objects around, reference count the objects, and generally control what happens to the object. Like the PIMPL idiom, the implementation may change completely while still preserving the original API and thus not forcing clients to recompile. It's especially useful if you're trying to offer a non-object-oriented API for clients written in procedural languages like C.

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

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