如何将 ReadDirectoryChangesW() 方法与完成例程一起使用?

发布于 2024-07-10 08:39:03 字数 415 浏览 9 评论 0 原文

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

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

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

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

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

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
);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-07-17 08:39:03

很好的问题! 虽然晚了 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

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

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

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

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

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

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.

See also:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文