Use CallNamedPipe function to write message, the error 233 ERROR_PIPE_NOT_CONNECTED occurs.
Example:
NamedPipeServer:
hpipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\testpipe1"
, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFFER_SIZE* sizeof(wchar_t), // output buffer size
BUFFER_SIZE* sizeof(wchar_t), // input buffer size
0, // client time-out
&sa); // default security attribute );
if(hpipe == INVALID_HANDLE_VALUE){
printf(": Create Namepipe error");
return false;
}
printf(": Waiting for connect" );
Connected = ConnectNamedPipe(hpipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if(Connected == TRUE){
printf(": NamedPipe is connected");
if(FALSE == ReadFile(hpipe,payload, IPC_BUFFER_SIZE * sizeof(wchar_t), &numBytes, NULL)){
printf(": ReadFile failed, error code = %d", GetLastError());
return false;
}
printf(": The received data is %s, %d bytes", payload,numBytes);
}
else{
printf(": ConnectNamedPipe failed, error = %d", GetLastError());
}
DisconnectNamedPipe(hpipe);
NamedPipeClient:
fSuccess = CallNamedPipe(
TEXT("\\\\.\\pipe\\testpipe1"
, // pipe name
payload, // message to server
wcslen(payload)*sizeof(wchar_t), // message length
chReadBuf, // buffer to receive reply
BUFFER_SIZE*sizeof(wchar_t), // size of read buffer
&cbRead, // number of bytes read
20000); // waits for 20 seconds
if (fSuccess) { printf(": Send data success,Payload = %s, %d bytes",payload,wcslen(payload)*sizeof(wchar_t)); } else{ if(GetLastError() == ERROR_MORE_DATA){ printf(": CallNamedPipe get ERROR_MORE_DATA error."); } else{ printf(": CallNamedPipe error. error code = %d",GetLastError()); // Here get error 233 } }
Root cause:
CallNamedPipe function does both writing and reading message. But the server disconnect the namedpipe after receiving message.
So CallNamedPipe detects the namedpipe is closed while it's trying to read message, then ERROR_PIPE_NOT_CONNECTED occurs.
Solution/Workaround:
Write a empty message back before DisconnectNamedPipe(hpipe);
Add a comment