[Win32utils-devel] Need some help with pure Ruby win32-changenotify

Heesob Park phasis at gmail.com
Sun Aug 5 05:36:45 EDT 2007


2007/8/4, Daniel Berger <djberg96 at gmail.com>:

> Hi all,
> I know it's deprecated, but people seem to still be using
> win32-changenotify, so I thought I'd see if I could make it pure Ruby.
> Also, I thought it would be a good opportunity to test passing a custom
> Win32::Event object.
> I'm mostly done I think, but I'm having trouble unraveling the
> FILE_NOTIFY_INFORMATION struct buffer. Please check out the latest
> lib/win32/changenotify.rb from CVS and take a look at both the custom
> wait method and the get_file_action private method.
> The notification is clearly getting picked up, but I'm just not
> unraveling the struct properly.
> Here's a little sample program you can use to test:
> require 'win32/changenotify'
> include Win32
> filter = ChangeNotify::FILE_NAME | ChangeNotify::DIR_NAME
> cn = ChangeNotify.new("C:\\", true, filter)
> cn.wait(15){ |x|
>    p x
> } while true
> Then, just make sure to modify some file within the 15 second wait period.
> Thanks,
> Dan

You may overlooked the API document that says the FileName field is a
variable-length field.

Here is working version of get_file_action:

     def get_file_action(fni)
         array  = []
         while true
            break if fni.nil? || fni[0,4].unpack('L')[0] == 0
            int_action = fni[4,4].unpack('L')[0]
            str_action = 'unknown'
            case int_action
               when FILE_ACTION_ADDED
                  str_action = 'added'
               when FILE_ACTION_REMOVED
                  str_action = 'removed'
               when FILE_ACTION_MODIFIED
                  str_action = 'modified'
               when FILE_ACTION_RENAMED_OLD_NAME
                  str_action = 'renamed old name'
               when FILE_ACTION_RENAMED_NEW_NAME
                  str_action = 'renamed new name'
            len = fni[8,4].unpack('L').first
            buf = fni[12,len]
            file = wide_to_multi(buf)
            struct = ChangeNotifyStruct.new(str_action, file)
            fni = fni[fni[0,4].unpack('L').first, -1] # Next offset



Park Heesob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20070805/c95798f4/attachment.html 

More information about the win32utils-devel mailing list