Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: is this file open?

by cdarke (Prior)
on Dec 19, 2011 at 09:46 UTC ( #944223=note: print w/replies, xml ) Need Help??

in reply to is this file open?

How can I test whether the spreadsheet is already "being used by another program",br>
That's actually quite difficult on Windows (although a breeze on Linux).

Using the Win32 API, given the right privileges, it is possible to get a copy of an open file handle in another process using DuplicateHandle(). However, the only documented API which is available is GetMappedFileName() - which means we must first map the file to our process address space. That does not work with a file of zero length! So if a process has, for example, just opened a file for write access then it might be zero length, and buffering will extend the period when it appears to be empty. With all this going on, there is the possibility of a race condition.
The SysInternals toolset includes handle.exe to give this information, albeit in an external process, and its output is sent to STDOUT (so run it in a pipe).
handle.exe uses the Windows Object Manager, which uses undocumented APIs. It iterates through kernel's object namespace.
On Windows 7, make sure you "Run as administrator".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://944223]
[choroba]: talking about golfing, I did Pascal's triangle in 51 characters ;-)
[choroba]: and 99 bottles of beer in 217
[Discipulus]: yes I know, bro' and I also know I cannot compete. But no one ask for oneliners.. ;=)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-10-22 20:16 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (275 votes). Check out past polls.