What is a handle? Why is there a handle? HANDLE

Source: Internet
Author: User

"Preface 1" has felt murderous, seems friendly, seems to follow the road of struggle to be more careful.

"Foreword 2" This article is highly praised, but feel the process ID and Hinstanse said is not very clear:

"Link" http://www.cppblog.com/mymsdn/archive/2009/02/19/handle-in-windows.html

In a broad sense, anything that can pick up a lot of data from a single value can be called a handle. The English of the handle is "Handle", the original meaning is "the handle", only in the computer science, is specially translates "the handle", actually still is a "handle". It's not like a "handle" to pick up a whole bunch of things from a small thing.

The pointer is then actually a "handle", simply because the pointer has a more specific meaning--actually corresponds to an address in the memory--so the pointer is usually not said to be a "handle." But pointers also have the function of referencing a lot of data from a 32-bit value, which is not a handle.

There are many kernel objects in the Windows system (the objects here are not exactly equivalent to "objects" in the word object-oriented programming), although they are essentially the same, such as open files, created threads, windows of programs, and so on. These important objects are certainly not 4 bytes or 8 bytes fully described, they have a large number of attributes. In order to save the state of such an "object", which often requires hundreds or even thousands of bytes of memory space, how to pass the data between the subroutine (function) within the program or program. Drag these hundreds of byte copies to copy it. is obviously wasteful of efficiency. So what to do. Of course, passing the first address of these objects is a way, but there are at least two drawbacks: exposing the kernel object itself, so that the program (rather than the operating system kernel) can also arbitrarily modify the internal state of the object (the first address is known, what can not be changed.) , which is clearly not allowed by the operating system kernel; the operating system has the responsibility to regularly defragment the memory, and if some of the memory is sorted out once, the object is removed.

As a result, the Windows operating system uses further indirection: in the process of the address space to set a table, the interior and exterior of the head to save some number and the corresponding address by this number, and by that address to refer to the actual object, this number and that address in the numerical value of no regular connection, is purely a mapping.

In a Windows system, this number is called a "handle".

Handle is very extensive in Windows, and the following about handle unless specifically stated, will be limited to the context of the process, the thread.

1, first to talk about handle

The handle itself is a 32-bit unsigned integer that is used to represent a kernel object. It does not point to the actual kernel object, and it is never possible for a program in user mode to obtain the actual address of a kernel object (in general). So what's the point of handle? It actually looks for the actual address of the corresponding kernel object in a table as an index. So where is this watch? Each process has such a table, called a handle table. The first item of the table is the process's own handle, which is why you call GetCurrentProcess () always return 0x7fffffff reason.

Simply put, handle is an integer value used to "indirectly" represent a kernel object. You can use handle in your program to represent the kernel objects you want to manipulate. The kernel objects here include events (event), threads, processes, mutexes, and so on. The most common of us is the file handle (handle).

It is also important to note that handle only makes sense in the process in which it belongs. It makes no sense to pass a process-owned handle to another process, and if you do, you need to use DuplicateHandle () to pass handle across multiple processes is another topic that has nothing to discuss here.

2, Process ID

First, the process ID is a 32-bit unsigned integer with one ID for each process, and the ID is unique within the system scope. The system uses this ID to uniquely determine a process.

In depth, the system may use the process ID to compute the base address of the kernel object representing the process (and the base address of the eprocess structure), and you can ask Microsoft's OS developers for a specific calculation formula.

3, HINSTANCE

HINSTANCE is also a 32 unsigned integer that represents the base address that the program loads into memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.