As far as the event viewer error code, I would suggest that you call PeekNamedPipe before calling ReadFile to verify that there is actually some data in the pipe for the ReadFile function. If there isn't any data in the pipe then do NOT execute ReadFile.
How about executing the DisplayError function when SetNamedPipeHandleState fails and post the detailed error message?