Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??

Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestion
6 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jude Deng
    wrote on last edited by
    #1

    Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??? The background of application :I want to monitor the I/O of a special file by hooking WriteFile and the file be opened by 'fopen'.In addition, I can`t modify the function form 'fopen' to 'CreateFile'. Therefore I can`t know which handle of file should be monitored according to the first parameter of WriteFile.So I hope there is a way that can convert HANDLE or FILE* to another. If you know it ,please tell me . Thank you.

    I 1 Reply Last reply
    0
    • J Jude Deng

      Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ??? The background of application :I want to monitor the I/O of a special file by hooking WriteFile and the file be opened by 'fopen'.In addition, I can`t modify the function form 'fopen' to 'CreateFile'. Therefore I can`t know which handle of file should be monitored according to the first parameter of WriteFile.So I hope there is a way that can convert HANDLE or FILE* to another. If you know it ,please tell me . Thank you.

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      I made a line

      FILE *p = fopen ("foo", "rw");

      and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between stream opened by _tsopen and HANDLE. This stream is the _file member of FILE. _get_osfhandle http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^] The below code omits any error checking whatsoever....

      HANDLE FileToHandle (FILE *f)
      {
      return (HANDLE)(f->_file);
      }

      There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc. Enjoy! Iain.

      J 2 Replies Last reply
      0
      • I Iain Clarke Warrior Programmer

        I made a line

        FILE *p = fopen ("foo", "rw");

        and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between stream opened by _tsopen and HANDLE. This stream is the _file member of FILE. _get_osfhandle http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^] The below code omits any error checking whatsoever....

        HANDLE FileToHandle (FILE *f)
        {
        return (HANDLE)(f->_file);
        }

        There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc. Enjoy! Iain.

        J Offline
        J Offline
        Jude Deng
        wrote on last edited by
        #3

        Dear Iain, Thank you. Your ideal is right. But there is a mistake in your demo code. Perhaps, you lose a function call. I believe your original thought is following: HANDLE FileToHandle (FILE *f) { return (HANDLE)(**_get_osfhandle**(f->_file)); } I have tried the method and verified it is right. Enjoy! June

        1 Reply Last reply
        0
        • I Iain Clarke Warrior Programmer

          I made a line

          FILE *p = fopen ("foo", "rw");

          and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between stream opened by _tsopen and HANDLE. This stream is the _file member of FILE. _get_osfhandle http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^] The below code omits any error checking whatsoever....

          HANDLE FileToHandle (FILE *f)
          {
          return (HANDLE)(f->_file);
          }

          There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc. Enjoy! Iain.

          J Offline
          J Offline
          Jude Deng
          wrote on last edited by
          #4

          Dear Iain, Now I encounter another problem that is how to get file`s name by HANDLE of a file. Do you know it??? If you know it, please help me. Thank you. Enjoy! June.

          I 1 Reply Last reply
          0
          • J Jude Deng

            Dear Iain, Now I encounter another problem that is how to get file`s name by HANDLE of a file. Do you know it??? If you know it, please help me. Thank you. Enjoy! June.

            I Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            Thanks for using the code I *should* have written to get the HANDLE. I suspect the filename will be stored in FILE * in a similar fashion. If you do the same thing I did, and step into the fopen code, you can find out the information too. Good luck, Iain.

            Iain Clarke appearing by Special Request of CPallini.

            J 1 Reply Last reply
            0
            • I Iain Clarke Warrior Programmer

              Thanks for using the code I *should* have written to get the HANDLE. I suspect the filename will be stored in FILE * in a similar fashion. If you do the same thing I did, and step into the fopen code, you can find out the information too. Good luck, Iain.

              Iain Clarke appearing by Special Request of CPallini.

              J Offline
              J Offline
              Jude Deng
              wrote on last edited by
              #6

              Thank your help. Best wish for you. June

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups