2017-02-08 3 views
1

편집 : 해리 존스턴 (Harry Johnston)의 제안에 따라 수정 된 기능으로 Child_In_Write 핸들을 닫습니다.
다소 아이러니하게도, 이전에 Child_In_Read 핸들을 닫으려고했습니다. 이 작동하지 않는, 쓰기 핸들을 닫아야 할 유일한 것입니다. 내가생성 된 자식 프로세스에 의해 무시되는 리디렉션 된 stdin 파이프

간단한 충분히 생각을 배관 명령 줄을 통해 호출 된 것처럼 - 내가 만들려고 도구

, 나는 프로세스를 시작하고 표준 입력을 통해 그것을 데이터를 제공 할 수 있어야합니다. I've primarily I've followed this guide from Microsoft and got things "working". 내 자신의 테스트 프로그램에서 나는 정상적으로 stdin에서 읽을 수 있습니다. 그러나 예를 들어 고양이처럼 다른 프로그램을 사용하려고 할 때, 그들은 여전히 ​​입력을 기다리는 것처럼 멈추지 않습니다. 여기

The full repo is here.

는 관련 코드 비트입니다

는 파이프를 초기화합니다.

방금 ​​초기화 한 파이프에 쓰고 프로세스를 시작하십시오. 잘 작동하는 표시

// From Cao/Main.cpp 
    // Write to the processes' standard in. 
    { 
     DWORD inBytesWritten = 0; 
     bool writeSuccess = 
      WriteFile(
       Child_In_Write, 
       text,    // Simple char array. 
       text_numBytes, 
       &inBytesWritten, 
       NULL); 
     if (!writeSuccess) 
     { 
      // @logging log error. 
      printf("Could not write to child's standard in!\n"); 
      goto textData_cleanup; 
     } 
    } 

    // Create the child process. 
    { 
     STARTUPINFO startupInfo = { 0 }; 
     startupInfo.cb   = sizeof(startupInfo); 
     startupInfo.hStdInput = Child_In_Read; 
     startupInfo.hStdError = Child_Out_Write; 
     startupInfo.hStdOutput = Child_Out_Write; 
     startupInfo.dwFlags = STARTF_USESTDHANDLES; 

     bool createProcessSuccess = CreateProcessW(
      NULL, 
      commandLine, 
      NULL, 
      NULL, 
      true, 
      0, 
      NULL, 
      NULL, 
      &startupInfo, 
      &ChildProcInfo);   
     if (!createProcessSuccess) 
     { 
      printf("Could not start child process with command line: %ls", commandLine); 
      goto textData_cleanup; 
     } 

     isChildRunning = true; 
     ModifyMenu(IconMenu, IconMenu_RunCancel, MF_BYCOMMAND, IconMenu_RunCancel, L"Cancel"); 

     // newHandle is always 0x00000000 so I'm assuming I don't need to clean it up. 
     HANDLE newHandle; 
     RegisterWaitForSingleObject(&newHandle, ChildProcInfo.hProcess, LaunchedProcessExitedOrCancelled, NULL, INFINITE, WT_EXECUTEONLYONCE); 
    } 

내 독서 코드 :

// From Echoer/Main.cpp 
printf("via stdin:\n"); 
{ 
    const int readBuffer_size = 5000; 
    char *readBuffer[readBuffer_size]; 

    { 
     HANDLE standardIn = GetStdHandle(STD_INPUT_HANDLE); 
     DWORD bytesRead = 0; 

     bool readSuccess = 
      ReadFile(
       standardIn, 
       readBuffer, 
       readBuffer_size, 
       &bytesRead, 
       NULL); 
     if (!readSuccess) 
     { 
      printf("Could not read from standard in!\n"); 
     } 

     CloseHandle(standardIn); 
    } 

    printf("%s", readBuffer); 
} 

I가 전송 될 필요가 뭔가를 놓치고 있습니까 "볼이 굴러 얻을"? "\ r \ n"또는 이와 비슷한 것을 추가해야합니까? 포탄은 어떻게 이것을 처리합니까?

+0

당신이 연습을 위해 그것을 할 경우 괜찮아. 일을 끝내고 싶다면 boost.process (공식적인 부스트가 아닌)를 사용하고 시간을 절약하십시오 :) –

+0

@HarryJohnston 파이프를 닫으면 효과가 있습니다! 정말 고마워요,이게 제 정신이 아니에요! 내 소식을 업데이트하겠습니다. 나는 당신에게 당신의 잘 맞는 점수를 줄 수 있도록 대답해야합니다. –

답변

0

cat을 포함한 일부 응용 프로그램은 종료하기 전에 표준 입력에서 파일 끝을 기다립니다. 파이프의 끝을 닫음으로써이를 수행 할 수 있습니다. (파이프의 최종 핸들이 아이 또는 다른 프로세스에 의해 상속하지만, 코드가 이미 올바르게 처리되지 않았 음을 당신은 또한 특정해야합니다.)

관련 문제