HANDLE与Linux中的文件描述符类似吗?据我所知,HANDLE用于处理Windows上的每个资源,例如字体,图标,文件,设备......,这实际上只是指向保存特定资源数据的内存块的void指针
答案 0 :(得分:13)
Yes, Windows handles are very similar to Unix file descriptors (FDs).
Note that a HANDLE
is not a pointer to a block of memory. Although HANDLE
is typedef
'd as void *
, that's just to make it more opaque. In practice, a HANDLE
is an index that is looked up in a table, just as an FD number is.
This blog post explores some of the similarities and differences: http://lackingrhoticity.blogspot.com/2015/05/passing-fds-handles-between-processes.html
答案 1 :(得分:4)
是的,它们在概念上是相似的。 unix中的文件描述符将整数映射到指向其他对象的指针的每个进程表(也可以是除文件之外的其他东西)。文件描述符并不统一 - 有些东西存在于单独的“命名空间”中(例如,进程计时器)。在这方面,Windows更正交 - 不管它是什么,CloseHandle总是释放资源。
答案 2 :(得分:0)
除了句柄指的是Windows上更广泛的概念。即使我们将讨论限制为仅文件句柄,也存在重大差异。 Windows上的C运行时库有一个名为_open_osfhandle()的函数。其目的是引用“将C运行时文件描述符与现有的操作系统文件句柄相关联”。也就是说,内核代码域和C运行时代码域之间的粘合功能。功能签名如下:
int _open_osfhandle (
intptr_t osfhandle,
int flags
);
文件句柄实际上,Windows比C中的文件描述符功能更丰富,当使用CreateFileA(ANSI版本)或CreateFile(UTF16版本)创建文件句柄时,可以配置Windows,这反映了* Nix和Windows之间的设计差异。最终的句柄会携带所有这些信息及其所有含义。
答案 3 :(得分:-1)