C
C
Curly Brace2015-09-21 19:35:02
Programming
Curly Brace, 2015-09-21 19:35:02

How to read from console window if it doesn't output anything to stdout?

Question on C# WinForms (just in case)
Half a day I've been fiddling with launching a console application program from the GUI, so that I would get the text output line by line.
With the help of Process I start the process, I hang handlers on DataRecieved.
For half a day I could not understand why I did not receive anything in response, now I tried it with the standard ipconfig and O MIRACLE!
I concluded that the program I need outputs text to the console in a non-standard way, like I heard about printf from the author, but I'm not sure.
How to be? I really need to parse what the program will output during operation)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem Voronov, 2015-09-21
@newross

DLL-injection and go, parse the memory of the console program.

A
AxisPod, 2015-09-22
@AxisPod

Where do you think printf outputs? Maybe output to stderr?

N
nuclearthinking, 2015-09-22
@nuclearthinking

Poking around in one interesting application, I came across something that might suit you
, the method executes a console command

public static int runProcess(File workDir, String... command) throws Exception {
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.directory(workDir);
        pb.environment().put("JAVA_HOME", System.getProperty("java.home"));
        if(!pb.environment().containsKey("MAVEN_OPTS")) {
            pb.environment().put("MAVEN_OPTS", "-Xmx1024M");
        }

        Process ps = pb.start();
        (new Thread(new Builder.StreamRedirector(ps.getInputStream(), System.out))).start();
        (new Thread(new Builder.StreamRedirector(ps.getErrorStream(), System.err))).start();
        int status = ps.waitFor();
        if(status != 0) {
            throw new RuntimeException("Error running command, return status !=0: " + Arrays.toString(command));
        } else {
            return status;
        }
    }

the StreamRedirector method used in the previous method
private static class StreamRedirector implements Runnable {
        private final InputStream in;
        private final PrintStream out;

        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(this.in));

            try {
                String ex;
                while((ex = br.readLine()) != null) {
                    this.out.println(ex);
                }

            } catch (IOException var3) {
                throw Throwables.propagate(var3);
            }
        }

        @ConstructorProperties({"in", "out"})
        public StreamRedirector(InputStream in, PrintStream out) {
            this.in = in;
            this.out = out;
        }
    }

try to explore the libraries that are used by this method,
I’m afraid that I won’t be able to provide you with a ready-made solution, I’m still very poorly versed in Java

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question