I'm trying to figure out how to read the standard out/err from the process I've created with CreateProcessW. I looked at the docs, googled and searched this list but I didn't find good pointers/samples yet :)
Here's what I came up with so far (it's working fine on windows, it's a relevant snippet from my java code):
Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
Kernel32.StartupInfo startupInfo = new Kernel32.StartupInfo();
Kernel32.ProcessInfo processInformation = new Kernel32.ProcessInfo();
if (!kernel32.CreateProcessW(null, new WString(command), null, null, false,
DETACHED_PROCESS, null, new WString(dir.getAbsolutePath()), startupInfo,
processInformation)) {
throw new IOException("Could not start process. Errno: " +
kernel32.GetLastError());
}
kernel32.CloseHandle(processInformation.hProcess);
kernel32.CloseHandle(processInformation.hThread);
So... how can I grab the output from that process? Anyone has done that already and care sharing a sample?
Thanks guys for any help in advance.
To write to a console for a process created with
CreateProcess
function, MSDN suggests to create a child process and use anonymous pipes to redirect child process's standard input and output handles.Creating a Child Process with Redirected Input and Output
Since JNA 3.3.0 Platform hasn't include all the Kernel32 functions we need, we need to provide the required JNA interfaces as follows: (NOTE JNA 4.0 provides Kernel32 for you)
Kernel32.java:
Then, the main part:
RunTest.java:
The original MSDN program only asks for one argument. But, the modified Runtest java program will need two arguments: (1) the command line; (2) the input file.
Example usage:
Example output of the program:
If you want to see an elaborate version... WindowsXPProcess.java