Results 1 to 4 of 4

Thread: Windows Events

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    308

    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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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:
    1. 'Author: Aaron Young
    2. 'Origin: Vb-World.net forums
    3. 'Purpose: Monitor a Directory
    4. 'Version: VB5+
    5.  
    6.  
    7. 'You can use the FindFirstChangeNotification() and
    8. 'FindNextChangeNotification() API's to monitor a directory
    9. 'for file changes including creation of new files, i.e.
    10.  
    11.  
    12. Private Declare Function WaitForSingleObject _
    13. Lib "kernel32" (ByVal hHandle As Long, ByVal _
    14. dwMilliseconds As Long) As Long
    15.  
    16. Private Declare Function FindFirstChangeNotification _
    17. Lib "kernel32" Alias "FindFirstChangeNotificationA" _
    18. (ByVal lpPathName As String, ByVal bWatchSubtree As _
    19. Long, ByVal dwNotifyFilter As Long) As Long
    20.  
    21. Private Declare Function FindNextChangeNotification _
    22. Lib "kernel32" (ByVal hChangeHandle As Long) As Long
    23.  
    24. Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
    25. Private Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
    26. Private Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
    27.  
    28. Private Sub WaitForFileActivity(ByVal sDirectory As String)
    29. Dim lHandle As Long
    30. Dim lReturn As Long
    31.  
    32. lHandle = FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_LAST_WRITE) 'if something's written to a file
    33. 'or FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_CREATION) 'if a file is being created
    34. 'or FindFirstChangeNotification(sDirectory, 0&, FILE_NOTIFY_CHANGE_FILE_NAME)
    35. Do
    36. lReturn = WaitForSingleObject(lHandle, 1)
    37. Call FindNextChangeNotification(lHandle)
    38. DoEvents
    39. Loop While lReturn <> 0
    40. End Sub
    41.  
    42. Private Sub Command1_Click()
    43. WaitForFileActivity "C:\"
    44. MsgBox "Change", vbSystemModal
    45. End Sub

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Windows Events

    Quote 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
  •  



Click Here to Expand Forum to Full Width