4,393,630 members and growing!   8,643 now online. My Profile | My Settings | My Bookmarks | My Articles | Sign out
All Topics, MFC/C++ >> Files and Folders >> General

FileMonitor
By Franky Braem.

An ATL control for monitoring your directories and/or files for updates, creation and deletion
C++ (VC6), VB
Windows (Win95, NT4, Win2K, Win98)
MFC, Win32, VS (VS5, VS6)
Dev
Posted: 18 Apr 2000
Updated: 12 Dec 2000
Views: 95,456
Announcements


Search    
Advanced Search
Sitemap
PrintBroken Article?Bookmark Discuss Send to a friend
35 votes for this article.
Popularity: 6.93. Rating: 4.49 out of 5.

Introduction

This project makes it possible to monitor your directories and/or your files. The control will generate an event whenever a file or subdirectory is created, updated or deleted.

If you want to know how you can use this control in a MFC application, then look at the page: How to use an ATL-control with MFC.

I've written this project in Visual C++ 6.0 with ATL 3.0, because you can't use multithreading in Visual Basic.

Properties

Name Type R/W Description
Start Boolean Read/Write

When set to true, the monitor will start watching the directories.

Returns True if the monitor is started. False when it is not started.

Methods

Name Returns Parameters Description
AddPath / ByVal sPathName as String Adds a directory for monitoring.
RemovePath / ByVal sPathName as String Removes a directory.

Events

Name Parameters Description
Notify ByVal sPathName As String, ByVal nType As Integer Notifies the control that a file (nType = 1) or a subdirectory (nType = 0) is changed, deleted or created in the directory sPathName.

Remarks

It's recommended that you first add all the directories you want to monitor before you start monitoring, because when the monitor is running, it will always be stopped and started again when you add a new directory.

The control was tested on Windows NT and Windows 98.

The control is written with Visual C++ 6.0 using ATL 3.0. The control was tested with Visual Basic 6.0

Resources

These are the resources I've used to create this project:

  1. PRB: Firing Event in Second Thread Causes IPF or GPF

Because I use multithreading in this control and it's not possible to fire an event from within a thread, I used the following trick to fire the event.

The control is created as a window. I pass a pointer of the control to the thread function where I can use the window handle of the control to send a message. This message will be handled and will fire the event to the container of the control.

class ATL_NO_VTABLE CWatch : 
    public CWindowImpl<CWatch>, // The control is implemented as a window
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CWatch, &CLSID_Watch>,
    public IConnectionPointContainerImpl<CWatch>,
    public IDispatchImpl<IWatch, &IID_IWatch, &LIBID_FILEMONITOR>,
    public CProxy_IWatchEvents< CWatch >
{
public:
    CWatch()
    {
        m_bStarted = false;
        m_hEvent = NULL;
        m_hThread = NULL;
    }
    deqString m_Paths;
    HANDLE m_hEvent;
    HANDLE m_hThread;
    DWORD m_dwThreadId;
    bool m_bStarted;
    cstring m_sNotify;

DECLARE_WND_CLASS(TEXT("Watch"))
BEGIN_MSG_MAP(CWatch)
    MESSAGE_HANDLER(WM_WATCH_NOTIFY, OnWatchNotify) // The messagehandler
END_MSG_MAP()

    LRESULT OnWatchNotify(UINT uMsg, WPARAM wParam, 
        LPARAM lParam, BOOL &bHandled)    // Fires the Notify event
    {
        Fire_Notify(_bstr_t(m_sNotify.c_str()), wParam);
        return 0;
    }
    ...
};

The thread is created as follows in the Start method:

m_hThread = CreateThread(NULL, 0, ThreadFunc, (LPVOID) this, 0, &m_dwThreadId);

As you can see, I pass the this-pointer to the ThreadFunc.

Updates

  • 11 May 2000 - fixed a bug in the AddPath-method
  • 12 Dec 2000 - The major change is that the thread for monitoring the directories is created only once.

    In the past, the thread was recreated when you added or removed a path. Now I use an event to notify the thread. When the thread receives the event, it will automatically refresh the FileNotifications.

    Check my website for updates.

About Franky Braem


Programmer since 1991 using C/C++/Visual Basic and Java. Playing with wxWindows at home.

Click here to view Franky Braem's online profile.


Other popular Files and Folders articles:

[Top] Rate this Article for us!     PoorExcellent  
 
Hint: For a faster board use IE 4+ or Mozilla, choose 'Message View' from the View dropdown and hit 'Set Options'.
FAQ Message score threshold    Search comments  
 View   Per page  
New MessageMsgs 1 to 25 of 27 (Total: 27) (Refresh)[First] [Prev] [Next] [    ]
Subject 
Author 
Date 
  good codes, but 2 questions... [modified]
 m_harriss 23:03 26 Jul '06 
  Why First Access Winlogon????
 easter_2007 1:04 21 Feb '06 
  details of detected file
 gajesh 7:01 13 Sep '05 
  Java Integration
 anindya12345 4:15 11 Nov '04 
  some bug ?
 boy0612 2:44 13 May '04 
  How to get Log of all files used for a day
 Legend Never Dies 2:51 24 Feb '04 
  Re: How to get Log of all files used for a day
 m_harriss 22:40 26 Jul '06 
  Creating an invisible file monitor
 PerlHack 22:24 17 Apr '03 
  Can you notify the user before deleting a file and let the user confirm it?
 ja16188 20:12 12 Dec '02 
  watch.cpp
Unconfirmed/Anonymous posting Anonymous 18:33 13 Nov '01 
  :watch.h
Unconfirmed/Anonymous posting Anonymous 18:32 13 Nov '01 
  :watch.h
Unconfirmed/Anonymous posting Anonymous 18:30 13 Nov '01 
  help, gurus!!
Unconfirmed/Anonymous posting Anonymous 20:23 12 Oct '01 
  Re: help, gurus!!
 Jim Crafton 20:29 12 Oct '01 
  Re: help, gurus!!
Unconfirmed/Anonymous posting Anonymous 20:35 12 Oct '01 
  Re: help, gurus!!
Unconfirmed/Anonymous posting Anonymous 20:51 12 Oct '01 
  Don't quit your day job...
Unconfirmed/Anonymous posting Anonymous 23:54 4 Jul '01 
  Re: Don't quit your day job...
 Franky Braem 9:21 6 Aug '01 
  Good but needs some work
Unconfirmed/Anonymous posting Anonymous 23:49 4 Jul '01 
  Display Filename?
Unconfirmed/Anonymous posting Anonymous 13:33 16 Apr '01 
  Who Deleted the files....
 Fred 10:59 12 Dec '00 
  Re: Who Deleted the files....
 Franky Braem 2:57 13 Dec '00 
  New release & example of using it MFC
Unconfirmed/Anonymous posting F B 7:13 7 Dec '00 
  If you use control in VB why not Use ::GetActiveWindow for the this pointer?
Unconfirmed/Anonymous posting Rick Benish 16:39 28 Jun '00 
  Re: If you use control in VB why not Use ::GetActiveWindow for the this pointer?
 Wes Jones 17:47 12 Jul '01 
Last Visit: 7:46 Sunday 12th August, 2007[First] [Prev] [Next] [    ]

General comment    News / Info    Question    Answer    Joke / Game    Admin message


Updated: 12 Dec 2000 Article content copyright Franky Braem, 2000
everything else Copyright © CodeProject, 1999-2007.
Web18 | Advertise on The Code Project | Privacy

The Ultimate ToolboxASP AllianceDeveloper FusionDevelopersdexDevGuruProgrammers HeavenPlanet Source CodeTek-Tips Forums