如何使用ReadDirectoryChangesW()方法完成例程? [英] How to use ReadDirectoryChangesW() method with completion routine?

查看:748
本文介绍了如何使用ReadDirectoryChangesW()方法完成例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用在异步模式功能 ReadDirectoryChangesW()与I / O完成例程提供。

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

现在的问题是我不知道如何检索有关在完成例程变化的确切信息(A 回调功能)。完成例程的定义是这样的:

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

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

我想知道的信息包括在 LPOVERLAPPED 结构。但我不知道如何得到它。

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

推荐答案

很好的问题!它的七座旬,但这里有一个答案有点的,或者更重要的是,如何找到它。因此,对于ReadDirectoryChangesW的文档:

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:

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

在参数部分给出了一个链接FileIOCompletionRoutine:

in the Parameters section gives a link to FileIOCompletionRoutine:

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

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

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

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

和则可以使用这两个函数看到他们的例子(ReadFileEx和CompletedReadRoutine(这是应用程序定义的回调函数FileIOCompletionRoutine的实现)),在这片code的:

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:

// 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.

另请参阅:

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

这篇关于如何使用ReadDirectoryChangesW()方法完成例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆