什么是“句柄”?
我看到对窗口句柄的引用 、引用窗口句柄的控件等。但我不知道句柄是什么。
我特别想知道它是什么,以及任何相关的细节,因为它与 .NET 有关。
Possible Duplicates:
What is a Windows Handle?
What exactly is “handle” ?
I see references to window handles, controls which have a reference to a window handle, etc. But I don't know what a handle is.
I especially would like to know what it is, and any relevant details, as it pertains to .NET.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
句柄是操作系统的参考。它没有编程引用的语义,但它的作用是让系统资源在 API 调用中传递它时知道您所引用的内容。
通常,HANDLE 被包装在类的实例中。 CWnd 是一个很好的例子,它包含一个 HWND,它是窗口的句柄。
你可以这样做。 CWnd *pWnd = CWnd::FromHandle(hWnd) 注意:CWnd::FromHandle(hWnd) 是静态的,不需要实例。
它会将 hWnd 所包装的包装器传回给您。好吧,不完全是!如果句柄实际上并未被对象包裹,它将创建一个句柄,并且它只是暂时的。因此,使用它一次,然后将其扔掉。它可以创建实例,因为 hWnd 在其结构中有足够的信息供 Windows 实例化 CWnd 对象。它不会将其添加到句柄v对象表中,因此它只是临时的。
HWND 实际上是一个内核对象,还有更多吗?
我不确定如果需要的话所有这些是否都会传回临时对象。
GetDC(hWnd) 将从 hWnd 获取 hDC,但这将是临时的,可能更好使用 CDC 类。
了解更多:http://wiki.answers.com/Q/What_is_a_handle_in_Windows_Programming#ixzz1JBmoF0lv
A handle is a reference for the operating system. It does not have the semantics of a programming reference but what it does do is allow the system resources to know what you are referring to when it is passed in an API call.
Usually, the HANDLE is wrapped in an instance of a class. CWnd is a good example, it contains an HWND which is a handle to a window.
You can do this. CWnd *pWnd = CWnd::FromHandle(hWnd) Note: that CWnd::FromHandle(hWnd) is static and does not require an instance.
It will pass you back the wrapper that the hWnd is wrapped by. Well not quite! If the handle is not actually wrapped by an object it will create one AND IT WILL ONLY BE TEMPORARY.So use it once then throw it away. It can create the instance because the hWnd has enough information in its struct for windows to instantiate a CWnd object. It does not add it to the handle v object table, so it is only temporary.
The HWND is in fact a kernel object and there's more?
I am not sure if all of these would pass back a temporary object if required.
GetDC(hWnd) will get you a hDC from a hWnd but it will be temporary, probably better to use the CDC class.
Read more: http://wiki.answers.com/Q/What_is_a_handle_in_Windows_Programming#ixzz1JBmoF0lv
句柄是对另一方(通常是操作系统)提供给您的某些资源的抽象引用,您可以将其交还给另一方以引用该资源。
操作系统通常包含一个句柄表,其中包含用户创建的实体(打开的文件、创建的信号量、进程、线程……);句柄(通常实现为)该表的整数索引。您的进程执行打开操作,操作系统在其句柄表中创建一个条目,用您的(进程)名称标记它,并将该条目的索引返回给您的进程。当您的进程想要进行读取时,它会向操作系统提供句柄整数,操作系统只需将其用作表索引即可在表中查找它;它现在知道您的进程想要读取哪个实体(文件)。
通过将进程 ID 放入句柄条目中,操作系统可以判断该句柄对于该进程是否有效。您的进程可以提供垃圾作为操作系统的句柄;如果句柄槽匹配,操作系统就会做你想做的事,不管它有多么愚蠢。毕竟,它是您的资源。
A handle is an abstract reference to some resource provided to you by another party (usually the OS), that you can hand back to the other party to reference that resource.
OSes often contain a handle table containing entities that users have created (open files, created semaphores, processes, threads, ...); the handle is (often implemented as) an integer index into this table. Your process does an open, the OS creates an entry in its handle table, marks it with your (process) name, and hands the index of that entry back to your process. When your process wants to do a read, it provides the handle integer to the OS, which looks it up in the table by simply using it as table index; it now knows which entity (file) your process wants to read from.
By putting your process id in the handle entry, the OS can tell if the handle is valid for the process. Your process can provide trash as a handle to the OS; if the handle slot matches, the OS will do what you want, regardless of how stupid it is. After all, its your resource.
句柄是唯一标识操作系统对象的东西,无论是套接字、同步原语等(在 Unix 中它们通常称为描述符)。从技术上讲,它要么是全局对象表中的偏移量,要么是指向包含对象信息的记录的指针。但您需要将此句柄视为不透明的数字。
.NET 使用对象的引用,因此在 .NET 中,当涉及到与操作系统的互操作时,您会遇到句柄。
Handle is something that uniquely identifies OS object, be it a socket, synchronization primitive etc (in Unix they are usually called descriptors). Technically it's either an offset in global object table or a pointer to record that contains object information. But you need to treat this handle as an opaque number.
.NET uses references to objects, so in .NET you come across handles when it comes to interoperation with OS.