I am not a Windows expert, but I believe I know enough to answer your
question.
Windows has an extraordinary system of file locking, so that if one
process has a file open, even if it's just for reading, then it gets
locked so that no other process can open it, even if it's just for
reading. This seems screwy to me, but judging by the amount of software
available for Windows, I guess it works out somehow. I think this is why
you need to reboot a Windows machine even if all you've done is install
a new version of Free Cell - it's impossible to overwrite an open file.
Anyway, I think all you need to do is something like:
char *path = resolve_full_path("libtwink.dll"); /* use an appropriate
function here! */
FILE *fp = fopen(path, "rb"); /* note need for b on Windows */
if(fp == NULL && errno == EACCES) {
/* DLL is in use by another process */
That's both OT and untrue.
A Windows application has control of what sharing is allowed for open
files. By default files opened for read may be opened for read by
other processes, while files opened for write are exclusive (although
the application, at the OS API level, can change the default
behavior). This usually includes executables opened for execution as
well. Windows sometimes opens executables for the duration, and
sometimes not, based on several factors (for example, Windows will
usually copy executables from removable or network devices, and *not*
keep the source file open). But even when it's open, you typically
*can* open it for read and you obviously can open it if Windows hasn't
kept it open). So your code won't actually detect anything.
You can try to open it for write, and that *will* fail if the
executable is open (IOW, it's running and not on a network share or
similar), but that can obviously be problematic if you succeed. Using
the OS functions (or an MS CRT extension like _fsopen() ), you could
open a file read-only but exclusive, and that would detect if anyone
had the file open (again, that doesn't help in the network share
case). But even that carries a (small) risk in that it would leave
the file locked for a time, which might cause and ill timed
application start to fail.
And, of course, security brings its own set of issues.