Is HANDLE similar to file descriptor in Linux?

被刻印的时光 ゝ 提交于 2019-12-22 03:16:08

问题


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


回答1:


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




回答2:


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.




回答3:


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:

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.




回答4:


A HANDLE is a void pointer

typedef PVOID HANDLE;
typedef void *PVOID;

Windows Data Types



来源:https://stackoverflow.com/questions/7964907/is-handle-similar-to-file-descriptor-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!