no he said he wants to check if a file is open ,
by other thread or process or even the same thread
you need to learn to read dude!!!!!!!!!!!
no he said he wants to check if a file is open ,
by other thread or process or even the same thread
you need to learn to read dude!!!!!!!!!!!
notepad is a process you nubcake , if he wants to know if a txt file is open then this is the way !!!!!!!!
i will write the code out for you later on seeing as i am a nice guy
Last edited by Anddos; 04-03-2008 at 08:18 AM.
Notepad is one process. A file is a file and can be held open by one or more processes, not limited to notepad.
This is NOT the way.
Opening the file in exclusive mode is an easy way to know if another process is already holding the file open or not.
You need to learn to be less rude.
he said a txt file , so the default editor to open a .txt is notepad ,
the method you said means to open the file not check if the txt file is running !!! jesus christ
I will repeat the original message:
In other words: how can I figure out if another process (or another thread) is holding the file open?
Answer: open in exclusive mode. This will fail unless the file is not opened by any other process.
If it succeeds, then the file is not open by any other process.
And even though notepad may be the default, it doesn't mean it's the only process that can hold a file open! George did mention "another thread," too, which could mean another thread in the same process perhaps? That's not notepad.
Anddos, chill out and stop attacking Elysia. There is no reason to inflame an otherwise calm thread.
Windows, AFAIK, has no command that will tell you which file handles belong to which threads. There are several undocumented NT_ functions within the kernel code that you can call but since they are undocumented and also subject to change I don't know if I would rely on these. I researched this one time to write a utility that would allow me to view which process had a file open and if need be attempt to gain ownership of that handle and close the file. This would eliminate the infamous 'Access denied. ..' error that really only helps viruses and spyware continue to run unmolested on your system.
There is a website that keeps a database of undocumented Win32 API functions that you can google for and that may assist you. I know it is possible to do this since there are utils out there that can tell you this information.
ifstream will only tell you about the process in which it is used. For system wide information about which file handles are being used and by whom you would have to make a call into the Windows kernel. There is no way the C/C++ standard libraries will know anything about other tasks or processes in the system that may have files open.
Last edited by VirtualAce; 04-03-2008 at 07:10 PM.
Thanks Bubba,
Two more comments,
1.
Do you mean there are some methods from ifstream class, which can tell which handle is used by which process? I have performed some research, but failed.
Could you indicate which method from ifstream do you mean from which we can know which process opens the file please? :-)
2.
You mean there is public API in ifstream which can tell which process opens the file, not there is no open API to tell which thread opens the file?
Thanks Elysia,
You correctly understand my points. To be honest, my English is not very good, but you can understand all of my points. Appreciated. :-)
(Or, it means you have the same level of English skills -- just joking.)
regards,
George
No, that's not right. There is no method in ifstream to tell you that. There are undocumented Win32 API that does, however.
Have a look at http://undocumented.ntinternals.net/
Just beware of using these functions. They are usually undocumented for a reason.
I don't think using ifstream is the right approach. Windows would be the only one who could really tell you if the file was opened by a process. Just because a file is opened in one process does not mean that it is in exclusive mode. This is actually relying on a side effect of what Windows may do to get ifstream to report the information you want. In essence you are using ifstream to report something that it is not responsible for.