A few challenges related to the Windows system

Windows specific forum
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

A few challenges related to the Windows system

Post 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
The truth is never confined to a single number - especially scientific truth!
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A few challenges related to the Windows system

Post by Shardik »

IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: A few challenges related to the Windows system

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post 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)...
The truth is never confined to a single number - especially scientific truth!
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A few challenges related to the Windows system

Post 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 ?
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post 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?
The truth is never confined to a single number - especially scientific truth!
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: A few challenges related to the Windows system

Post 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
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post 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.
The truth is never confined to a single number - especially scientific truth!
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post 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?
The truth is never confined to a single number - especially scientific truth!
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post by merendo »

Anyone, please?
The truth is never confined to a single number - especially scientific truth!
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: A few challenges related to the Windows system

Post 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
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post 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.
The truth is never confined to a single number - especially scientific truth!
void
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2011 9:50 pm
Location: Washington, USA

Re: A few challenges related to the Windows system

Post 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.
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Re: A few challenges related to the Windows system

Post 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.
The truth is never confined to a single number - especially scientific truth!
void
Enthusiast
Enthusiast
Posts: 116
Joined: Sat Aug 27, 2011 9:50 pm
Location: Washington, USA

Re: A few challenges related to the Windows system

Post by void »

Well, that's not a subtle approach, but if it meets your needs, you do what you have to. Right? :)
Post Reply