windows HANDLE 类似于 Linux 中的文件描述符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7964907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:22:52  来源:igfitidea点击:

Is HANDLE similar to file descriptor in Linux?

c++windowsfile-descriptor

提问by Amumu

Is HANDLE similar to file descriptor in Linux? As far as I know, HANDLE is used for handling every resources on Windows, such as font, icons, files, devices..., which in essence is just a void pointer point to a memory block holding data of a specific resource

HANDLE 类似于 Linux 中的文件描述符吗?据我所知,HANDLE 用于处理 Windows 上的所有资源,例如字体、图标、文件、设备...,本质上只是一个指向保存特定资源数据的内存块的空指针

回答by Mark Seaborn

Yes, Windows handles are very similar to Unix file descriptors (FDs).

是的,Windows 句柄与 Unix 文件描述符 (FD) 非常相似。

Note that a HANDLEis not a pointer to a block of memory. Although HANDLEis typedef'd as void *, that's just to make it more opaque. In practice, a HANDLEis an index that is looked up in a table, just as an FD number is.

请注意, aHANDLE不是指向内存块的指针。虽然HANDLEtypedef'd as void *,那只是为了让它更不透明。实际上,aHANDLE是在表中查找的索引,就像 FD 编号一样。

This blog post explores some of the similarities and differences: http://lackingrhoticity.blogspot.com/2015/05/passing-fds-handles-between-processes.html

这篇博文探讨了一些异同:http: //lackingrhotity.blogspot.com/2015/05/passing-fds-handles-between-processes.html

回答by zvrba

Yes, they are conceptually similar. File descriptors in unix map integers to a per-process table of pointers to other objects (which can be other things than files, too). File descriptors are not as unified though -- some things exist in a separate "namespace" (e.g., process timers). In that respect, Windows is more orthogonal -- CloseHandle will always free a resource regardless of what it is.

是的,它们在概念上是相似的。unix 中的文件描述符将整数映射到指向其他对象(也可以是文件以外的其他事物)的每个进程的指针表。但是文件描述符并不统一——有些东西存在于单独的“命名空间”中(例如,进程计时器)。在这方面,Windows 更加正交——无论资源是什么,CloseHandle 总是会释放它。

回答by John Z. Li

Besides the fact that handles refer to a far broader concept on Windows. Even we restrict the discussion to only file handles, there is significant differences. There is a function called _open_osfhandle() as part of C run-time library on Windows. Its purpose is to, quote "Associates a C run-time file descriptor with an existing operating-system file handle." That is, a glue function between the kernel land and the C Run-time land. The function signature is as below:

此外,句柄在 Windows 上是指更广泛的概念。即使我们将讨论仅限于文件句柄,也存在显着差异。有一个名为 _open_osfhandle() 的函数作为 Windows 上 C 运行时库的一部分。其目的是引用“将 C 运行时文件描述符与现有操作系统文件句柄相关联”。也就是说,内核域和 C 运行时域之间的粘合函数。函数签名如下:

int _open_osfhandle (
    intptr_t osfhandle,
    int flags
);

File handles Windows is actually more feature rich than file descriptors in C, which can be configured when a file handle is created with CreateFileA (ANSI version) or CreateFile (UTF16 version), reflecting the design difference between *Nix and Windows. And the resulted handle carries all these information around with all its implications.

文件句柄 Windows 实际上比 C 中的文件描述符功能更丰富,可以在使用 CreateFileA(ANSI 版本)或 CreateFile(UTF16 版本)创建文件句柄时进行配置,体现了 *Nix 和 Windows 的设计差异。结果句柄携带所有这些信息及其所有含义。

回答by Steven Penny

A HANDLEis a void pointer

AHANDLE是空指针

typedef PVOID HANDLE;
typedef void *PVOID;

Windows Data Types

Windows 数据类型