In Windows XP with the C language: checking if a file is open

In my C written application under Windows-XP: how can I check if a file is open by another application? One option is to rename the file and check its rename. Another option is to open the file to add. But these options are very laborious. Is there any other, less time-consuming solution to the problem?

+4
source share
2 answers

Open the file in exclusive mode .

HANDLE file = CreateFile(_T("MyFile"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); if (file != INVALID_HANDLE_VALUE) { // file is not used by anyone else CloseHandle(file); } 
+2
source

There is no function in the Windows API that checks if a function is open in another application. If it existed, it would be the subject of a race.

Suppose you first noted whether the file was already open and the response returned that it was not open at the moment. Then you go to open it, but in the meantime someone else has it. Then your attempt to open fails.

So the only way to find out if you can open the file is to try to do it. If the file was opened in such a way as to prevent an attempt to open it, then this attempt will fail.

+3
source

All Articles