免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2081 | 回复: 1
打印 上一主题 下一主题

[C++] WaitForMultipleObjects怎么得到具体的事件参数 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-06-12 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-07-07 21:44 |显示全部楼层 |倒序浏览

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <tchar.h>

  5. void RefreshDirectory(LPTSTR);
  6. void RefreshTree(LPTSTR);
  7. void WatchDirectory(LPTSTR);

  8. void _tmain(int argc, TCHAR *argv[])
  9. {
  10.     if(argc != 2)
  11.     {
  12.         _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
  13.         return;
  14.     }

  15.     WatchDirectory(argv[1]);
  16. }

  17. void WatchDirectory(LPTSTR lpDir)
  18. {
  19.    DWORD dwWaitStatus;
  20.    HANDLE dwChangeHandles[2];
  21.    TCHAR lpDrive[4];
  22.    TCHAR lpFile[_MAX_FNAME];
  23.    TCHAR lpExt[_MAX_EXT];

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

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

  27. // Watch the directory for file creation and deletion.

  28.    dwChangeHandles[0] = FindFirstChangeNotification(
  29.       lpDir,                         // directory to watch
  30.       TRUE,                         // do not watch subtree
  31.       FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes

  32.    if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
  33.    {
  34.      printf("\n ERROR: FindFirstChangeNotification function failed.\n");
  35.      ExitProcess(GetLastError());
  36.    }

  37. // Watch the subtree for directory creation and deletion.

  38.    dwChangeHandles[1] = FindFirstChangeNotification(
  39.       lpDrive,                       // directory to watch
  40.       TRUE,                          // watch the subtree
  41.       FILE_NOTIFY_CHANGE_DIR_NAME);  // watch dir name changes

  42.    if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
  43.    {
  44.      printf("\n ERROR: FindFirstChangeNotification function failed.\n");
  45.      ExitProcess(GetLastError());
  46.    }


  47. // Make a final validation check on our handles.

  48.    if ((dwChangeHandles[0] == NULL) || (dwChangeHandles[1] == NULL))
  49.    {
  50.      printf("\n ERROR: Unexpected NULL from FindFirstChangeNotification.\n");
  51.      ExitProcess(GetLastError());
  52.    }

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

  55.    while (TRUE)
  56.    {
  57.    // Wait for notification.

  58.       printf("\nWaiting for notification...\n");

  59.       dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,
  60.          FALSE, INFINITE);

  61.       switch (dwWaitStatus)
  62.       {
  63.          case WAIT_OBJECT_0:

  64.          // A file was created, renamed, or deleted in the directory.
  65.          // Refresh this directory and restart the notification.

  66.              RefreshDirectory(lpDir);
  67.              if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE )
  68.              {
  69.                printf("\n ERROR: FindNextChangeNotification function failed.\n");
  70.                ExitProcess(GetLastError());
  71.              }
  72.              break;

  73.          case WAIT_OBJECT_0 + 1:

  74.          // A directory was created, renamed, or deleted.
  75.          // Refresh the tree and restart the notification.

  76.              RefreshTree(lpDrive);
  77.              if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE )
  78.              {
  79.                printf("\n ERROR: FindNextChangeNotification function failed.\n");
  80.                ExitProcess(GetLastError());
  81.              }
  82.              break;

  83.          case WAIT_TIMEOUT:

  84.          // A timeout occurred, this would happen if some value other
  85.          // than INFINITE is used in the Wait call and no changes occur.
  86.          // In a single-threaded environment you might not want an
  87.          // INFINITE wait.

  88.             printf("\nNo changes in the timeout period.\n");
  89.             break;

  90.          default:
  91.             printf("\n ERROR: Unhandled dwWaitStatus.\n");
  92.             ExitProcess(GetLastError());
  93.             break;
  94.       }
  95.    }
  96. }

  97. void RefreshDirectory(LPTSTR lpDir)
  98. {
  99.    // This is where you might place code to refresh your
  100.    // directory listing, but not the subtree because it
  101.    // would not be necessary.

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

  104. void RefreshTree(LPTSTR lpDrive)
  105. {
  106.    // This is where you might place code to refresh your
  107.    // directory listing, including the subtree.
  108.   
  109.    _tprintf(TEXT("Directory tree (%s) changed.\n"), lpDrive);
  110. }

复制代码

这段程序是监控某个目录的io操作的,目前只能实现知道发生了某个事件,但如果要获取详细的参数,比如 “A file was created, renamed, or deleted in the directory.”
  新建的文件名,删除的文件名,这些详细参数,有没有api可以获取到?


论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-06-12 06:20:00
2 [报告]
发表于 2017-07-10 16:10 |显示全部楼层
回复 2# bruceteen

是在case WAIT_OBJECT_0: 的分支中调用这个api吗?  能否详细指点下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP