tags:

views:

9

answers:

0

Hello,

I'm using a small program that notify me when a file size change inside a directory. The program worked perfect under Win XP, now in Windows 7 the size changes are ignored for long periods and notifications are not in sync with the changes.

At first glance that could be seen as something related to caching/flushing differences between XP and Win 7 but I verified that the file I'm monitoring is being flushed to the disk and is not staying in cache.

I used the command tail -f monitoredfile.txt displaying the file content as it is updated and I was able to see all the changes in real time but my monitor program wasn't reporting any file size change.

This is the monitor program:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>

void RefreshDirectory(LPTSTR);
void RefreshTree(LPTSTR);
void WatchDirectory(LPTSTR);

int _tmain(int argc, TCHAR *argv[])
{
    if(argc != 2)
    {
        _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
        return 1;
    }

    WatchDirectory(argv[1]);
}

void WatchDirectory(LPTSTR lpDir)
{
   DWORD dwWaitStatus; 
   HANDLE dwChangeHandles[1]; 
   TCHAR lpDrive[4];
   TCHAR lpFile[_MAX_FNAME];
   TCHAR lpExt[_MAX_EXT];

   _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);

   lpDrive[2] = (TCHAR)'\\';
   lpDrive[3] = (TCHAR)'\0';

// Watch the directory for file size changes. 

   dwChangeHandles[0] = FindFirstChangeNotification( 
      lpDir,                         // directory to watch 
      FALSE,                         // do not watch subtree 
      FILE_NOTIFY_CHANGE_SIZE); // watch file size changes 


// Change notification is set. Now wait on notification handles

   while (TRUE) 
   {  printf("\nWaiting for notification...\n");

      dwWaitStatus = WaitForMultipleObjects(1, dwChangeHandles, FALSE, INFINITE); 

      switch (dwWaitStatus) 
      { 
         case WAIT_OBJECT_0: 

         // A file size changed in the directory.
         // do your processing and restart the notification.

          _tprintf(TEXT("Directory (%s) changed.\n"), lpDir);

             if (FindNextChangeNotification(dwChangeHandles[0]) == FALSE)
             {
               printf("\n ERROR: FindNextChangeNotification function failed.\n");
               ExitProcess(GetLastError()); 
             }
             break; 

         default: 
            printf("\n ERROR: Unhandled dwWaitStatus.\n");
            ExitProcess(GetLastError());
            break;
      }
   }
}