Click here to Skip to main content
13,194,231 members (49,839 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 17 Apr 2000


, 11 Dec 2000
Rate this:
Please Sign up or sign in to vote.
An ATL control for monitoring your directories and/or files for updates, creation and deletion


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.



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

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


AddPath/ByVal sPathName as StringAdds a directory for monitoring.
RemovePath/ByVal sPathName as StringRemoves a directory.


NotifyByVal sPathName As String, ByVal nType As IntegerNotifies the control that a file (nType = 1) or a subdirectory (nType = 0) is changed, deleted or created in the directory sPathName.


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


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 >
        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;

    MESSAGE_HANDLER(WM_WATCH_NOTIFY, OnWatchNotify) // The messagehandler

    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.


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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Franky Braem
Web Developer
Belgium Belgium
Programmer since 1991 using C/C++/Visual Basic and Java. Playing with wxWindows at home.

You may also be interested in...

Comments and Discussions

QuestionThread problem Pin
fredrick9911-Aug-11 8:18
memberfredrick9911-Aug-11 8:18 
Answergood Pin
v_fisher2-Aug-11 17:22
memberv_fisher2-Aug-11 17:22 
GeneralNo FileMonitor Pin
sprice864-Nov-07 6:11
membersprice864-Nov-07 6:11 
Questiongood codes, but 2 questions... [modified] Pin
m_harriss26-Jul-06 17:03
memberm_harriss26-Jul-06 17:03 
QuestionWhy First Access Winlogon???? Pin
easter_200720-Feb-06 19:04
membereaster_200720-Feb-06 19:04 
Generaldetails of detected file Pin
gajesh13-Sep-05 1:01
membergajesh13-Sep-05 1:01 
GeneralJava Integration Pin
anindya1234510-Nov-04 22:15
memberanindya1234510-Nov-04 22:15 
Questionsome bug ? Pin
boy061212-May-04 20:44
memberboy061212-May-04 20:44 
QuestionHow to get Log of all files used for a day Pin
Legend Never Dies23-Feb-04 20:51
memberLegend Never Dies23-Feb-04 20:51 
AnswerRe: How to get Log of all files used for a day Pin
m_harriss26-Jul-06 16:40
memberm_harriss26-Jul-06 16:40 
GeneralCreating an invisible file monitor Pin
PerlHack17-Apr-03 16:24
memberPerlHack17-Apr-03 16:24 
QuestionCan you notify the user before deleting a file and let the user confirm it? Pin
ja1618812-Dec-02 14:12
memberja1618812-Dec-02 14:12 
Generalwatch.cpp Pin
Anonymous13-Nov-01 12:33
memberAnonymous13-Nov-01 12:33 
General:watch.h Pin
Anonymous13-Nov-01 12:32
memberAnonymous13-Nov-01 12:32 
General:watch.h Pin
Anonymous13-Nov-01 12:30
memberAnonymous13-Nov-01 12:30 
Generalhelp, gurus!! Pin
Anonymous12-Oct-01 14:23
memberAnonymous12-Oct-01 14:23 
GeneralRe: help, gurus!! Pin
Jim Crafton12-Oct-01 14:29
memberJim Crafton12-Oct-01 14:29 
GeneralRe: help, gurus!! Pin
Anonymous12-Oct-01 14:35
memberAnonymous12-Oct-01 14:35 
GeneralRe: help, gurus!! Pin
Anonymous12-Oct-01 14:51
memberAnonymous12-Oct-01 14:51 
GeneralDon't quit your day job... Pin
Anonymous4-Jul-01 17:54
memberAnonymous4-Jul-01 17:54 
GeneralRe: Don't quit your day job... Pin
Franky Braem6-Aug-01 3:21
memberFranky Braem6-Aug-01 3:21 
GeneralGood but needs some work Pin
Anonymous4-Jul-01 17:49
memberAnonymous4-Jul-01 17:49 
QuestionDisplay Filename? Pin
Anonymous16-Apr-01 7:33
memberAnonymous16-Apr-01 7:33 
GeneralWho Deleted the files.... Pin
Fred12-Dec-00 4:59
memberFred12-Dec-00 4:59 
GeneralRe: Who Deleted the files.... Pin
Franky Braem12-Dec-00 20:57
memberFranky Braem12-Dec-00 20:57 
GeneralNew release & example of using it MFC Pin
F B7-Dec-00 1:13
memberF B7-Dec-00 1:13 
QuestionIf you use control in VB why not Use ::GetActiveWindow for the this pointer? Pin
Rick Benish28-Jun-00 10:39
sussRick Benish28-Jun-00 10:39 
AnswerRe: If you use control in VB why not Use ::GetActiveWindow for the this pointer? Pin
Wes Jones12-Jul-01 11:47
memberWes Jones12-Jul-01 11:47 
GeneralNew Release Pin
Franky Braem15-May-00 22:46
sussFranky Braem15-May-00 22:46 
GeneralFiring Event across Thread Pin
Etienne8-May-00 6:43
sussEtienne8-May-00 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171018.2 | Last Updated 12 Dec 2000
Article Copyright 2000 by Franky Braem
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid