Hi everyone.
I'm doing a project related to named pipe. I used CreatePipe to create a new named pipe, and then, another process will read content of pipe. In this case, I only want Process 1 create a pipe with Write permission (I don't want it to have READ permission) and Process 2 will have READ permission only.
In process 1, I use this Function:
hPipe = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_OUTBOUND
, // write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
0, // client time-out
NULL);
In Process 2, I use CreateFile function to open Pipe, like this:
hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ
, // read access
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
The problem is, I cannot change to message_read mode in process 2, it always have error and exit
dwMode = PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
if ( ! fSuccess) //fSuccess is always FALSE, I don't understand this
{
_tprintf( TEXT("SetNamedPipeHandleState failed. GLE=%d\n"), GetLastError() );
return -1;
}
Thank you very much for paying attention to this question