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

  • Home
  • SEARCH
  • 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 8179185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:56:46+00:00 2026-06-06T23:56:46+00:00

How can I send string data from Java to a C++ console application under

  • 0

How can I send string data from Java to a C++ console application under Windows? I am trying to do this:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
String o = ...;
proc.getOutputStream().write(o.getBytes());

But I never see it on the C++ side when I do this:

ReadFile(stdin_h,buf, sizeof(buf), &bytes, 0)

ReadFile never returns.

What follows is further elaboration and sample code.


I have written a simple C++ console (Win32) application which reads from STDIN and performs actions based on input.

Now I want to write a Java application to “drive” the C++ application. The Java applicaton should:

  1. Start the C++ application using Runtime.exec()
  2. Write string data to the C++ app’s STDIN
  3. Repeat until it’s time to die.

My Java application seems to be working, but the C++ application never receives any data on STDIN.

Here is the C++ application:

int main()
{
    ofstream f("c:\\temp\\hacks.txt");

    HANDLE stdin_h = GetStdHandle(STD_INPUT_HANDLE);
    DWORD file_type = GetFileType(stdin_h);
    if( file_type != FILE_TYPE_CHAR )   
        return 42;

    f << "Pipe" << endl;
    for( bool cont = true; cont; )
    {
        char buf[64*1024] = {};
        DWORD bytes = 0;
        if( ReadFile(stdin_h,buf, sizeof(buf), &bytes, 0) )
        {
            string in(buf,bytes);
            cout << "Got " << in.length() << " bytes: '" << in << "'" << endl;
            f << "Got " << in.length() << " bytes: '" << in << "'" << endl;
            if( in.find('Q') )
                cont = false;
        }
        else
        {
            cout << "Err " << GetLastError() << " while reading file" << endl;
            f << "Err " << GetLastError() << " while reading file" << endl;
        }
    }
}

And here is the Java side:

public static void main(String[] args) {
    Runtime rt =Runtime.getRuntime();
    try {
        Process proc = rt.exec("c:\\dev\\hacks\\x64\\debug\\hacks.exe");

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));

        int a = 0;
        while(a < 5)
        {
            String o = (a == 4 ? "Q\n" : "A\n");
            proc.getOutputStream().write(o.getBytes());
            System.out.println("Wrote '" + o + "'");
            ++a;
        }
        try {
            proc.waitFor();

            // TODO code application logic here
        } catch (InterruptedException ex) {
            Logger.getLogger(Java_hacks.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (IOException ex) {
        Logger.getLogger(Java_hacks.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The Java side seems to be working correctly, but I’m not ever receiving the strings on the C++ side.

Am I doing something wrong here? How can I send string data from Java to a C++ console application under Windows?

  • 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-06T23:56:47+00:00Added an answer on June 6, 2026 at 11:56 pm

    Why are you not flushing the output stream on the Java side after writing 5 strings?

    proc.getOutputStream().flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send a very basic XML string from a Windows Phone application
I can only send string or numeric values,how to send an array?
In Javascript I can send XML string to JSP server (XmlAction.jsp): Javascript Code: var
In fortran, I can declare a character string as: character*80 mystring and then send
How this UIButton rewind can send notification to UIButton play to perform playpauseaction method
how i can send key from my C# program to external program ? ex:
how i can send sms from asp.net?any idea about it?or any resource through which
I like to ask, how I can read data from my databast test_database.db and
I have to send the audio data in byte array obtain by recording from
my target is it to collect data from clients which send data to an

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.