Capturing an external output process

Do not close it as duplicate, as I have a subtle but significant change from similar questions:

Is it possible to capture the output of an external process (i.e. stdout) in java, when I have not created a process , and all I know is the name of the process?

I am running Windows 7.

EDIT:

If there is a way to do this in another language (C # \ C ++), I can write the program "CaptureOutput", which will write the output, write to stdout and in my java code run "CaptureOutput" and read its stdput.

Ugly but can work.

So the answer in other languages ​​is also with me.

+8
java c ++ c # process stdout
source share
4 answers

First, let me say that what you ask for violates all the rules for isolating processes. If your process does not create the process whose output you want to capture, and you also do not have access to modify the calling process (shell services manager? You did not say which one). Then your only chance, and at best it’s subtle, is to insert a thread into the process, and while all the other threads are suspended, change the global stdout (and stderr?). This can only be done by a process with full access rights to the target process. Performing such an operation while the process is running is not for the faint of heart.

+1
source share

What you are trying to do is quite dangerous. It would be very easy to accidentally damage the memory of the process you are trying to penetrate. Test, test, test. Then check some more. And good luck - I know that I do not want this to be done.

This article, the Hooking API , explains how to get started with what you want (using C ++). After entering the code into the running process, there are other Windows API calls to replace STDOUT (for example, SetStdHandle ).

+1
source share

Do you have control over the process? If so, you can start this process and transfer it stdout to a file that can be read, or to another program you write that can write it to a database, event viewer, etc.

0
source share

On Linux, check the operating system's IPC mechanisms, such as message queues, pipes, shared memory, and sockets. These mechanisms allow interprocess communication . Although, if you are particularly interested in outputting a program, a workaround may simply cause the first process to output data to disk in a file and read using a separate process. Thus, you can use several languages ​​for the task. A simple example would be for C ++ to write some data to a file and use JAVA to read / use data, given the same file. I hope I come close to the answer, if at all.

0
source share

All Articles