-2

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;       
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • 1
    I just gave the `/proc` as an example, I just wanted to know if Windows has some other to get the file name of a process, maybe through a library interface or something... – Hello World Feb 19 '20 at 15:51
  • Also if someone ever gets a chance to write an answer about ways to improve SO...could anyone put foward a suggestion to add a guideline to not upvote/downvote answers without having any background information of what's going on...I'm not taunting or mocking anyone...But I have been for long on SO, (this is one of my secondary accounts) and I have seen some users who vote for Q/A without any prior knowledge and don't return to update their vote if the answer is edited. Maybe there could be a 1-day notifier by which voters could listen to edits on the answer they have voted for. – Hello World Feb 19 '20 at 15:57
  • Also that answer only has links and no C reference to a predefined library function or something.. – Hello World Feb 19 '20 at 16:00
  • This will also prevent users for getting extra points for no reason. (-2+10!=-2) – Hello World Feb 19 '20 at 16:08
  • 1
    From a file handle (non CRT) yes... but a pointer to random memory? No. – Mgetz Feb 19 '20 at 16:28
  • "Is it possible in Windows?" --> It is not an OS issue, but a compiler one. – chux - Reinstate Monica Feb 19 '20 at 17:00

1 Answers1

1

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.

Will Eccles
  • 394
  • 1
  • 5
  • 15
  • 1
    This answer is correct if the OP is using a `HANDLE` however the request is impossible if they are using a random pointer to memory [(technically you can get the `HANDLE` from a crt file)](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019). – Mgetz Feb 19 '20 at 16:32
  • 1
    @Mgetz I answered with the only possible thing I could find, given that from a pointer to "perhaps a memory location" it is almost impossible to do anything with the data unless you know beforehand what the data being pointed to is. – Will Eccles Feb 19 '20 at 16:38
  • 1
    @WillEccles yep, aware just wanted to call that out and point out you can get the `HANDLE` from a CRT `FILE` – Mgetz Feb 19 '20 at 16:42
  • How does the OS do it? Is there any way to do it using it? – Hello World Feb 19 '20 at 16:45
  • 1
    @HelloWorld what do you mean? This is the win32 API for getting the name of a file from a `HANDLE` to it, which you can get from a `FILE`. This is the equivalent to using /proc on *nix, but the "Windows way." – Will Eccles Feb 19 '20 at 16:49
  • 1
    @WillEccles the OP updated their question looks like they can get the `HANDLE` via [`_fileno`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fileno?view=vs-2019) and the method I linked above – Mgetz Feb 19 '20 at 17:25