I wrote a class to handle named pipe connections, and if I create an instance, close it and then try to create another instance, the call CreateFile()returns INVALID_HANDLE_VALUE, and GetLastError()returns ERROR_PIPE_BUSY, What is happening here? What can I do to ensure that the call ends Connect()?
PipeAsync A, B;
A.Connect("\\\\.\\pipe\\test",5000);
A.Close();
cout << GetLastError();
B.Connect("\\\\.\\pipe\\test",5000);
cout << GetLastError();
B.Close();
Here are my implementations Connect()andClose()
BOOL PipeAsync::Connect(LPCSTR pszPipeName, DWORD dwTimeout)
{
this->pszPipeName = pszPipeName;
this->fExisting = TRUE;
DWORD dwMode = this->fMessageMode ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE;
hPipe = CreateFile(
this->pszPipeName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if( INVALID_HANDLE_VALUE == hPipe )
return FALSE;
if( GetLastError() == ERROR_PIPE_BUSY )
if(!WaitNamedPipe( this->pszPipeName, dwTimeout ))
return FALSE;
if( !SetNamedPipeHandleState( hPipe, &dwMode, NULL, NULL ) )
return FALSE;
return TRUE;
}
VOID PipeAsync::Close()
{
if( fExisting )
DisconnectNamedPipe( hPipe );
CloseHandle( hPipe );
}
EDIT: I forgot to tell you how I did it ... I set the breakpoints indicated in the comments. At startup, it stops at the first breakpoint.
EDIT: This is my updated code.
if( INVALID_HANDLE_VALUE == hPipe )
if( GetLastError() == ERROR_PIPE_BUSY )
{
if(!WaitNamedPipe( this->pszPipeName, dwTimeout ))
return FALSE;
}
else
return FALSE;
WaitNamedPipe() false Connect() GetLastError() 2 ERROR_FILE_NOT_FOUND?