C语言 如何从 fopen FILE 结构中获取文件 HANDLE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3989545/
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
How do I get the file HANDLE from the fopen FILE structure?
提问by lornova
The fopenfunction returns a pointer to a FILEstructure, which should be considered an opaque value, without dealing with its content or meaning.
该fopen函数返回一个指向FILE结构的指针,该结构应被视为不透明值,而不处理其内容或含义。
On Windows, the C runtime is a wrapper of the Windows API, and the fopenfunction relies on the CreateFilefunction. The CreateFilefunction returns a HANDLE, which is used by other Windows API.
在 Windows 上,C 运行时是 Windows API 的包装器,fopen函数依赖于该CreateFile函数。该CreateFile函数返回一个HANDLE,它被其他 Windows API 使用。
Now, I need to use Windows API deep inside of a library that uses fopenand FILE*. So: is there a way to get the HANDLEfrom the FILEstructure? As this is compiler specific, I mean on the MSVC runtime library.
现在,我需要在使用fopen和FILE*. 那么:有没有办法HANDLE从FILE结构中获取?由于这是特定于编译器的,我的意思是在 MSVC 运行时库上。
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.
我知道这将是一个丑陋的、不可移植的 hack,如果 Microsoft 更改FILE...将是困难和耗时的。
采纳答案by Steve Townsend
Use _filenofollowed by _get_osfhandle. Don't forget to _closeit when you are done.
使用_fileno后跟_get_osfhandle. 完成后不要忘记_close它。
EDIT: it's not clear to me that _get_osfhandleis supported on WinCE. However the docs for WinCE _filenosay it returns a "file handle" rather than "descriptor". YMMV but this suggests that you can maybe just use _filenoreturn value directly as a handle on WinCE.
编辑:我不清楚_get_osfhandleWinCE 是否支持。然而,WinCE 的文档_fileno说它返回一个“文件句柄”而不是“描述符”。YMMV 但这表明您可以_fileno直接使用返回值作为 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:
“如果您查看我在 1 月 29 日发布到列表中的头文件,您会看到我是如何处理文件创建/处理问题的。我不必用 HANDLE 替换所有 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
It turns out that _fileno returns a handle. You just have to cast it."
事实证明 _fileno 返回一个句柄。你只需要投射它。”
回答by Didier Trosset
On Linux, there's the int fileno(FILE *);function that returns the file descriptor (the one that was returned by the low-level openfunction) from the FILE*.
在 Linux 上,有int fileno(FILE *);一个open函数从FILE*.
I don't know if it applies to Windows and returns the HANDLE though?
我不知道它是否适用于 Windows 并返回 HANDLE?
回答by Steven Penny
For C, try this
对于 C,试试这个
HANDLE foo = (HANDLE)_get_osfhandle(fileno(fopen("bar.txt", "w")));

