Page 1 of 2

A few challenges related to the Windows system

Posted: Wed Jan 18, 2012 12:26 pm
by merendo
Hello everyone.

I have two programming challenges which I can't seem to cope with all on my own, so I'd appreciate your help with them. I did some googleing to no avail.

Challenge one: I'd like my programme to present a question to the user when he or she chooses to shut down his or her computer down (or log off). I know there is a way to do that, I just haven't figured it out yet :) I realise I can't prevent Windows from eventually terminating my programme whenever Windows wants to shut down, but maybe I can delay it a few seconds during which the user can answer my question.

Challenge two: Is there a way to detect whenever someone opens a file somewhere on the system? All I'd need to know is the name and location of the file, that's all. So my programme would listen for file opens all the time, and be notified when any file is opened. Is there a way to do that?

Thanks a lot!
merendo

Re: A few challenges related to the Windows system

Posted: Wed Jan 18, 2012 12:51 pm
by Shardik

Re: A few challenges related to the Windows system

Posted: Wed Jan 18, 2012 1:07 pm
by IdeasVacuum
C1: There is an MS way, you can get windows to run a script on shutdown, but it seems there are different ways to trigger it, depending on Windows version.

The general idea
http://technet.microsoft.com/en-us/libr ... 70300.aspx
http://technet.microsoft.com/en-us/maga ... 30947.aspx

Edit: Ricardo's method is really what you want, a concise solution in your app.

Re: A few challenges related to the Windows system

Posted: Wed Jan 18, 2012 1:32 pm
by merendo
Challenge one is completed. Thanks.

As for challenge two, thanks for the codes you've posted, but I don't just need to monitor for actual changes in files, but I also need to know when a file has been accessed by any process, ie when it has been opened, with just read access. Any way to do that?

Oh, and I guess there is a third challenge: Is there any way I can selectively prohibit read access to files? I'd like to lock down a file for read access (although at this point, one read and write access will already be in progress)...

Re: A few challenges related to the Windows system

Posted: Wed Jan 18, 2012 2:02 pm
by Shardik
merendo wrote:As for challenge two, thanks for the codes you've posted, but I don't just need to monitor for actual changes in files, but I also need to know when a file has been accessed by any process, ie when it has been opened, with just read access. Any way to do that?
Have you already tried to change the NotifyFilter in jpfiste's example from
#FILE_NOTIFY_CHANGE_FILE_NAME
to
#FILE_NOTIFY_CHANGE_LAST_ACCESS ?

Re: A few challenges related to the Windows system

Posted: Mon Jan 23, 2012 9:32 am
by merendo
I just tried that, doesn't throw me a notification for access events. I did change the event handler in the WatchDirOrFile-procedure (Select-Case-Section), to handle all events, but I still receive nothing.

Any ideas what I'm missing here?

Re: A few challenges related to the Windows system

Posted: Mon Jan 23, 2012 2:53 pm
by ar-s
i think a simple way to do that should be to "catch" the list of recent document at a specific date the to catch it again when you return to the computer.
%APPDATA% + Microsoft\Windows\Recent

Re: A few challenges related to the Windows system

Posted: Mon Jan 30, 2012 8:26 am
by merendo
The thing is that this file access will in virtually all cases not happen locally - users will access files from remote client machines.

Re: A few challenges related to the Windows system

Posted: Mon Jan 30, 2012 10:24 am
by merendo
Okay, I've just tried modifying the codes posted above - with no success. It would appear that the notification FILE_NOTIFY_CHANGE_LAST_ACCESS is what I want, but none of the codes ever throws me that event when I expect it to (that is, when I simply perform read access to a file). But I suppose that I am expecting the wrong thing here, as the 'last accessed' timestamp in any file doesn't change either, whenever I read-open a file, so I suppose that is an error in reasoning on my part.

The MSDN documentation on ReadDirectoryChangesW states for the FILE_NOTIFY_CHANGE_LAST_ACCESS event says: 'Any change to the last access time of files in the watched directory or subtree causes a change notification wait operation to return.' - but since the 'last access' time of a file remains unchanged even though the file has been accessed, this can't work.

Can anyone point me to a different solution? I need to monitor a filesystem specifically for file read access events.


EDIT: Alternative solution: Is it at all feasible to write a fileserver myself? That is, act as if my programme were a regular windows file server, but I'd be in full control over file access attempts and I can even prohibit read access to specific files. Anyone had any experience in this area?

Re: A few challenges related to the Windows system

Posted: Tue Jan 31, 2012 3:38 pm
by merendo
Anyone, please?

Re: A few challenges related to the Windows system

Posted: Tue Jan 31, 2012 4:26 pm
by Shardik
merendo wrote:The MSDN documentation on ReadDirectoryChangesW states for the FILE_NOTIFY_CHANGE_LAST_ACCESS event says: 'Any change to the last access time of files in the watched directory or subtree causes a change notification wait operation to return.' - but since the 'last access' time of a file remains unchanged even though the file has been accessed, this can't work.
Sorry, but I recently discovered that using FILE_NOTIFY_CHANGE_LAST_ACCESS
is unfortunately no way to go. I've got it to work but you might have to wait up
to an hour before the access of a file is signalled: :oops:
MSDN wrote:The NTFS file system delays updates to the last access time for a file by up to 1 hour after the last access.
http://msdn.microsoft.com/en-us/library/ms724290

Re: A few challenges related to the Windows system

Posted: Tue Jan 31, 2012 5:06 pm
by merendo
Crap. That renders this solution entirely useless. Guess I'm back to square one then, and writing my own file server is the only option left.

Thank you all very much for your help anyway!

Oh, while I'm burning the midnight oil writing this server, if anyone has an idea how to solve my problem still, please step forward with it.

EDIT: Okay, stupid idea. I abandoned it and now I feel like banging my head to the wall for even considering writing my own CIFS server.

Re: A few challenges related to the Windows system

Posted: Sat Apr 21, 2012 11:38 pm
by void
Slightly belated response.

Random research on the 'current open files' problem suggests it is very hard. I was looking through some things for a related project, and I came across this Visual C posting which looks... decidedly non-trivial, and possibly not reliable.

The short answer is, there doesn't appear to be a single portable way to do this on all currently used versions of windows, and you're going to have to play with the plumbing of Windows' internals.

Re: A few challenges related to the Windows system

Posted: Mon Apr 30, 2012 1:27 pm
by merendo
Well, thank you for your response. Actually, I like to hear that this cannot be done easily, if you know what I mean. I now use a solution similar to FlexLink, with which all open-file requests are first processed by my own application.

Re: A few challenges related to the Windows system

Posted: Wed May 02, 2012 12:53 am
by void
Well, that's not a subtle approach, but if it meets your needs, you do what you have to. Right? :)