[英]Windows named pipe partial read
當我通過 ReadFile 讀取管道並返回部分數據時,是否可能出現這種情況,導致我再次調用 ReadFile 直到達到指定的讀取字節數?
PIPE_TYPE_BYTE
設置在創建命名管道的一側。
在讀取端,設置PIPE_READMODE_BYTE
模式以讀取部分數據。
在部分讀取模式下,您可以使用PIPE_READMODE_BYTE
和PIPE_NOWAIT
組合來避免ReadFile
函數掛起。 lpNumberOfBytesRead
參數可用於檢測是否有更多數據要讀取。 當沒有要讀取的數據時,如果ReadFile
函數調用成功, lpNumberOfBytesRead
將為零。 否則,通過調用 GetLastError 函數檢查錯誤。
客戶端閱讀相關代碼將更改為:
dwMode = PIPE_READMODE_BYTE | PIPE_NOWAIT;// PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
// ...
WCHAR rdWChar;
do
{
// Read from the pipe.
fSuccess = ReadFile(
hPipe, // pipe handle
&rdWChar, // buffer to receive reply
2, // test size, read two bytes per read operation
&cbRead, // number of bytes read
NULL); // not overlapped
if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
break;
wprintf(L"%c", rdWChar);
} while (cbRead); // repeat loop if there is more bytes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.