0

for my current program it is neccessary to detect the path of the currently active file, e.g. my program shall run in the background (no problem), and when you are entering a special short cut, it has to look what file is in the foreground (a pdf/a doc/whatever), and find out the path of this file.
Is this possible, and if yes, how? I am using C++ on a Windows machine.
Thank you very much!

ed_me
  • 3,338
  • 2
  • 24
  • 34
arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • Normally you'd keep file path of currently open file in a variable and know from that. Its part of the state of your program. – Tony The Lion Aug 22 '13 at 10:36
  • 2
    Can you define background and foreground in this context? Do you mean screen-wise or process-wise? – enhzflep Aug 22 '13 at 10:36
  • Can you describe a bit more about what you are ACTUALLY trying to achieve? So, if someone has "my_work.doc" open in Word, what do you want to do with it? Why does the path matter? – Mats Petersson Aug 22 '13 at 10:44
  • My idea is: Writing an app for moving files from one device to another as shown in the movie "Avatar", e.g. moving it away from one device (in this case, my pc) to my tablet just by "taking" it with my hands. Thus, on my pc the program running in the background needs to know which file is active and in the foreground, so that this file can be synced via GDocs (or is already) and can be accessed on my tablet. So this is the reason why my program needs to know which file is active. – arc_lupus Aug 22 '13 at 11:35
  • So, if I'm running SvnDiff to compare two files, what should the result be? – MSalters Aug 23 '13 at 09:08
  • @MSalters: What do you mean with that? – arc_lupus Aug 23 '13 at 11:12
  • The "diff" program calculates and shows the difference between two files. Which of the two files is the "currently active" file? – MSalters Aug 23 '13 at 12:37
  • Also, I now realize that it probably wouldn't work anyway. In almost all programs, when you're editing the file, the state on disk is not instantaneously updated. Even if you could open a handle, it may be to stale data on disk. You would need to force the foreground application to save its data. – MSalters Aug 23 '13 at 12:42

3 Answers3

0

Given a process ID

If you know the PID (process ID) of the file, you can use windows API, as you mention you're on windows, to enumerate through the process list using EnumProcesses and then use OpenProcess to return a handle which you can use GetModuleFileNameEx on.

The links there should provide enough information to use the API. The basic principle would be to identify the process given it's handle or process ID, then use the windows API to acquire the information you want.

Given a file handle

If you have a handle to the file, you can take a look at see GetFinalPathNameByHandle on Vista or later. Although the link also provides information for using file mapping for previous versions.

As others have said, if you are opening the file yourself, you may want to just use state to maintain the location that was used initially.

ed_me
  • 3,338
  • 2
  • 24
  • 34
  • This will work for .exe and .dll's. If someone opens a file "my_work.doc", I don't believe there is an official API to find the name of that file - although there clearly is a way to do that, as ProcessExplorer can do that - but it doesn't explain how it works anywhere that I know of. – Mats Petersson Aug 22 '13 at 10:43
  • I would assume there's a file handle for, 'my_work.doc', a quick search on MSDN shows for vista or later there is GetFinalPathNameByHandle, I'll edit – ed_me Aug 22 '13 at 10:52
  • @MatsPetersson: ProcExp will show the command line, IIRC. But if I open Notepad, and use "Save As...", you would probably want the new file. Notepad doesn't keep a file handle open, so that is not a solution either. And if this doesn't even work with Notepad, how reliable is this then going to be? – MSalters Aug 23 '13 at 09:10
  • If you go into the detail of a process, you will see all the files that the program has open. – Mats Petersson Aug 23 '13 at 09:58
  • @MatsPetersson: True, but you won't see all files that a program has opened before, nor the files it will open in the future. And in the case of Notepad, that is what you would need. – MSalters Aug 23 '13 at 12:39
0

On Linux I'd use lsof - list open files and grep for those, which are interesting. On Windows batch mode of the process monitor or file monitor (from SysInternals)

I think this is a pretty similar problem: How can I determine whether a specific file is open in Windows?

(Probably that's why you've earned -1.)

Community
  • 1
  • 1
Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • This is not a similar problem, because then you only know if a specific file has been opened, but you do not know which file is the currently active file. – arc_lupus Aug 23 '13 at 08:59
0

Found the solution to my problem: I can use the WinAPI-Function "FindWindow" and "GetWindowText".

arc_lupus
  • 3,942
  • 5
  • 45
  • 81