Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need to check if certain files have been opened externally to my application i.e by clicking on the file in windows explorer, and then the file opens in an application like notepad. But I can't to find a way to do this.

I have tried CFile::Open() with modeWrite, and CFile::Write(), and these do not fail if the file has been opened elsewhere.

Is there another way?
Posted
Updated 18-Nov-11 20:38pm
v2
Comments
Timberbird 18-Nov-11 4:52am    
In which way are they opened? The file may be opened by an application exclusively, when write access to that file is blocked, or it may be just loaded in memory. For example, when you open a file with notepad, the latter loads file content into memory and then closes the file, so it can be viewed and edited by other programs.
I know this is quite obvious, just in case
Sergey Alexandrovich Kryukov 18-Nov-11 14:16pm    
And also, a file which seems to be open may be not actually open. This is even more trivial that what you point out, but it needs to be checked up. Please see my solution.
--SA

1 solution

Your method is basically correct and should work, but it is destructive, you may want to use read instead or check stream size. The problem is that the file could be opened for non-exclusive access, which is not very typical but your method of detection won't detect it.

You can try this: http://read.pudn.com/downloads36/sourcecode/windows/111597/NtSystemInfo/NtSystemInfoTest/NtSystemInfoTest.cpp__.htm[^].

Also, it may happen that you think the file is opened but actually it is not. Please check it up. For example, many text editor do not keep a file handle open. They open a file, read it all into the application's memory and close immediately. File is read and not opened. You can check up what really happens using Sysinternals Process Explorer: see Sysinternals Suite: http://technet.microsoft.com/en-us/sysinternals/bb842062[^].

—SA
 
Share this answer
 
Comments
Jackie Lloyd 19-Nov-11 2:36am    
Thankyou very much for your help, but I am still quite confused. Why is what I am doing 'destructive'? What does'please check it up mean?'. The first link has loads of code but I'm not actually sure which bit does the checking if the file is open - does it just look for processes like when you bring up the task-manager? And lastly, the last link - is that something I can actually use within my code - it looked like a tool to use externaly? Sorry for so many questions.
Sergey Alexandrovich Kryukov 10-Jan-12 1:44am    
Happy New Year!
"Destructive" means that you are trying to write file in order to merely get information. This way, you interfere with operation of other process. Imagine that you modify the file by writing, but this file is to be used in next moment of time by another process. You could disrupt it.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900