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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/342668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:42:28  来源:igfitidea点击:

How to use ReadDirectoryChangesW() method with completion routine?

windowsasynchronousreaddirectorychangesw

提问by user26404

I want to use function ReadDirectoryChangesW()in asynchronous mode with I/O completion routine supplied.

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

The question is I don't know how to retrieve the exact information about the change in the completion routine (a CALLBACKfunction). Completion routine is defined like this:

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

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

I wonder the information is included in the LPOVERLAPPEDstructure. But I don't know how to get it.

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

回答by Andrew

Excellent question! It's 7 years late, but here's somewhat of an answer, or, more importantly, how to find it. So, the documentation for ReadDirectoryChangesW:

好问题!晚了 7 年,但这里有一些答案,或者更重要的是,如何找到它。因此,ReadDirectoryChangesW 的文档:

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

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

in the Parameters section gives a link to FileIOCompletionRoutine:

在参数部分中提供了指向 FileIOCompletionRoutine 的链接:

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

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

which in the Examples section gives a link to Named Pipe Server Using Completion Routines:

在示例部分提供了使用完成例程的命名管道服务器的链接:

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

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

which believe it or not doesn't even use ReadDirectoryChangesW but actually gives an example using ReadFileEx, which alsouses FileIOCompletionRoutine:

信不信由你甚至不使用 ReadDirectoryChangesW 但实际上给出了一个使用 ReadFileEx 的例子,它使用 FileIOCompletionRoutine:

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

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

and you can see an example of them using these two functions (ReadFileEx and CompletedReadRoutine (which is an implementation of the application-defined callback function FileIOCompletionRoutine)) in this piece of code:

您可以在这段代码中看到使用这两个函数(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); 
}

It's not a great answer (I was only exploring whether I even wanted to use these functions, myself), but it should help people get started.

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

See also:

也可以看看:

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

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