S
Syed Babu
Guest
I have 2 processes A and B and using a unnamed pipe for communication.
Process A creates the pipe and write descriptor of the pipe is set to output handle of startup info while creating process B.
Process B get the standard output handle and calls WriteFile.
Process A calls for ReadFile on the read descriptor of the pipe to read the messages from Process B. And this happens in a while loop till the exit code of process B becomes not equal to STILL_ACTIVE.
This is my code of Process A.
HANDLE readHandle = NULL;
HANDLE writeHandle = NULL;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create pipe
CreatePipe(&readHandle, &writeHandle, &saAttr, 0);
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
// Pass the pipe handles to child process
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = writeHandle;
siStartInfo.hStdOutput = writeHandle;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
bSuccess = CreateProcess(NULL,
szCmdline,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&siStartInfo,
&piProcInfo);
const int BUFSIZE = 4096;
char buffer[BUFSIZE];
int processExitCode = STILL_ACTIVE;
while (processExitCode == STILL_ACTIVE)
{
int numBytesRead = 0;
ret = ReadFile( readHandle,
buffer,
BUFSIZE,
&numBytesRead,
NULL
);
buffer[numBytesRead] = 0;
GetExitCodeProcess(childproc, processExitCode);
}
I'm able to receive messages using ReadFile but I face an issue:
When I get the exit code of the process at the end of while loop it says the process is active but when I reach the ReadFile, process B was terminated and ReadFile call just hangs.How to handle this issue.?
Is there a way to make WriteFile is blocked till its read by ReadFile? I want to make sure that whatever Im writing is read and proceed.
Continue reading...
Process A creates the pipe and write descriptor of the pipe is set to output handle of startup info while creating process B.
Process B get the standard output handle and calls WriteFile.
Process A calls for ReadFile on the read descriptor of the pipe to read the messages from Process B. And this happens in a while loop till the exit code of process B becomes not equal to STILL_ACTIVE.
This is my code of Process A.
HANDLE readHandle = NULL;
HANDLE writeHandle = NULL;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create pipe
CreatePipe(&readHandle, &writeHandle, &saAttr, 0);
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
// Pass the pipe handles to child process
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = writeHandle;
siStartInfo.hStdOutput = writeHandle;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
bSuccess = CreateProcess(NULL,
szCmdline,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&siStartInfo,
&piProcInfo);
const int BUFSIZE = 4096;
char buffer[BUFSIZE];
int processExitCode = STILL_ACTIVE;
while (processExitCode == STILL_ACTIVE)
{
int numBytesRead = 0;
ret = ReadFile( readHandle,
buffer,
BUFSIZE,
&numBytesRead,
NULL
);
buffer[numBytesRead] = 0;
GetExitCodeProcess(childproc, processExitCode);
}
I'm able to receive messages using ReadFile but I face an issue:
When I get the exit code of the process at the end of while loop it says the process is active but when I reach the ReadFile, process B was terminated and ReadFile call just hangs.How to handle this issue.?
Is there a way to make WriteFile is blocked till its read by ReadFile? I want to make sure that whatever Im writing is read and proceed.
Continue reading...