I'm using c as language with native win32 api
I created file "test.txt" it's contents are some file list in the
directory.
and used it as opening source for stdin.
then I changed cmdline to "dir"
does the example supposed to read file for stdin and send output to
parent's stdout?
when I run the program it holds output until i press ctrl-c
is the example program supposed to do that?
dir program supposedly finishes after listing files in the directory.
why is it hold until i press ctrl-c and force finish the dir program(child
process)?
here is modified example code i used.
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 4096
HANDLE hStd_IN_Rd = NULL;
HANDLE hStd_IN_Wr = NULL;
HANDLE hStd_OUT_Rd = NULL;
HANDLE hStd_OUT_Wr = NULL;
HANDLE hStd_ERR_Rd = NULL;
HANDLE hStd_ERR_Wr = NULL;
HANDLE hInputFile = NULL;
int main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
CreatePipe(&hStd_OUT_Rd, &hStd_OUT_Wr, &saAttr, 0);
CreatePipe(&hStd_ERR_Rd, &hStd_ERR_Wr, &saAttr, 0);
CreatePipe(&hStd_IN_Rd, &hStd_IN_Wr, &saAttr, 0);
char szCmdLine[] = TEXT("dir");
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = hStd_OUT_Wr;
siStartInfo.hStdOutput = hStd_OUT_Wr;
siStartInfo.hStdInput = hStd_IN_Rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL,
&siStartInfo, &piProcInfo);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
hInputFile = CreateFile( "test.txt", GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
for (;;) {
bSuccess = ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead,
NULL);
if ( ! bSuccess || dwRead == 0 ) break;
bSuccess = WriteFile(hStd_IN_Wr, chBuf, dwRead, &dwWritten, NULL);
if ( ! bSuccess ) break;
}
CloseHandle(hStd_IN_Wr);
bSuccess = FALSE;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
for (;;) {
bSuccess = ReadFile (hStd_OUT_Rd, chBuf, BUFSIZE, &dwRead,
NULL);
if ( ! bSuccess || dwRead == 0 ) break;
bSuccess = WriteFile(hStdOut, chBuf, dwRead, &dwWritten,
NULL);
if ( ! bSuccess ) break;
}
return 0;
}