Click to See Complete Forum and Search --> : Could not open pipe when testing named pipe


magol
January 13th, 2006, 03:32 AM
I try to use named pipes with problem. I test following code:

HANDLE hPipe;
LPVOID lpvMessage;
CHAR chBuf[512];
BOOL fSuccess;
DWORD cbRead, cbWritten, dwMode;
LPTSTR lpszPipename = "\\\\.\\pipe\\mynamedpipe";

// Try to open a named pipe; wait for it, if necessary.
while (1)
{
hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // no security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file

// Break if the pipe handle is valid.

if (hPipe != INVALID_HANDLE_VALUE)
break;

// Exit if an error other than ERROR_PIPE_BUSY occurs.

if (GetLastError() != ERROR_PIPE_BUSY)
cout <<"Could not open pipe";

// All pipe instances are busy, so wait for 20 seconds.

if (! WaitNamedPipe(lpszPipename, 20000) )
cout <<"Could not open pipe";
}

And I only get “Could not open pipe” from both CreateFile and WaitNamedPipe.
The error I get from GetLastError() is 2, "The system cannot find the file specified."

Andreas Masur
January 13th, 2006, 03:58 AM
Well...does the pipe already exists? Was it created with the same access flags (write and read)?

magol
January 13th, 2006, 04:05 AM
I can give it any name and I get the same error.

Andreas Masur
January 13th, 2006, 04:23 AM
Well...that still doesn't my questions....the name is okay... ;)

NoHero
January 13th, 2006, 09:03 AM
I can give it any name and I get the same error.

With your code you open an existing one, if this pipe is not created yet you receive error code 2. Either make sure you enter a name of a pipe that exists or you create a new one.