Using the WaitForMultipleObjects() function in Win32 C program example

 

 

 

 

// For WinXp as a target, change appropriately

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

 

int main(int argc, char argv[])

{

DWORD dwWaitStatus;

HANDLE dwChangeHandles[2];

// The directory and drive is there...just for testing

// char * DirName = "F:\\myproject";

LPCWSTR DirName = L"F:\\myproject";

// char * DirName1 = "F:\\";

LPCWSTR DirName1 = L"F:\\";

 

// Watch the F:\myproject directory for file creation and deletion.

dwChangeHandles[0] = FindFirstChangeNotification(

DirName, // directory to watch

FALSE, // do not watch the subtree

FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes

 

if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)

ExitProcess(GetLastError());

else

printf("FindFirstChangeNotification() for file change is OK.\n");

 

// Watch the F:\\ subtree for directory creation and deletion.

dwChangeHandles[1] = FindFirstChangeNotification(

DirName1, // directory to watch

TRUE, // watch the subtree

FILE_NOTIFY_CHANGE_DIR_NAME); // watch directory name changes

 

if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)

{

printf("Something wrong!\n");

ExitProcess(GetLastError());

}

else

printf("FindFirstChangeNotification() for directory change is OK.\n");

 

// Again...some messages

if (dwChangeHandles[0] != INVALID_HANDLE_VALUE && dwChangeHandles[1] != INVALID_HANDLE_VALUE)

{

printf("\nI'm monitoring any file deletion/creation in %S and\n", DirName);

printf("I'm monitoring any directory deletion/creation in %S.\n", DirName1);

}

// Change notification is set. Now wait on both notifications handles and refresh accordingly.

while (TRUE)

{

// Wait for notification.

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

switch (dwWaitStatus)

{

case 0: // WAIT_OBJECT_0

// A file was created or deleted in F:\myproject. Refresh this directory and restart the change notification.

// May call application define function here...

if (FindNextChangeNotification(dwChangeHandles[0]) == FALSE)

{

printf("FindNextChangeNotification() not OK\n");

ExitProcess(GetLastError());

}

else

printf("File created/deleted in %S.\n", DirName);

break;

 

case 1: // WAIT_OBJECT_0 + 1

// A directory was created or deleted in F:\.

// Refresh the directory tree and restart the change notification.

// May call application define function here...

if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE)

ExitProcess(GetLastError());

else

printf("Directory was deleted/created in %S.\n", DirName1);

break;

 

default:

printf("FindNextChangeNotification(): Invalid return value.\n");

ExitProcess(GetLastError());

}

}

// May close the handles...

if(FindCloseChangeNotification(dwChangeHandles[0]) != 0)

printf("FindCloseChangeNotification() is OK\n");

if(FindCloseChangeNotification(dwChangeHandles[1]) != 0)

printf("FindCloseChangeNotification() is OK\n");

return 0;

}

 

// Run this program. Then open the F:\myproject folder, create a folder and a text file and then delete them. See the output...

 

Output example:

 

FindFirstChangeNotification() for file change is OK.

FindFirstChangeNotification() for directory change is OK.

I'm monitoring any file deletion/creation in F:\myproject and

I'm monitoring any directory deletion/creation in F:\.

File created/deleted in F:\myproject.

Directory was deleted/created in F:\.

Directory was deleted/created in F:\.

Directory was deleted/created in F:\.

File created/deleted in F:\myproject.

^CPress any key to continue . . .

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Using the WaitForMultipleObjects() in Win32 C code sample

To show: The Windows process and thread related functions for thread signaling

 

 

C and C++ Programming Resources | C & C++ Code Example Index