Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the file HANDLE from the fopen FILE structure?

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.

like image 749
lornova Avatar asked Oct 21 '10 16:10

lornova


People also ask

What is the return value of fopen when file mode is W?

Opens the file for reading only. If the file cannot be opened fopen() returns NULL. “w” – Searches file. If the file exists already, its contents are overwritten. If the file doesn't exist, a new file is created.

When using the fopen () function to open a file in PHP What made should you use for appending data to a file?

One can append some specific data into a specific file just by using a+ or a mode in the fopen() function. The PHP Append to file is done just by using the fwrite() function of PHP Programming Language.

What is fopen () function?

The fopen() function opens the file specified by filename and associates a stream with it. The mode variable is a character string specifying the type of access requested for the file. The mode variable contains one positional parameter followed by optional keyword parameters.


3 Answers

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."

like image 140
Steve Townsend Avatar answered Sep 24 '22 14:09

Steve Townsend


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?

like image 22
Didier Trosset Avatar answered Sep 21 '22 14:09

Didier Trosset


For C, try this

HANDLE foo = (HANDLE)_get_osfhandle(fileno(fopen("bar.txt", "w")));
like image 32
Zombo Avatar answered Sep 24 '22 14:09

Zombo