void WriteToPipe(void)
// Read from a file and write its contents to the pipe for the child's STDIN.
// Stop when there is no more data.
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
char * name = malloc(100);
fgets(name, 100, stdin);
bSuccess = WriteFile(g_hChildStd_IN_Wr, name, 10, &dwWritten, NULL);
if (!bSuccess)
ErrorExit("");
}
void ReadFromPipe(void)
// Read output from the child process's pipe for STDOUT
// and write to the parent process's pipe for STDOUT.
// Stop when there is no more data.
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if (!bSuccess || dwRead == 0)
return 100;
bSuccess = WriteFile(hParentStdOut, chBuf,
dwRead, &dwWritten, NULL);
if (!bSuccess)
return 101;
}
The main (not full):
while (1)
{
Sleep(500);
ReadFromPipe();
WriteToPipe();
}
I am trying to open the cmd as a child process and to pass the parent input to the child STDIN stream, and than to print the STDOUT of the child.
As you can see, it works at the first time but then I get this "more?" back from the child process (the cmd) and then it gets stuck waiting for output.
Why am I getting this "more?" back?
What is that "more?"
You'll create processes with the child_process module by retrieving the results of a child process via a buffer or string with the exec() function, and then from a data stream with the spawn() function. You'll finish by using fork() to create a child process of another Node.
stdin property is an inbuilt application programming interface of the process module which listens for the user input. The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.
Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.
The only way of the redirection of parent input which I found - is creation an additional thread. The common algorithm is:
hPipeRead
, hPipeWrite
)hPipeRead
GetStdHandle(STD_INPUT_HANDLE)
and is writing read buffer to hPipeWrite
immediately. Thread is completed when stdInput is over.This way is described in Microsoft support article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With