简体   繁体   English

打开/关闭手柄时会发生什么?

[英]What happen when you open/close a handle?

I'm introduced to C++. 我被介绍给C ++。 I'm confused about the idea of "handle" Here's a little snippet I wrote today: 我对“处理”的想法感到困惑这是我今天写的一个小片段:

    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, a valid pid);
    printf("%d", hProcess);

I find out that the output is "48" every time. 我发现每次输出都是“48”。 It make sense, because handles are identifiers of resources like array indices. 这是有道理的,因为句柄是数组索引等资源的标识符。 But even I replace PROCESS_ALL_ACCESS with other flags, the return value is still the same. 但即使我用其他标志替换PROCESS_ALL_ACCESS,返回值仍然相同。 What exactly happened when you open a handle? 打开手柄时到底发生了什么? How does the Operating System know the access right of a handle? 操作系统如何知道句柄的访问权限? If handles are identifiers of resources, why doesn't the following code work? 如果句柄是资源的标识符,为什么以下代码不起作用?

    HANDLE hProces = 48;

Furthermore, what happened when you call CloseHandle(hProcess)? 此外,当您调用CloseHandle(hProcess)时发生了什么? why is the output still 48? 为什么输出仍然是48?

    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, a valid pid);
    CloseHandle(hProcess);
    printf("%d\n", hProcess);

hProcess is just a value associated a process. hProcess只是一个进程相关的值。 The handle itsself is managed and associated with the process by the Operating System. 句柄本身由操作系统管理并与进程相关联。

What exactly happened when you open a handle? 打开手柄时到底发生了什么?

OpenProcess creates an entry in some data structure of the OS, does the initialization stuff, and associates the handle ( 48 ) with the entry. OpenProcess在OS的某些数据结构中创建一个条目,执行初始化操作,并将句柄( 48 )与条目相关联。

How does the Operating System know the access right of a handle of a specific handle? 操作系统如何知道特定句柄句柄的访问权限?

It stores them in a seperate data structure, which the handle refers to (eg, if it's a table, the handle is an index into that table). 它将它们存储在句柄引用的单独数据结构中(例如,如果它是表,则句柄是该表的索引)。 The handle's value is independent of the access rights associated to it. 句柄的值独立于与其关联的访问权限。

If handles are identifiers of resources, why doesn't the following code work? 如果句柄是资源的标识符,为什么以下代码不起作用?

 HANDLE hProces = 48; 

Because the OS hasn't added the corresponding entry yet, so the handle 48 isn't associated with anything. 因为OS还没有添加相应的条目,所以句柄48不与任何东西相关联。

As an example, if you did 举个例子,如果你这样做了

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, a valid pid);
HANDLE alias = hProcess;

alias would refer to the same process as hProcess does. alias将引用与hProcess相同的过程。

why is the output still 48? 为什么输出仍然是48?

CloseHandle removes that entry from the data structure and disassociates the handle from the entry. CloseHandle从数据结构中删除该条目,并将句柄与条目解除关联。 1 1
Printing hProcess is well-defined. 印刷hProcess定义明确。 It is just a value, which is not augmented by CloseHandle . 它只是一个值,不会被CloseHandle扩充。 Only the associated entry is removed and with it the meaning of the handle ( 48 ) to the entry. 只删除相关的条目 ,并带有条目句柄( 48 )的含义


I think you can imagine the whole thing like this: there's a data structure, which contains data of your process, among it data for opened processes. 我认为你可以想象整个这样的事情:有一个数据结构,其中包含你的过程数据,其中包括打开过程的数据。 The handles are "pointers" to these process entries. 句柄是这些过程条目的“指针”。 If a process entry exist, the handle points to some well-known process. 如果存在进程条目,则句柄指向一些众所周知的进程。 However, if it does not, the handle does not point to a process. 然而,如果没有,把手并不指向一个过程。 Just like with pointers: 就像指针一样:

char* ptr;
{
    char c = 'a'; /* similar to the call to OpenProcess itsself */
    ptr = &c;    /* similar to hProcess initialized with the return value
                  * of OpenProcess */
}    /* c goes out of scope, similar to CloseHandle */

1 As @IInspectable said in the comments to this answer, that's actually not quite true. 1正如@IInspectable在对这个答案的评论中所说,这实际上并不完全正确。 The OS maintains a counter for the handle, which counts the number of entities referencing the associated process. 操作系统维护句柄的计数器,该计数器计算引用相关进程的实体数。 OpenProcess and CloseHandle only increment/decrement that counter, respectively to make multiple "possessors" of the handle possible. OpenProcessCloseHandle只增加/减少该计数器,分别使句柄的多个“拥有者”成为可能。

We could guess that the local variable hProcess is not changed by the CloseHandle function. 我们可以猜测, CloseHandle函数不会改变局部变量hProcess You have to check the return value of CloseHandle in order to know if this function worked or not, but you can't rely on the hProcess value. 您必须检查CloseHandle的返回值,以便知道此函数是否有效,但您不能依赖于hProcess值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM