简体   繁体   English

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

[英]Is HANDLE similar to file descriptor in Linux?

Is HANDLE similar to file descriptor in Linux? HANDLE与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用于处理Windows上的每个资源,例如字体,图标,文件,设备......,它实质上只是指向保存特定资源数据的内存块的void指针

Yes, Windows handles are very similar to Unix file descriptors (FDs). 是的,Windows句柄与Unix文件描述符(FD)非常相似。

Note that a HANDLE is not a pointer to a block of memory. 请注意, HANDLE不是指向内存块的指针。 Although HANDLE is typedef 'd as void * , that's just to make it more opaque. 虽然HANDLEtypedef as void * ,但这只是为了使它更加不透明。 In practice, a HANDLE is an index that is looked up in a table, just as an FD number is. 在实践中, HANDLE是在表中查找的索引,就像FD编号一样。

This blog post explores some of the similarities and differences: http://lackingrhoticity.blogspot.com/2015/05/passing-fds-handles-between-processes.html 这篇博客文章探讨了一些相似之处和不同之处: http//lackingrhoticity.blogspot.com/2015/05/passing-fds-handles-between-processes.html

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). unix中的文件描述符将整数映射到指向其他对象的指针的每个进程表(也可以是除文件之外的其他东西)。 File descriptors are not as unified though -- some things exist in a separate "namespace" (eg, process timers). 文件描述符并不统一 - 有些东西存在于单独的“命名空间”中(例如,进程计时器)。 In that respect, Windows is more orthogonal -- CloseHandle will always free a resource regardless of what it is. 在这方面,Windows更正交 - CloseHandle将始终释放资源,无论它是什么。

Besides the fact that handles refer to a far broader concept on Windows. 除了句柄在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. 在Windows上有一个名为_open_osfhandle()的函数作为C运行时库的一部分。 Its purpose is to, quote "Associates a C run-time file descriptor with an existing operating-system file handle." 它的目的是引用“将C运行时文件描述符与现有的操作系​​统文件句柄相关联”。 That is, a glue function between the kernel land and the C Run-time land. 也就是说,核心陆地和C运行时间之间的粘合功能。 The function signature is as below: 功能签名如下:

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. 文件句柄Windows实际上比C中的文件描述符更丰富,可以在使用CreateFileA(ANSI版本)或CreateFile(UTF16版本)创建文件句柄时进行配置,这反映了* Nix和Windows之间的设计差异。 And the resulted handle carries all these information around with all its implications. 结果句柄包含所有这些信息及其所有含义。

A HANDLE is a void pointer HANDLE是一个无效指针

typedef PVOID HANDLE;
typedef void *PVOID;

Windows Data Types Windows数据类型

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

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