“把柄”到底是什么?

发布于 2024-09-17 11:34:11 字数 92 浏览 3 评论 0原文

我经常听说“手柄”,它到底是什么?

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

等。这些东西是一样的吗?或者它们是一些抽象术语?

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

Edit:
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场春暖 2024-09-24 11:34:11

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

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

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

额外的间接层允许操作系统或库执行一些操作,例如移动对象、对对象进行引用计数以及通常控制对象发生的情况。与 PIMPL 习惯用法一样,实现可能会完全改变,同时仍然保留原始 API,因此不会强制客户端重新编译。如果您尝试为用 C 等过程语言编写的客户端提供非面向对象的 API,那么它特别有用。

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.

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.

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.

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.

寂寞清仓 2024-09-24 11:34:11

“句柄”是对资源的引用的另一个名称,它由程序员显式管理,而不是由运行时自动管理。

A "handle" is another name for a reference to a resource which is managed by the programmer explicitly instead of automatically by the runtime.

意中人 2024-09-24 11:34:11

句柄是指向非托管资源的指针,如文件句柄、数据库连接句柄、窗口句柄等。由于它们是非托管资源的句柄,在大多数情况下它们不会被自动垃圾收集,您需要确保正确释放它们,否则您可能听说过手柄泄漏

Handles are pointers to unmanaged resources like file handles, database connection handles, windows handles, etc... As they are handles to unmanaged resources in most cases they won't be automatically garbage collected and you need to ensure to properly release them or you might hear about leaking handles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文