Sign in to follow this  
Followers 0
Terenz

ReadDirectoryChangesW Exit!

2 posts in this topic

#1 ·  Posted (edited)

Hello guys,

For work i need to monitor file creation and file renaming, from which name to which name, into the entire HDD of an user. Searching i have found the example of ReadDirectoryChangesW and i have little modify it:

#include <APIFilesConstants.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIDiag.au3>
#include <WinAPIFiles.au3>
#include <WinAPISys.au3>
#NoTrayIcon
Opt("TrayMenuMode", 3) ; No Flag

Global $g_sPath = @HomePath
Global $aData
Global $OldName
Global $pBuffer

Local $hDirectory = _WinAPI_CreateFileEx($g_sPath, $OPEN_EXISTING, $FILE_LIST_DIRECTORY, BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE), $FILE_FLAG_BACKUP_SEMANTICS)
If @error Then
    _WinAPI_ShowLastError('', 1)
EndIf

$pBuffer = _WinAPI_CreateBuffer(8388608)

Local $iExit = TrayCreateItem("Exit")

TraySetState(1)
TraySetClick(8)

While 1
    Switch TrayGetMsg()
        Case $iExit ; Exit the loop.
            Exit
    EndSwitch
;~  $aData = _WinAPI_ReadDirectoryChanges($hDirectory, BitOR($FILE_NOTIFY_CHANGE_FILE_NAME, $FILE_NOTIFY_CHANGE_DIR_NAME), $pBuffer, 8388608, 1)
;~  If Not @error Then
;~      If IsArray($aData) Then
;~          For $e = 1 To $aData[0][0]
;~              If $aData[$e][1] = 1 Then
;~                  ConsoleWrite($g_sPath & "\" & $aData[$e][0] & " has been created" & @CRLF)
;~              ElseIf $aData[$e][1] = 4 Then
;~                  ;ConsoleWrite ($g_sPath & "\" & $aData[$e][0] & " has been renamed" & @CRLF)
;~                  $OldName = $aData[$e][0]
;~              ElseIf $aData[$e][1] = 5 Then
;~                  ConsoleWrite($g_sPath & "\" & $OldName & " has been renamed to " & $g_sPath & "\" & $aData[$e][0] & @CRLF)
;~              EndIf
;~          Next
;~      EndIf
;~  Else
;~      _WinAPI_ShowLastError('', 1)
;~  EndIf
WEnd

But main problem is i can't exit normally from the script! With HotkeySet or _IsPressed require a LOT of time, with tray icon i don't see the menu. I have try with the lastest stable also if i need for 3.3.8.1 for lagacy reason with XP. What is the problem here? The same if i selet a empty folder or the root of the HDD.

Many thanks :D

EDIT: I have see this example based on ReadDirectoryChangesW, it work also on Windows 7 x64:

CDirectoryChangeWatcher - ReadDirectoryChangesW all wrapped up

I have tested the executable, It have may feature like two folder etc. i don't need all that stuff and it exit immediately also if i monitor the entire disk. dunno what is the problem with the autoit one.

EDIT2: Reading online i have see the guys using AHK have the same problem but they have resolved in this way, author of the post is SKAN:

Quote

The problem with synchronous mode in AHK is that AHK is single threaded and ReadDirectoryChangesW can halt/freeze every other operation when it is waiting for an event in the watched folder. Halt means: you cannot start an another thread via hotkey /settimer .. the GUI ( if any ) would not respond for any messages ( including mouse ).. not even a right click on tray icon would work!

To overcome the said effect, the script calls ReadDirectoryChangesW in a seperate thread using CreateThread() and RegisterCallback().
When called in synchronous mode, ReadDirectoryChangesW sets the variable nReadLen ( with number of bytes ) when it exits the wait state.
Therefore, the script calls ReadDirectoryChangesW in a seperate thread and loops to check the variable nReadLen to ascertain whether the wait state has ended.

An interesting fact is that ReadDirectoryChangesW can spy without enough access rights to the folder.

Irrespective of mode, ReadDirectoryChangesW keeps updating the structure and it is upto the user to decide on how frequently to call the ReadDirectoryChangesW. The user may even call it once per hour but the structure size should be large enough to hold all the folder events that may occur during the period, else it will reset and start from the beginning of the Structure. This script uses a 64KB structure.

I don't know how to use CreateThread and RegisterCallback for this, need an matter experts

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0