It is currently Tue May 21, 2013 10:21 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: A few challenges related to the Windows system
PostPosted: Wed Jan 18, 2012 12:26 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Wed Jan 18, 2012 12:51 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 814
Location: Germany
Have you already done a forum search or a search on http://www.purearea.net
and taken a look into these examples? :wink:

1) Shutdown detection:
dige: http://www.purearea.net/pb/CodeArchiv/W ... hutDown.pb
ricardo: viewtopic.php?f=12&t=45779

2) Detect file changes in folder:
dige: http://www.purebasic.fr/german/viewtopic.php?t=16567
Sparkie: viewtopic.php?t=14776&start=17
Dare2: viewtopic.php?t=12483
jpfiste: viewtopic.php?f=12&t=47060


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Wed Jan 18, 2012 1:07 pm 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2853
Location: Wales, UK
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/library/cc770300.aspx
http://technet.microsoft.com/en-us/magazine/dd630947.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.


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Wed Jan 18, 2012 1:32 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Wed Jan 18, 2012 2:02 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 814
Location: Germany
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 ?


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Mon Jan 23, 2012 9:32 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Mon Jan 23, 2012 2:53 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Oct 06, 2007 11:20 pm
Posts: 133
Location: France
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


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Mon Jan 30, 2012 8:26 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Mon Jan 30, 2012 10:24 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Tue Jan 31, 2012 3:38 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
Anyone, please?

_________________
The truth is never confined to a single number - especially scientific truth!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Tue Jan 31, 2012 4:26 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 814
Location: Germany
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


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Tue Jan 31, 2012 5:06 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Sat Apr 21, 2012 11:38 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Aug 27, 2011 9:50 pm
Posts: 107
Location: Washington, USA
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.


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Mon Apr 30, 2012 1:27 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 7:24 pm
Posts: 458
Location: Germany
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!


Top
 Profile  
 
 Post subject: Re: A few challenges related to the Windows system
PostPosted: Wed May 02, 2012 12:53 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Aug 27, 2011 9:50 pm
Posts: 107
Location: Washington, USA
Well, that's not a subtle approach, but if it meets your needs, you do what you have to. Right? :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye