Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:32:11+00:00 2026-06-15T01:32:11+00:00

I have a C++ program that runs a java process and reads it’s output.

  • 0

I have a C++ program that runs a java process and reads it’s output.
I used the following MSDN code:

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

However, when reading the final line of the output

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

bsucces equals 1, so the program continue to the next loop, and when reaching the same line, the program just “fly”, with no exception, stopping to debug, the breakpoint never moves to then next line.

I guess this is because there is no EOF to indicate stop reading.
However, java do not have a EOF charcater.
The java program simply does:

System.out.close();

at end.

What can I do in the C++\Java code in order to fix it?

EDIT:

Here is the code.

It doesn’t work with any console application, read to end and then “hang”.

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();
            ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:32:13+00:00Added an answer on June 15, 2026 at 1:32 am

    The problem seems to be addressed in some comments of the page you linked: you have to close the writing side of the pipe after creating the child process, but before starting reading from the other side. If you fail to do that, even if the child process closes its output, the writing side of the pipe is still opened (by the parent process) and so the EOF is never reached.

    Ah and don’t forget to close all the handles of the parent process. The example in the linked page does not, and it leaks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java program that runs another (Python) program as a process. Process
I have a Java program that runs many small simulations. It runs a genetic
I have a Java program that runs on my Ubuntu 10.04 machine and, without
On Mac OSX 5.8 I have a Java program that runs at 100% CPU
I have a Java program that runs in the background without any UI. Can
I have successfully created a Java program that runs fine on my development computer,
I have a java program in that I am using mysql database connectivity code.
I have a Java program that runs a number of other programs. Once the
Sorry, Im sort of new to java, and I have a program that runs
So I have a program that runs something like the following public class SHandler

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.