Question

I am about to implement a new feature for our software of locking our documents while the user has them open (previously we just opened the file for reading, or for writing, but did not hold the handle open for the duration).

It is trivial to keep the handle, and open it in write-exclusive mode.

My question is how to query windows to discover if a given file cannot be opened with write-access, what user/entity has already locked the file for write access?

e.g.:

_fsopen(m_ClipboardFilename, "wb", _SH_DENYWR);

Or

CStdioFile listFile(strLabelfile, CFile::typeText | CFile::modeRead | CFile::shareDenyWrite | CFile::modeNoInherit);

Either of these opens the given file with exclusive write access (or fails with an error indicating whether there was a sharing violation or some other problem).

But what I want to do is to discover "who" has the file locked if it is a sharing violation, so I can report to the current user that

Sam, on PC-007, has this file locked, you can only open it in read-only mode. Do you wish to proceed?

There are utilities I've seen which will tell you who has a file locked - and more that will force locks to be unlocked, so that a file can be manipulated (sys-admin type utils). So it seems that there must be a way to query for this information.

Was it helpful?

Solution

Raymond Chen recently explained how to do this using the Restart Manager.

This will tell you which process has the file open, but I don't know if it will tell you anything very useful if the file is an a different server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top