手柄有什么用?内部指针
我一直在阅读 IntPtr 并了解到它用于表示句柄。这究竟意味着什么?我确信这是一个简单的解释,但灯泡现在没有打开。
I have been reading up on IntPtr and have read that it is used to represent a Handle(s). What does this mean exactly? I'm sure it is a simple explanation, but the light bulb is just not turning on at the moment..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这通常指的是操作系统句柄,并在内部使用。例如,Windows 窗体使用
IntPtr
来引用控件的本机窗口句柄 (HWND)。Windows API 中的句柄有很多用途 - 大多数操作系统相关资源(文件、套接字、窗口等)都是通过句柄公开的,句柄实际上是一个指针。在托管代码中,它存储在
IntPtr
中。话虽如此,
IntPtr
也经常用于在互操作场景中存储指针,因为它会根据 32 位或 64 位代码自动调整大小。This is typically referring to an operating system Handle, and used internally. For example, Windows Forms uses an
IntPtr
to refer to the Control's native Window Handle (HWND).Handles in the Windows API are used for many things - most operating system related resources (files, sockets, windows, etc) are all exposed through a handle, which is effectively a pointer. In managed code, this gets stored in an
IntPtr
.That being said,
IntPtr
is also regularly used to store pointers in interop scenarios, as well, as it automatically resizes based on 32bit or 64bit code.“句柄”是“不透明指针”。它是操作系统提供给应用程序以表示内部对象的值(通常只是一个数组索引的数字),而不是提供指向实际对象的指针。这既是出于安全原因,也是出于抽象原因——它强制应用程序仅通过提供的 API 使用句柄。
A "handle" is an "opaque pointer". It's a value (usually just a number that's an index into an array) that the operating system gives to an application to represent an internal object, instead of giving it a pointer to the actual object. This is both for safety and abstraction reasons -- it forces the application to use the handle only through the provided APIs.