gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:40:14 28 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

关于windows - 如何将 ReadDirectoryChangesW() 方法与完成例程一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/342668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com