2008-12-05 23 views
11

Tôi muốn sử dụng chức năng ReadDirectoryChangesW() ở chế độ không đồng bộ với quy trình hoàn thành I/O được cung cấp.Làm thế nào để sử dụng phương thức ReadDirectoryChangesW() với thường trình hoàn thành?

Câu hỏi đặt ra là tôi không biết cách lấy thông tin chính xác về thay đổi trong quy trình hoàn thành (chức năng CALLBACK). Quy trình hoàn thành được xác định như sau:

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

Tôi tự hỏi thông tin được bao gồm trong cấu trúc LPOVERLAPPED. Nhưng tôi không biết làm thế nào để có được nó.

Trả lời

3

Câu hỏi hay! Đã trễ 7 năm nhưng đây là câu trả lời hay quan trọng hơn là cách tìm số. Vì vậy, tài liệu cho ReadDirectoryChangesW:

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

trong phần thông số cho một liên kết đến FileIOCompletionRoutine:

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

mà trong phần Các ví dụ đưa ra một liên kết đến Named Pipe server Sử dụng Hoàn thành thói quen :

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

mà tin hay không, điều đó thậm chí không sử dụng ReadDirectoryChangesW nhưng thực sự đưa ra một ví dụ sử dụng ReadFileEx, mà cũng sử dụng FileIOCompletionRoutine:

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

và bạn có thể thấy một ví dụ trong số họ sử dụng hai chức năng (ReadFileEx và CompletedReadRoutine (đó là một thực hiện các ứng dụng xác định gọi lại chức năng FileIOCompletionRoutine)) trong đoạn mã này:

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

Nó không phải là một câu trả lời tuyệt vời (tôi chỉ khám phá liệu Tôi thậm chí còn muốn tự mình sử dụng các chức năng này, nhưng nó sẽ giúp mọi người bắt đầu.

Xem thêm:

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

Các vấn đề liên quan