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.开发者_开发技巧
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?
- HWND (CWnd and CWnd-derived classes)
- HDC (CDC and CDC-derived classes)
- HMENU (CMenu)
- HPEN (CGdiObject)
- HBRUSH (CGdiObject)
- HFONT (CGdiObject)
- HBITMAP (CGdiObject)
- HPALETTE (CGdiObject)
- HRGN (CGdiObject)
- HIMAGELIST (CImageList)
- SOCKET (CSocket) (Should have been HSOCKET?)
- and others.
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
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.
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.
精彩评论