2012-11-28 3 views
1

Java 프로세스를 실행하고 출력을 읽는 C++ 프로그램이 있습니다.C++ java 프로세스 출력 읽기

bSuccess = ReadFile(g_hInputFile, chBuf, BUFSIZE, &dwRead, NULL); 

bsucces 1에 해당 출력의 마지막 줄을 읽고, 그래서 프로그램이 다음 루프를 계속 때, 도달 그러나

http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx

: 나는 다음 MSDN 코드를 사용 같은 줄, 프로그램은 그냥 "파리", 예외없이, 디버깅을 중단, 중단 점 결코 다음 줄로 이동합니다.

멈춤 읽기를 나타내는 EOF가 없기 때문입니다. 그러나 에는 EOF 문자가 없습니다. 자바 프로그램은 단순히 다음을 수행합니다.

System.out.close(); 

끝에.

C++ \ Java 코드를 수정하려면 어떻게해야합니까?

편집 : 여기

는 코드입니다.

콘솔 응용 프로그램과 작동하지 않고 끝까지 읽은 다음 "중단"합니다.

HANDLE g_hChildStd_IN_Rd = NULL; 
HANDLE g_hChildStd_IN_Wr = NULL; 
HANDLE g_hChildStd_OUT_Rd = NULL; 
HANDLE g_hChildStd_OUT_Wr = NULL; 

HANDLE g_hInputFile = NULL; 
PROCESS_INFORMATION piProcInfo; 

void CreateChildProcess() 
// Create a child process that uses the previously created pipes for STDIN and STDOUT. 
{ 
    std::string d=processLoaction; 
    TCHAR *cmdline=new TCHAR[d.size()+1]; 
    cmdline[d.size()]=0; 
    std::copy(d.begin(),d.end(),cmdline); 
    STARTUPINFO siStartInfo; 
    BOOL bSuccess = FALSE; 

// Set up members of the PROCESS_INFORMATION structure. 

    ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION)); 

// Set up members of the STARTUPINFO structure. 
// This structure specifies the STDIN and STDOUT handles for redirection. 

    ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); 
    siStartInfo.cb = sizeof(STARTUPINFO); 
    siStartInfo.hStdError = g_hChildStd_OUT_Wr; 
    siStartInfo.hStdOutput = g_hChildStd_OUT_Wr; 
    siStartInfo.dwFlags |= STARTF_USESTDHANDLES; 

// Create the child process. 
    bSuccess = CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo); // receives PROCESS_INFORMATION 

    // If an error occurs, exit the application. 
    if (! bSuccess) 
     throw new Exception("Failed to create process"); 
    else 
    { 
     // Close handles to the child process and its primary thread. 
     // Some applications might keep these handles to monitor the status 
     // of the child process, for example. 

     CloseHandle(piProcInfo.hProcess); 
     CloseHandle(piProcInfo.hThread); 
    } 
} 

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,status; 
    CHAR chBuf[BUFSIZE]; 
    BOOL bSuccess = FALSE; 
    HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 
    BOOL res; 
    for (;;) 
    { 
     bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL); 
     if(! bSuccess || dwRead == 0) 
      break; 
     chBuf[dwRead] = NULL; 
     bSuccess = WriteFile(hParentStdOut, chBuf, dwRead, &dwWritten, NULL); 
     if (! bSuccess) 
      break; 
    } 

} 

int main(int argc, char** argv) 
{ 
      //run the Jar to validate manifest & jeff 
      SECURITY_ATTRIBUTES saAttr; 

      // Set the bInheritHandle flag so pipe handles are inherited. 
      saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
      saAttr.bInheritHandle = TRUE; 
      saAttr.lpSecurityDescriptor = NULL; 

      // Create a pipe for the child process's STDOUT. 
      if (! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)) 
       throw new Exception("faild to create a pipe for the child process's STDOUT"); 

      // Ensure the read handle to the pipe for STDOUT is not inherited. 
      if (! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)) 
       throw new Exception("Read handle to the pipe for STDOUT is inherited"); 
      CreateChildProcess(); 
      ReadFromPipe(); 
      ... 
} 
+0

EOF는 (는) 문자가 아닙니다. – melpomene

+0

올바른 파일/핸들을 읽고 계십니까? - 예제 코드에서, 데이터는'bSuccess = ReadFile (g_hChildStd_OUT_Rd, chBuf, BUFSIZE, & dwRead, NULL); '을 통해 자식 프로세스에서 읽혀진다. – JimmyB

+0

@HannoBinder 예, 모든 행이 성공적으로 읽히고 내 자신의 콘솔에 쓰여집니다. 마지막으로 나는 방금 "교수형"을 남겼습니다. – sara

답변

1

문제는 당신이 링크 된 페이지의 일부 의견에서 해결 될 것으로 보인다 : 당신이 자식 프로세스를 생성 한 후 파이프의 쓰기 측을 폐쇄해야하지만, 전에 다른 측면에서 읽기 시작. 그렇게하지 않으면 자식 프로세스가 출력을 닫더라도 파이프의 쓰기면이 여전히 부모 프로세스에서 열리고 EOF에 도달하지 못합니다.

아, 부모 프로세스의 모든 핸들을 닫는 것을 잊지 마십시오. 링크 된 페이지의 예는 그렇지 않으며 누출됩니다!

+0

쓰기 파이프를 사용하고 있지 않습니다 ... 읽기 전용 – sara

+0

@ 사라 : 사용하지 않더라도 핸들이 있으며 닫아야합니다. 쓰기 핸들이 열려있는 한 파이프 자체는 EOF로 표시되지 않습니다. – rodrigo