如何在完成例程中使用ReadDirectoryChangesW()方法?

时间:2008-12-05 01:38:33

标签: windows asynchronous readdirectorychangesw

我希望在异步模式下使用函数ReadDirectoryChangesW()并提供I / O完成例程。

问题是我不知道如何在完成例程(CALLBACK函数)中检索有关更改的确切信息。完成例程的定义如下:

VOID CALLBACK FileIOCompletionRoutine(
  [in]                 DWORD dwErrorCode,
  [in]                 DWORD dwNumberOfBytesTransfered,
  [in]                 LPOVERLAPPED lpOverlapped
);

我想知道这些信息是否包含在LPOVERLAPPED结构中。但我不知道如何得到它。

1 个答案:

答案 0 :(得分:4)

很棒的问题!它已经晚了7年,但这里有点回答,或者更重要的是,如何找到它。那么,ReadDirectoryChangesW的文档:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx

参数部分中的

提供了一个指向FileIOCompletionRoutine的链接:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364052%28v=vs.85%29.aspx

在Examples部分中给出了使用完成例程命名管道服务器的链接:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365601%28v=vs.85%29.aspx

是否相信它甚至不使用ReadDirectoryChangesW,但实际上是使用ReadFileEx给出一个例子,使用FileIOCompletionRoutine:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365468%28v=vs.85%29.aspx

你可以在这段代码中看到使用这两个函数(ReadFileEx和CompletedReadRoutine(它是应用程序定义的回调函数FileIOCompletionRoutine的实现))的示例:

// CompletedWriteRoutine(DWORD, DWORD, LPOVERLAPPED) 
// This routine is called as a completion routine after writing to 
// the pipe, or when a new client has connected to a pipe instance.
// It starts another read operation. 

VOID WINAPI CompletedWriteRoutine(DWORD dwErr, DWORD cbWritten, 
   LPOVERLAPPED lpOverLap) 
{ 
   LPPIPEINST lpPipeInst; 
   BOOL fRead = FALSE; 

// lpOverlap points to storage for this instance. 

   lpPipeInst = (LPPIPEINST) lpOverLap; 

// The write operation has finished, so read the next request (if 
// there is no error). 

   if ((dwErr == 0) && (cbWritten == lpPipeInst->cbToWrite)) 
      fRead = ReadFileEx( 
         lpPipeInst->hPipeInst, 
         lpPipeInst->chRequest, 
         BUFSIZE*sizeof(TCHAR), 
         (LPOVERLAPPED) lpPipeInst, 
         (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedReadRoutine); 

// Disconnect if an error occurred. 

   if (! fRead) 
      DisconnectAndClose(lpPipeInst); 
} 

// CompletedReadRoutine(DWORD, DWORD, LPOVERLAPPED) 
// This routine is called as an I/O completion routine after reading 
// a request from the client. It gets data and writes it to the pipe. 

VOID WINAPI CompletedReadRoutine(DWORD dwErr, DWORD cbBytesRead, 
    LPOVERLAPPED lpOverLap) 
{ 
   LPPIPEINST lpPipeInst; 
   BOOL fWrite = FALSE; 

// lpOverlap points to storage for this instance. 

   lpPipeInst = (LPPIPEINST) lpOverLap; 

// The read operation has finished, so write a response (if no 
// error occurred). 

   if ((dwErr == 0) && (cbBytesRead != 0)) 
   { 
      GetAnswerToRequest(lpPipeInst); 

      fWrite = WriteFileEx( 
         lpPipeInst->hPipeInst, 
         lpPipeInst->chReply, 
         lpPipeInst->cbToWrite, 
         (LPOVERLAPPED) lpPipeInst, 
         (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedWriteRoutine); 
   } 

// Disconnect if an error occurred. 

   if (! fWrite) 
      DisconnectAndClose(lpPipeInst); 
}

这不是一个好的答案(我只是在探索自己是否甚至想要使用这些功能),但它应该可以帮助人们开始。

另见:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365261%28v=vs.85%29.aspx