Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287220
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:28:07+00:00 2026-05-17T20:28:07+00:00

The fopen function returns a pointer to a FILE structure, which should be considered

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:28:07+00:00Added an answer on May 17, 2026 at 8:28 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have reduced the problem to the following basic function which should simply print
fopen is failing when I try to read in a very moderately sized file
The iPhone SDK docs claim fopen() is a supported method of file access but
It seems if I do something like $file = fopen($filepath, w); $CR = curl_init();
Is there a standard way to do an fopen with a Unicode string file
This is the code: unsigned int number; FILE* urandom = fopen(/dev/urandom, r); if (urandom)
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
Sometimes, when I open the a file like so: FILE *file = fopen(fname, wb);
Depending on command-line arguments, I'm setting a file pointer to point either towards a
I make several calls to a function that reads data from an input file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.