ConnectNamedPipe
The ConnectNamedPipe
function enables a named pipe server process to wait for a client process to
connect to an instance of a named pipe. A client process connects by calling
either the CreateFile
or CallNamedPipe
function.
BOOL ConnectNamedPipe(
|
HANDLE hNamedPipe, |
// handle to named
pipe to connect |
|
LPOVERLAPPED lpOverlapped |
// pointer to
overlapped structure |
|
); |
|
Parameters
hNamedPipe
Identifies
the server end of a named pipe instance. This handle is returned by the CreateNamedPipe function.
lpOverlapped
Points to an OVERLAPPED structure.
Return Values
If the
function succeeds, the return value is nonzero.
If the
function fails, the return value is zero. To get extended error information,
call GetLastError.
Remarks
A named pipe
server process can use ConnectNamedPipe with a newly created pipe
instance. It can also be used with an instance that was previously connected to
another client process; in this case, the server process must first call the DisconnectNamedPipe function to disconnect the
handle from the previous client before the handle can be reconnected to a new
client. Otherwise, ConnectNamedPipe returns FALSE, and GetLastError
returns ERROR_NO_DATA if the previous client has closed its handle or
ERROR_PIPE_CONNECTED if it has not closed its handle.
The behavior
of ConnectNamedPipe depends on two conditions: whether the pipe handle s
wait mode is set to blocking or nonblocking and whether the function is set to
execute synchronously or in overlapped mode. A server initially specifies a
pipe handle s wait mode in the CreateNamedPipe21DZYT function, and it can be changed by using
the SetNamedPipeHandleState
function.
If hNamedPipe
was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must
not be NULL. It must point to a valid OVERLAPPED structure. If hNamedPipe
was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the
function can incorrectly report that the connect operation is complete.
If hNamedPipe
was created with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the OVERLAPPED structure pointed to by lpOverlapped
must contain a handle to a manual-reset event object (which the server can
create by using the CreateEvent function).
If hNamedPipe
was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the
function does not return until a client is connected or an error occurs.
Successful synchronous operations result in the function returning TRUE if a
client connects after the function is called. If a client connects before the
function is called, the function returns FALSE and GetLastError returns
ERROR_PIPE_CONNECTED. This can happen if a client connects in the interval
between the call to CreateNamedPipe and the call to ConnectNamedPipe.
In this situation, there is a good connection between client and server, even
though the function returns FALSE.
If hNamedPipe
was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL,
the operation executes asynchronously. The function returns immediately with a
return value of FALSE. If a client process connects before the function is
called, GetLastError returns ERROR_PIPE_CONNECTED. Otherwise, GetLastError
returns ERROR_IO_PENDING, which indicates that the operation is executing in
the background. When this happens, the event object in the OVERLAPPED structure is set to the nonsignaled
state before ConnectNamedPipe returns, and it is set to the signaled
state when a client connects to this instance of the pipe.
The server
process can use any of the wait functions or SleepEx to determine when the state of the event object is
signaled, and it can then use the GetOverlappedResult function to determine the
results of the ConnectNamedPipe operation.
If the
specified pipe handle is in nonblocking mode, ConnectNamedPipe always
returns immediately. In nonblocking mode, ConnectNamedPipe returns TRUE
the first time it is called for a pipe instance that is disconnected from a
previous client. This indicates that the pipe is now available to be connected
to a new client process. In all other situations when the pipe handle is in
nonblocking mode, ConnectNamedPipe returns FALSE. In these situations, GetLastError
returns ERROR_PIPE_LISTENING if no client is connected, ERROR_PIPE_CONNECTED if
a client is connected, and ERROR_NO_DATA if a previous client has closed its
pipe handle but the server has not disconnected. Note that a good connection
between client and server exists only after the ERROR_PIPE_CONNECTED error is
received.
Note that
nonblocking mode is supported for compatibility with Microsoft LAN Manager
version 2.0, and it should not be used to achieve asynchronous input and output
(I/O) with named pipes.
See Also