Windows API - CreateFile() with OPEN_EXISTING set returns error code of ERROR_ALREADY_EXISTS (183)

StackOverflow https://stackoverflow.com/questions/14883547

Pregunta

I am trying to open a named pipe using CreateFile():

g_hPipe = CreateFile(szPipeName, 
                    GENERIC_READ | GENERIC_WRITE, 
                    0, 
                    NULL, 
                    OPEN_EXISTING, 
                    FILE_FLAG_OVERLAPPED, 
                    NULL);

where szPipeName looks like \\\\.\\pipe\\service_name, and I am getting INVALID_HANDLE_VALUE returned.

According to the MSDN, the OPEN_EXISTING flag only returns an error if the file doesn't exist, yet GetLastError() shows ERROR_ALREADY_EXISTS (183). I know the file exists, that's why I'm using OPEN_EXISTING... The MSDN suggests this only occurs when OPEN_ALWAYS or CREATE_ALWAYS are set. I call SetLastError(0) immediately before CreateFile() to make sure the GetLastError() result is referring to the CreateFile() function.

I've searched quite a bit and haven't found anything to suggest why I am receiving this error. Any help is appreciated.

¿Fue útil?

Solución

The docs indicate you can only CreateFile once in a single client, after that you have to duplicate the returned handle:

If CreateFile opens the client end of a named pipe, the function uses any instance of the named pipe that is in the listening state. The opening process can duplicate the handle as many times as required, but after it is opened, the named pipe instance cannot be opened by another client.

Pipes are a point-to-point transport. If you want other apps to connect to your server, they will need their own pipe instance, which means another CreateNamedPipe call on the server.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top