fSuccess = ReadFile(
hPipe, // pipe handle
chBuf, // buffer to receive reply
BUFSIZE*sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
If not safe, how can I ensure the other side is not writin开发者_JAVA技巧g when reading a pipe in windows?
Yes, this is a perfectly legal operation on the pipe. One end of the pipe can read from and write to a pipe irrespective of what is happening to the other end.
It's perfectly safe -- pipes handle all the necessary synchronization on the buffers and such automatically. If you try to write to/read from a pipe when the other process has closed its connection to the pipe (either explicitly by closing the pipe, or implicitly by exiting the process) you'll get ERROR_BROKEN_PIPE
from GetLastError
. If you're using anonymous pipes the parent process will typically look for this to detect when the child process exited, so there will be no more data to process.
精彩评论