ReactOS 0.4.15-dev-7958-gcd0bb1a
notificationtest.c File Reference
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
Include dependency graph for notificationtest.c:

Go to the source code of this file.

Macros

#define _wsplitpath_s(f, d, dl, p, pl, n, nl, e, el)   _wsplitpath(f, d, p, n, e)
 
#define _splitpath_s(f, d, dl, p, pl, n, nl, e, el)   _splitpath(f, d, p, n, e)
 

Functions

void RefreshDirectory (LPTSTR)
 
void RefreshTree (LPTSTR)
 
void WatchDirectory (LPTSTR)
 
int _tmain (int argc, TCHAR *argv[])
 

Macro Definition Documentation

◆ _splitpath_s

#define _splitpath_s (   f,
  d,
  dl,
  p,
  pl,
  n,
  nl,
  e,
  el 
)    _splitpath(f, d, p, n, e)

Definition at line 16 of file notificationtest.c.

◆ _wsplitpath_s

#define _wsplitpath_s (   f,
  d,
  dl,
  p,
  pl,
  n,
  nl,
  e,
  el 
)    _wsplitpath(f, d, p, n, e)

Definition at line 15 of file notificationtest.c.

Function Documentation

◆ _tmain()

int _tmain ( int  argc,
TCHAR argv[] 
)

Definition at line 23 of file notificationtest.c.

24{
25 if(argc != 2)
26 {
27 _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
28 return -1;
29 }
30
32
33 return 0;
34}
static int argc
Definition: ServiceArgs.c:12
#define _tprintf
Definition: tchar.h:506
#define TEXT(s)
Definition: k32.h:26
#define argv
Definition: mplay32.c:18
void WatchDirectory(LPTSTR)

◆ RefreshDirectory()

void RefreshDirectory ( LPTSTR  lpDir)

Definition at line 142 of file notificationtest.c.

143{
144 // This is where you might place code to refresh your
145 // directory listing, but not the subtree because it
146 // would not be necessary.
147
148 _tprintf(TEXT("Directory (%s) changed.\n"), lpDir);
149}

Referenced by WatchDirectory().

◆ RefreshTree()

void RefreshTree ( LPTSTR  lpDrive)

Definition at line 151 of file notificationtest.c.

152{
153 // This is where you might place code to refresh your
154 // directory listing, including the subtree.
155
156 _tprintf(TEXT("Directory tree (%s) changed.\n"), lpDrive);
157}

Referenced by WatchDirectory().

◆ WatchDirectory()

void WatchDirectory ( LPTSTR  lpDir)

Definition at line 36 of file notificationtest.c.

37{
38 DWORD dwWaitStatus;
39 HANDLE dwChangeHandles[2];
40 TCHAR lpDrive[4];
41 TCHAR lpFile[_MAX_FNAME];
42 TCHAR lpExt[_MAX_EXT];
43
44 _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);
45
46 lpDrive[2] = (TCHAR)'\\';
47 lpDrive[3] = (TCHAR)'\0';
48
49// Watch the directory for file creation and deletion.
50
51 dwChangeHandles[0] = FindFirstChangeNotification(
52 lpDir, // directory to watch
53 FALSE, // do not watch subtree
54 FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes
55
56 if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
57 {
58 printf("\n ERROR: FindFirstChangeNotification function failed.\n");
60 }
61
62// Watch the subtree for directory creation and deletion.
63
64 dwChangeHandles[1] = FindFirstChangeNotification(
65 lpDrive, // directory to watch
66 TRUE, // watch the subtree
67 FILE_NOTIFY_CHANGE_DIR_NAME); // watch dir name changes
68
69 if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
70 {
71 printf("\n ERROR: FindFirstChangeNotification function failed.\n");
73 }
74
75
76// Make a final validation check on our handles.
77
78 if ((dwChangeHandles[0] == NULL) || (dwChangeHandles[1] == NULL))
79 {
80 printf("\n ERROR: Unexpected NULL from FindFirstChangeNotification.\n");
82 }
83
84// Change notification is set. Now wait on both notification
85// handles and refresh accordingly.
86
87 while (TRUE)
88 {
89 // Wait for notification.
90
91 printf("\nWaiting for notification...\n");
92
93 dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,
95
96 switch (dwWaitStatus)
97 {
98 case WAIT_OBJECT_0:
99
100 // A file was created, renamed, or deleted in the directory.
101 // Refresh this directory and restart the notification.
102
103 RefreshDirectory(lpDir);
104 if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE )
105 {
106 printf("\n ERROR: FindNextChangeNotification function failed.\n");
108 }
109 break;
110
111 case WAIT_OBJECT_0 + 1:
112
113 // A directory was created, renamed, or deleted.
114 // Refresh the tree and restart the notification.
115
116 RefreshTree(lpDrive);
117 if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE )
118 {
119 printf("\n ERROR: FindNextChangeNotification function failed.\n");
121 }
122 break;
123
124 case WAIT_TIMEOUT:
125
126 // A timeout occurred, this would happen if some value other
127 // than INFINITE is used in the Wait call and no changes occur.
128 // In a single-threaded environment you might not want an
129 // INFINITE wait.
130
131 printf("\nNo changes in the timeout period.\n");
132 break;
133
134 default:
135 printf("\n ERROR: Unhandled dwWaitStatus.\n");
137 break;
138 }
139 }
140}
BOOL WINAPI FindNextChangeNotification(IN HANDLE hChangeHandle)
Definition: cnotify.c:223
#define WAIT_TIMEOUT
Definition: dderror.h:14
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
VOID WINAPI ExitProcess(IN UINT uExitCode)
Definition: proc.c:1487
#define INFINITE
Definition: serial.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
#define printf
Definition: freeldr.h:97
#define _tsplitpath_s
Definition: tchar.h:687
#define _MAX_FNAME
Definition: utility.h:74
#define _MAX_EXT
Definition: utility.h:76
void RefreshDirectory(LPTSTR)
void RefreshTree(LPTSTR)
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FindFirstChangeNotification
Definition: winbase.h:3781
#define WAIT_OBJECT_0
Definition: winbase.h:406
#define FILE_NOTIFY_CHANGE_FILE_NAME
#define FILE_NOTIFY_CHANGE_DIR_NAME
char TCHAR
Definition: xmlstorage.h:189

Referenced by _tmain().