免费注册 查看新帖 |

Chinaunix

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

[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可以获取到?


论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2017-07-10 08:19 |只看该作者
得用 ReadDirectoryChangesW 才行

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-06-12 06:20:00
3 [报告]
发表于 2017-07-10 16:10 |只看该作者
回复 2# bruceteen

是在case WAIT_OBJECT_0: 的分支中调用这个api吗?  能否详细指点下

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
4 [报告]
发表于 2017-07-11 08:56 |只看该作者
按照MSDN随手写了个例子,但你最好还是自己去google吧

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <locale.h>

  4. int main( void )
  5. {
  6.         setlocale( LC_CTYPE, "" );

  7.         HANDLE hFile = CreateFileW( L"E:", FILE_LIST_DIRECTORY, FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL );
  8.         if( hFile != INVALID_HANDLE_VALUE )
  9.         {
  10.                 char buf[4096] = { 0 };
  11.                 DWORD ByteReturned;
  12.                 while( ReadDirectoryChangesW( hFile, buf, sizeof(buf), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME, &ByteReturned, NULL, NULL ) )
  13.                 {
  14.                         for( const FILE_NOTIFY_INFORMATION* pNotify=(FILE_NOTIFY_INFORMATION*)buf; pNotify; pNotify=pNotify->NextEntryOffset==0?NULL:(FILE_NOTIFY_INFORMATION*)((char*)pNotify + pNotify->NextEntryOffset) )
  15.                         {
  16.                                 switch( pNotify->Action )
  17.                                 {
  18.                                 case FILE_ACTION_ADDED:            wprintf( L"%s", L"ADDED   : " ); break;
  19.                                 case FILE_ACTION_REMOVED:          wprintf( L"%s", L"REMOVED : " ); break;
  20.                                 case FILE_ACTION_MODIFIED:         wprintf( L"%s", L"MODIFIED: " ); break;
  21.                                 case FILE_ACTION_RENAMED_OLD_NAME: wprintf( L"%s", L"OLD NAME: " ); break;
  22.                                 case FILE_ACTION_RENAMED_NEW_NAME: wprintf( L"%s", L"NEW NAME: " ); break;
  23.                                 }
  24.                                 wprintf( L"%.*s\n", pNotify->FileNameLength/sizeof(wchar_t), pNotify->FileName );
  25.                         }
  26.                 }

  27.                 CloseHandle( hFile );
  28.         }
  29. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP