【问题标题】:Waitformultipleobjects returns invalid handleWaitformultipleobjects 返回无效句柄
【发布时间】:2015-10-22 05:11:30
【问题描述】:

代码如下,它是线程的一部分。 pFileChange->m_hDirectory 是 HANDLE 类型,如果是 CEvent 类型,则 pFileChange->m_eventFileChange。 CreateFile 和 ReadDirectoryChangesW 成功返回。我无法弄清楚为什么我的句柄状态无效,请帮助!

UINT CFileChange::FileMontiorThread(LPVOID pArgs)
{
  CFileChange* pFileChange = NULL;

  pFileChange = (CFileChange*)pArgs;

  pFileChange = (CFileChange*)pArgs;

  CString str = pFileChange->m_strDirectory;

  LPSTR  strDirectory;
  strDirectory = str.GetBuffer(str.GetLength());
  PathRemoveFileSpec(strDirectory);

  DWORD dwBytes = 0;    
  vector<BYTE> m_Buffer;
  BOOL      m_bChildren;
  OVERLAPPED    m_Overlapped;
  HANDLE arrHandles[2] = { pFileChange->m_hDirectory,  pFileChange->m_eventFileChange };

  ::ZeroMemory(&m_Overlapped, sizeof(OVERLAPPED));
  DWORD dwBufferSize = 16384;
  m_Buffer.resize(dwBufferSize);
  m_bChildren = false;


  pFileChange->m_hDirectory = ::CreateFile(
     (LPCSTR)(LPCTSTR)strDirectory,
    FILE_LIST_DIRECTORY,                
    FILE_SHARE_READ                     
    | FILE_SHARE_WRITE
    | FILE_SHARE_DELETE,
    NULL,                               
    OPEN_EXISTING,                      
    FILE_FLAG_BACKUP_SEMANTICS          
    | FILE_FLAG_OVERLAPPED,
    NULL);                              

  if (pFileChange->m_hDirectory == INVALID_HANDLE_VALUE)
  {
    return false;
  }

  BOOL success = ::ReadDirectoryChangesW(
    pFileChange->m_hDirectory,  // handle to directory
    &m_Buffer[0],                       // read results buffer
    m_Buffer.size(),                    // length of buffer
    m_bChildren,                        // monitoring option
    FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION |  FILE_NOTIFY_CHANGE_FILE_NAME,        // filter conditions
    &dwBytes,                           // bytes returned
    &m_Overlapped,                      // overlapped buffer
    NULL);           // no completion routine

  DWORD dwWaitStatus;

  while (!pFileChange->m_bKillThread)
  {
    dwWaitStatus = WaitForMultipleObjects(2, arrHandles, FALSE, INFINITE);

   Switch(dwWaitStatus)
   {
     case WAIT_FAILED:
        {
            ULONG rc = 0;
            rc = ::GetLastError();
            LPVOID lpMsgBuf = NULL;
            ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                rc,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default  language
                (LPTSTR)&lpMsgBuf,
                0,
                NULL);
            CString strErrMsg;
            strErrMsg.Format(_T("%s, %s, Reason:%s"), "", "",         (LPTSTR)lpMsgBuf);
            break;
        }
    }
  }
    return 0;
}

【问题讨论】:

  • 您太早将句柄复制到arrHandles它们被创建之前。这当然行不通。

标签: multithreading visual-c++


【解决方案1】:

请注意,由于它指定了in the documentation,因此您不能等待任何类型的句柄。

等待目录句柄不会做你认为应该做的事情。 Read this related question 及其答案以获取更多信息和背景阅读。

您似乎正在尝试创建文件夹监视器,也许read this blog post 以正确使用ReadDirectoryChangesW

【讨论】:

    【解决方案2】:

    答案是provided by Hans Passant in a comment

    您太早将句柄复制到arrHandles它们被创建之前。这当然行不通。

    【讨论】:

      猜你喜欢
      • 2010-12-10
      • 1970-01-01
      • 2023-04-03
      • 2013-04-24
      • 2015-05-18
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多