如何从 fopen FILE 结构中获取文件句柄?

发布于 2024-09-28 19:02:48 字数 703 浏览 2 评论 0原文

fopen 函数返回一个指向FILE 结构,应将其视为不透明值,而不处理其内容或含义。

在 Windows 上,C 运行时是 Windows API 的包装器,fopen 函数依赖于 CreateFile 函数。 CreateFile 函数返回一个HANDLE,供其他 Windows API 使用。

现在,我需要在使用 fopenFILE* 的库内部深入使用 Windows API。那么:有没有办法从 FILE 结构中获取 HANDLE ?由于这是特定于编译器的,我指的是 MSVC 运行时库。

我知道这将是一个丑陋的、不可移植的黑客行为,如果微软改变FILE的内部格式,这可能会破坏......但我正在一个封闭的系统上开发(即在Windows上) CE嵌入式系统)和重构库将是困难且耗时的。

The fopen function returns a pointer to a FILE structure, which should be considered an opaque value, without dealing with its content or meaning.

On Windows, the C runtime is a wrapper of the Windows API, and the fopen function relies on the CreateFile function. The CreateFile function returns a HANDLE, which is used by other Windows API.

Now, I need to use Windows API deep inside of a library that uses fopen and FILE*. So: is there a way to get the HANDLE from the FILE structure? As this is compiler specific, I mean on the MSVC runtime library.

I understand that this would be an ugly, non-portable hack, and that could broke if Microsoft changes the internal format of FILE... but I'm developing on a closed system (i.e. on a Windows CE embedded system) and refactoring the library would be difficult and time consuming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-10-05 19:02:48

使用 _fileno 后跟 _get_osfhandle。完成后不要忘记_close它。

编辑:我不清楚 WinCE 是否支持 _get_osfhandle。然而,WinCE _fileno 的文档说它返回“文件句柄”而不是“描述符”。 YMMV 但这表明您可以直接使用 _fileno 返回值作为 WinCE 上的句柄。

编辑:#2该理论得到此人的经验。

“如果你看一下我 1 月 29 日发布到列表中的头文件
您可以看到我如何处理文件创建/处理问题。我没有
用句柄替换所有 FILE* 项。请参阅以下片段
fileio.cpp:

#ifndef q4_WCE

  FlushFileBuffers((HANDLE) _get_osfhandle(_fileno(_file)));
  HANDLE h = ::CreateFileMapping((HANDLE)
_get_osfhandle(_fileno(_file)),
                        0, PAGE_READONLY, 0, len, 0);
#else

  FlushFileBuffers((HANDLE) _fileno(_file));
  HANDLE h = ::CreateFileMapping((HANDLE) _fileno(_file),
                    0, PAGE_READONLY, 0, len, 0);
#endif //q4_WCE

事实证明_fileno返回一个句柄。你只需要施展它就可以了。”

Use _fileno followed by _get_osfhandle. Don't forget to _close it when you are done.

EDIT: it's not clear to me that _get_osfhandle is supported on WinCE. However the docs for WinCE _fileno say it returns a "file handle" rather than "descriptor". YMMV but this suggests that you can maybe just use _fileno return value directly as a handle on WinCE.

EDIT: #2 That theory is supported by this person's experience.

"If you take a look at the header files that I posted to the list on Jan 29
you can see how I handled the file creation/handle problem. I didn't have
to replace all FILE* items with HANDLEs. See the following snippet from
fileio.cpp:

#ifndef q4_WCE

  FlushFileBuffers((HANDLE) _get_osfhandle(_fileno(_file)));
  HANDLE h = ::CreateFileMapping((HANDLE)
_get_osfhandle(_fileno(_file)),
                        0, PAGE_READONLY, 0, len, 0);
#else

  FlushFileBuffers((HANDLE) _fileno(_file));
  HANDLE h = ::CreateFileMapping((HANDLE) _fileno(_file),
                    0, PAGE_READONLY, 0, len, 0);
#endif //q4_WCE

It turns out that _fileno returns a handle. You just have to cast it."

忆离笙 2024-10-05 19:02:48

在 Linux 上,有一个 int fileno(FILE *); 函数从 返回文件描述符(由低级 open 函数返回的文件描述符) >文件*。

我不知道它是否适用于 Windows 并返回句柄?

On Linux, there's the int fileno(FILE *); function that returns the file descriptor (the one that was returned by the low-level open function) from the FILE*.

I don't know if it applies to Windows and returns the HANDLE though?

維他命╮ 2024-10-05 19:02:48

对于C,试试这个

HANDLE foo = (HANDLE)_get_osfhandle(fileno(fopen("bar.txt", "w")));

For C, try this

HANDLE foo = (HANDLE)_get_osfhandle(fileno(fopen("bar.txt", "w")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文