|
-
Feb 2nd, 2005, 11:02 AM
#1
Thread Starter
Hyperactive Member
Windows Events
Hey Guys,
This is a rather bizarre question but is there an api/callback/something that I can hook to to listen for any changes in a directory ? I.e. When a file is added tothe directory ???
I dont really want to constantly poll a directory for changes and wondered if there's a hook/callback that colud event me when smoething happens ??
Suppose not but any ideas ??
Cheers
Chubby.
-
Feb 2nd, 2005, 11:16 AM
#2
Re: Windows Events
I download this from vbforums 3 or 4 years ago, and used it once in a project, and it worked just fine for what I needed. I hope it helps.
VB Code:
'Author: Aaron Young
'Origin: Vb-World.net forums
'Purpose: Monitor a Directory
'Version: VB5+
'You can use the FindFirstChangeNotification() and
'FindNextChangeNotification() API's to monitor a directory
'for file changes including creation of new files, i.e.
Private Declare Function WaitForSingleObject _
Lib "kernel32" (ByVal hHandle As Long, ByVal _
dwMilliseconds As Long) As Long
Private Declare Function FindFirstChangeNotification _
Lib "kernel32" Alias "FindFirstChangeNotificationA" _
(ByVal lpPathName As String, ByVal bWatchSubtree As _
Long, ByVal dwNotifyFilter As Long) As Long
Private Declare Function FindNextChangeNotification _
Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
Private Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
Private Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
Private Sub WaitForFileActivity(ByVal sDirectory As String)
Dim lHandle As Long
Dim lReturn As Long
lHandle = FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_LAST_WRITE) 'if something's written to a file
'or FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_CREATION) 'if a file is being created
'or FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_FILE_NAME)
Do
lReturn = WaitForSingleObject(lHandle, 1)
Call FindNextChangeNotification(lHandle)
DoEvents
Loop While lReturn <> 0
End Sub
Private Sub Command1_Click()
WaitForFileActivity "C:\"
MsgBox "Change", vbSystemModal
End Sub
-
Feb 2nd, 2005, 12:18 PM
#3
Re: Windows Events
Hack, what is the purpose of DoEvents in that code?
Doesn't WaitForSingleObject hang until something happens?
If it doesn't, and that code loops, then you MUST handle the DoEvents in your code, otherwise it will screw your application up.
Code:
'in module/form
Private mblnCancel As Boolean
'in sub
Do
lReturn = WaitForSingleObject(lHandle, 1)
Call FindNextChangeNotification(lHandle)
DoEvents
Loop While lReturn <> 0 Or Not mblnCancel
Then when you close your app down you need to set mblnCancel=true
Make sense?
Woka
-
Feb 2nd, 2005, 12:31 PM
#4
Re: Windows Events
 Originally Posted by Wokawidget
Hack, what is the purpose of DoEvents in that code?
Doesn't WaitForSingleObject hang until something happens?
If it doesn't, and that code loops, then you MUST handle the DoEvents in your code, otherwise it will screw your application up.
Code:
'in module/form
Private mblnCancel As Boolean
'in sub
Do
lReturn = WaitForSingleObject(lHandle, 1)
Call FindNextChangeNotification(lHandle)
DoEvents
Loop While lReturn <> 0 Or Not mblnCancel
Then when you close your app down you need to set mblnCancel=true
Make sense?
Woka
Now that you mention it, I remember running into this issue when I first tried to use it. As I said, it was quite a while ago and my memory not so good no more, but what you posted and said does make sense. I didn't pop that out of a project (I don't have access to the source code I wrote for it any more). I just dug that code out of my Code Library.
Chubby: It will take a little tweaking, but if you decide to go with what I posted, and make the changes Woka posted, you should be good to go.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|