Search code examples
cwindowsc89

Is it possible to get the file name of a pointer in Windows?


As far as I have researched on SO, in order to obtain the file name from a pointer(perhaps a memory location), we'll need to get it's File Descriptor No. which could then be used to indirectly request the OS for the file name. Maybe by navigating it's proc directory or there could be different ways OS-dependent ways to do it.

But the real question here is,

Is it possible in Windows?

I am looking for a way to do it specifically using C, since my code is written in C.

===

Just for the sake of reference,

typedef struct  {
        int             level;          /* fill/empty level of buffer */
        unsigned        flags;          /* File status flags          */
        char            fd;             /* File descriptor            */
        unsigned char   hold;           /* Ungetc char if no buffer   */
        int             bsize;          /* Buffer size                */
        unsigned char   *buffer;        /* Data transfer buffer       */
        unsigned char   *curp;          /* Current active pointer     */
        unsigned        istemp;         /* Temporary file indicator   */
        short           token;          /* Used for validity checking */
}       FILE;       

Solution

  • It appears that you can use the GetFileInformationByHandleEx function with the argument FileNameInfo. I am not on Windows and cannot test, but it's suggested by this answer as well.