How to redirect WriteFile func writes to console
-
Thank you for the example code. The effect is that output from my console app is redirected when this app quits, but not when this app is running.
Your statement is also somewhat nonsensical in that when my sample app quits it destroys the console you wouldn't see it (see WM_DESTROY). So I can only assume you mean your app you are running wont display until it exits???? So are you wanting live output keeping the app alive ??? You will have to spawn the thread before the call at the moment the program is CreateProcess Process output That is the app must complete it's setup to deal with normal DOS like command files. If it doesn't complete then the app will still be on the screen so why do you want it redirected to the console?????
In vino veritas
-
Thank you for the example code. The effect is that output from my console app is redirected when this app quits, but not when this app is running.
Okay so this spawns the thread before it executes the command passed in. It should spit the console even if the command doesn't terminate, which I still find strange.
#include
#include
#include
#includeFILE* OutStream;
FILE* InStream;
FILE* ErrStream;struct execData {
TCHAR* CmdLine;
TCHAR* CmdRunDir;
PROCESS_INFORMATION process_info;
STARTUPINFO startup_info;
};struct execData data = { 0 };
/*---------------------------------------------------------------------------
Application handler.
---------------------------------------------------------------------------*/
LRESULT CALLBACK GUIDemoHandler (HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CREATE:
AllocConsole(); //Allocate a console
freopen_s(&InStream, "CONIN$", "r", stdin); // Redirect standard in
freopen_s(&OutStream, "CONOUT$", "w", stdout); // Redirect standard out
freopen_s(&ErrStream, "CONOUT$", "w", stderr); // Redirect standard error
break;
case WM_DESTROY: // WM_DESTROY MESSAGE
if (data.process_info.hProcess) TerminateProcess(data.process_info.hProcess, 0);
FreeConsole();
PostQuitMessage(0); // Post quit message
break;default: return DefWindowProc(Wnd, Msg, wParam, lParam); // Default handler };// end switch case return 0;
}
DWORD WINAPI ExecThreadFunction(LPVOID lpParam)
{
int Success;
struct execData* data = (struct execData*)lpParam;
Success = CreateProcess(
nullptr,
data->CmdLine,
nullptr,
nullptr,
TRUE,
0,
nullptr,
data->CmdRunDir,
&data->startup_info,
&data->process_info
);if (!Success) { CloseHandle(data->process\_info.hProcess); CloseHandle(data->process\_info.hThread); return -4; } else { CloseHandle(data->process\_info.hThread); } WaitForSingleObject(data->process\_info.hProcess, INFINITE); CloseHandle(data->process\_info.hProcess); data->process\_info.hProcess = 0; return (0);
}
int SystemCapture(
TCHAR* CmdLine, //Command Line
TCHAR* CmdRunDir) //set to '.' for current directory
{
SECURITY_ATTRIBUTES security_attributes;data.CmdLine = CmdLine; data.CmdRunDir = CmdRunDir; security\_attributes.nLength = sizeof(SECURITY\_ATTRIBUTES); security\_attributes.bInheritHandle = TRUE; security\_attributes.lpSecurityDescriptor = nullptr; data.startup\_info.cb = sizeof(STARTUPINFO); data.startup\_info.hStdInput = 0; data.startu
-
Okay so this spawns the thread before it executes the command passed in. It should spit the console even if the command doesn't terminate, which I still find strange.
#include
#include
#include
#includeFILE* OutStream;
FILE* InStream;
FILE* ErrStream;struct execData {
TCHAR* CmdLine;
TCHAR* CmdRunDir;
PROCESS_INFORMATION process_info;
STARTUPINFO startup_info;
};struct execData data = { 0 };
/*---------------------------------------------------------------------------
Application handler.
---------------------------------------------------------------------------*/
LRESULT CALLBACK GUIDemoHandler (HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CREATE:
AllocConsole(); //Allocate a console
freopen_s(&InStream, "CONIN$", "r", stdin); // Redirect standard in
freopen_s(&OutStream, "CONOUT$", "w", stdout); // Redirect standard out
freopen_s(&ErrStream, "CONOUT$", "w", stderr); // Redirect standard error
break;
case WM_DESTROY: // WM_DESTROY MESSAGE
if (data.process_info.hProcess) TerminateProcess(data.process_info.hProcess, 0);
FreeConsole();
PostQuitMessage(0); // Post quit message
break;default: return DefWindowProc(Wnd, Msg, wParam, lParam); // Default handler };// end switch case return 0;
}
DWORD WINAPI ExecThreadFunction(LPVOID lpParam)
{
int Success;
struct execData* data = (struct execData*)lpParam;
Success = CreateProcess(
nullptr,
data->CmdLine,
nullptr,
nullptr,
TRUE,
0,
nullptr,
data->CmdRunDir,
&data->startup_info,
&data->process_info
);if (!Success) { CloseHandle(data->process\_info.hProcess); CloseHandle(data->process\_info.hThread); return -4; } else { CloseHandle(data->process\_info.hThread); } WaitForSingleObject(data->process\_info.hProcess, INFINITE); CloseHandle(data->process\_info.hProcess); data->process\_info.hProcess = 0; return (0);
}
int SystemCapture(
TCHAR* CmdLine, //Command Line
TCHAR* CmdRunDir) //set to '.' for current directory
{
SECURITY_ATTRIBUTES security_attributes;data.CmdLine = CmdLine; data.CmdRunDir = CmdRunDir; security\_attributes.nLength = sizeof(SECURITY\_ATTRIBUTES); security\_attributes.bInheritHandle = TRUE; security\_attributes.lpSecurityDescriptor = nullptr; data.startup\_info.cb = sizeof(STARTUPINFO); data.startup\_info.hStdInput = 0; data.startu
I am checking it using test app with WriteFile function and it works. But I will check it using target app tomorrow because it needs some hardware to work. Thanks
-
Okay so this spawns the thread before it executes the command passed in. It should spit the console even if the command doesn't terminate, which I still find strange.
#include
#include
#include
#includeFILE* OutStream;
FILE* InStream;
FILE* ErrStream;struct execData {
TCHAR* CmdLine;
TCHAR* CmdRunDir;
PROCESS_INFORMATION process_info;
STARTUPINFO startup_info;
};struct execData data = { 0 };
/*---------------------------------------------------------------------------
Application handler.
---------------------------------------------------------------------------*/
LRESULT CALLBACK GUIDemoHandler (HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CREATE:
AllocConsole(); //Allocate a console
freopen_s(&InStream, "CONIN$", "r", stdin); // Redirect standard in
freopen_s(&OutStream, "CONOUT$", "w", stdout); // Redirect standard out
freopen_s(&ErrStream, "CONOUT$", "w", stderr); // Redirect standard error
break;
case WM_DESTROY: // WM_DESTROY MESSAGE
if (data.process_info.hProcess) TerminateProcess(data.process_info.hProcess, 0);
FreeConsole();
PostQuitMessage(0); // Post quit message
break;default: return DefWindowProc(Wnd, Msg, wParam, lParam); // Default handler };// end switch case return 0;
}
DWORD WINAPI ExecThreadFunction(LPVOID lpParam)
{
int Success;
struct execData* data = (struct execData*)lpParam;
Success = CreateProcess(
nullptr,
data->CmdLine,
nullptr,
nullptr,
TRUE,
0,
nullptr,
data->CmdRunDir,
&data->startup_info,
&data->process_info
);if (!Success) { CloseHandle(data->process\_info.hProcess); CloseHandle(data->process\_info.hThread); return -4; } else { CloseHandle(data->process\_info.hThread); } WaitForSingleObject(data->process\_info.hProcess, INFINITE); CloseHandle(data->process\_info.hProcess); data->process\_info.hProcess = 0; return (0);
}
int SystemCapture(
TCHAR* CmdLine, //Command Line
TCHAR* CmdRunDir) //set to '.' for current directory
{
SECURITY_ATTRIBUTES security_attributes;data.CmdLine = CmdLine; data.CmdRunDir = CmdRunDir; security\_attributes.nLength = sizeof(SECURITY\_ATTRIBUTES); security\_attributes.bInheritHandle = TRUE; security\_attributes.lpSecurityDescriptor = nullptr; data.startup\_info.cb = sizeof(STARTUPINFO); data.startup\_info.hStdInput = 0; data.startu
This code is working. Now I will try to analyze outputs and build GUI. I am not sure if can I use the code in C# WPF application. If not I will try to build GUI using Win API starting from your examples. Thank you.
-
This code is working. Now I will try to analyze outputs and build GUI. I am not sure if can I use the code in C# WPF application. If not I will try to build GUI using Win API starting from your examples. Thank you.
It's a simple background worker thread it will work without issue in a C# WPF application. How to: Use a Background Worker[^] Inside the worker thread you want a standard command execute
static void runCommand() {
//* Create your Process
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c yourfile.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//* Set your output and error (asynchronous) handlers
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
//* Start process and handlers
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
//* Do your stuff with the output (write to console/log/StringBuilder)
Console.WriteLine(outLine.Data);
}In vino veritas
-
It's a simple background worker thread it will work without issue in a C# WPF application. How to: Use a Background Worker[^] Inside the worker thread you want a standard command execute
static void runCommand() {
//* Create your Process
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c yourfile.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//* Set your output and error (asynchronous) handlers
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
//* Start process and handlers
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
//* Do your stuff with the output (write to console/log/StringBuilder)
Console.WriteLine(outLine.Data);
}In vino veritas
I do simple test and problem is the same as before. Only when console app quits OutputHandler is executed and then I can read every line by line that app prints during running. Furthermore, it seems that StandardInput is not redirected because click on the button does not quit the app. App quits when 'e' key is pressed in the console window.
public partial class MainWindow : Window { Process process = new Process(); public MainWindow() { InitializeComponent(); runCommand(); } private void button\_Click(object sender, RoutedEventArgs e) { textBox.Text += "test\\r\\n"; process.StandardInput.Write('e'); } void runCommand() { //\* Create your Process process.StartInfo.FileName = "c:\\\\ConsoleApp.exe"; process.StartInfo.Arguments = "."; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; //\* Set your output and error (asynchronous) handlers process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); //\* Start process and handlers process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); // process.WaitForExit(); } void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { //\* Do your stuff with the output (write to console/log/StringBuilder) Console.WriteLine(outLine); } }
-
I do simple test and problem is the same as before. Only when console app quits OutputHandler is executed and then I can read every line by line that app prints during running. Furthermore, it seems that StandardInput is not redirected because click on the button does not quit the app. App quits when 'e' key is pressed in the console window.
public partial class MainWindow : Window { Process process = new Process(); public MainWindow() { InitializeComponent(); runCommand(); } private void button\_Click(object sender, RoutedEventArgs e) { textBox.Text += "test\\r\\n"; process.StandardInput.Write('e'); } void runCommand() { //\* Create your Process process.StartInfo.FileName = "c:\\\\ConsoleApp.exe"; process.StartInfo.Arguments = "."; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; //\* Set your output and error (asynchronous) handlers process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); //\* Start process and handlers process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); // process.WaitForExit(); } void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { //\* Do your stuff with the output (write to console/log/StringBuilder) Console.WriteLine(outLine); } }
Yes because you didn't put it in a worker thread :-) You need runCommand() to be in a worker thread .. it has to execute in the background so you can keep the WPF framework running in the foreground. Look back at the windows code I had to do the same thing the CreateProcess(...) is inside a function which is spawned into it's own thread via CreateThread. Unfortunately I don't use C# enough to know the code for making a worker thread off the top of my head. I actually don't have the WPF framework installed on my VisualStudio at the moment to work it out. Basically however we can describe it 1.) everything in runCommand() needs to go inside the worker thread start function .. when the thread starts it runs the app 2.) When the app finishes or your WPF app finishes you need to close the worker thread (LOOK at WM_DESTROY on the windows code) So basically this worker thread will be chugging along in the background running the app but your program comes back to you to continue on to run the WPF framework. So you job is to work out how to make a WPF worker thread and incorporate the code :-)
In vino veritas
-
I do simple test and problem is the same as before. Only when console app quits OutputHandler is executed and then I can read every line by line that app prints during running. Furthermore, it seems that StandardInput is not redirected because click on the button does not quit the app. App quits when 'e' key is pressed in the console window.
public partial class MainWindow : Window { Process process = new Process(); public MainWindow() { InitializeComponent(); runCommand(); } private void button\_Click(object sender, RoutedEventArgs e) { textBox.Text += "test\\r\\n"; process.StandardInput.Write('e'); } void runCommand() { //\* Create your Process process.StartInfo.FileName = "c:\\\\ConsoleApp.exe"; process.StartInfo.Arguments = "."; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; //\* Set your output and error (asynchronous) handlers process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); //\* Start process and handlers process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); // process.WaitForExit(); } void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) { //\* Do your stuff with the output (write to console/log/StringBuilder) Console.WriteLine(outLine); } }
If you want a total guess looking at the MSDN reference and your code but ignoring the exit problem ;-)
using System.Threading;
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
runCommand();
}).Start(); -
If you want a total guess looking at the MSDN reference and your code but ignoring the exit problem ;-)
using System.Threading;
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
runCommand();
}).Start();Running runCommand() from new Thread didn't help. I can read all outputs after console app quits. It is harder to build GUI using WinAPI than using WPF but WinAPI code works.
-
Running runCommand() from new Thread didn't help. I can read all outputs after console app quits. It is harder to build GUI using WinAPI than using WPF but WinAPI code works.
Okay the problem is console.writeline is blocking C# test.net » Using Process.Start to capture console output[^] .net - Does Console.WriteLine block? - Stack Overflow[^] You can use one of the methods or Console.Out.WriteLineAsync("..."); if available. For us old timers it's actually faster to build a GUI with the API than with the WPF framework :-)
In vino veritas
-
Okay the problem is console.writeline is blocking C# test.net » Using Process.Start to capture console output[^] .net - Does Console.WriteLine block? - Stack Overflow[^] You can use one of the methods or Console.Out.WriteLineAsync("..."); if available. For us old timers it's actually faster to build a GUI with the API than with the WPF framework :-)
In vino veritas
OK, I know what you mean :) For me, it is easier to use WPF because I am embedded system engineer. I am from embedded world and I do not know Windows internals. That is why I am asking a lot of (stupid) questions. After reading several articles I supposed that redirect output and develop GUI app should be easier :) Now I know that my strange console app does not use standard printf function :((
-
OK, I know what you mean :) For me, it is easier to use WPF because I am embedded system engineer. I am from embedded world and I do not know Windows internals. That is why I am asking a lot of (stupid) questions. After reading several articles I supposed that redirect output and develop GUI app should be easier :) Now I know that my strange console app does not use standard printf function :((
The obvious question is can you not simply replace the program communicating with a proper windows code or is it a private format they won't disclose. Just saying it would be faster, cleaner and less code than what you are doing now :-)
In vino veritas
-
The obvious question is can you not simply replace the program communicating with a proper windows code or is it a private format they won't disclose. Just saying it would be faster, cleaner and less code than what you are doing now :-)
In vino veritas
Yes, it is a private closed format. Another way to communicate with the console app is passing command line arguments:
MyApp.exe -Iinput.txt -Ooutput.txt
where in the input.txt file in each line is a key command (e - exits the program, other keys do some actions): l f m c o e in the output.txt file, the program saves its output. Even something like this works: MyApp.exe -OCON - prints output to console twice. But problem is the same. If I monitor the output file, the content appears after app quits (e command in input file)
-
Yes, it is a private closed format. Another way to communicate with the console app is passing command line arguments:
MyApp.exe -Iinput.txt -Ooutput.txt
where in the input.txt file in each line is a key command (e - exits the program, other keys do some actions): l f m c o e in the output.txt file, the program saves its output. Even something like this works: MyApp.exe -OCON - prints output to console twice. But problem is the same. If I monitor the output file, the content appears after app quits (e command in input file)
This smells to me of buffering. I'm guessing your app doesn't call flush() or equivalent on its output file, so the output is buffered until the explicit or implicit close() call at termination. If you can make your app output lots of data, you will probably see it appear in chunks, which will be whatever buffer size is used. The reason it works on the console is that console drivers do not buffer. I have no idea if you can suppress buffering in your redirection, but if you could, my 2c says that would solve your problem. Good luck! Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
This smells to me of buffering. I'm guessing your app doesn't call flush() or equivalent on its output file, so the output is buffered until the explicit or implicit close() call at termination. If you can make your app output lots of data, you will probably see it appear in chunks, which will be whatever buffer size is used. The reason it works on the console is that console drivers do not buffer. I have no idea if you can suppress buffering in your redirection, but if you could, my 2c says that would solve your problem. Good luck! Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
Yes standard console output is always buffered. You can actually easily remove it
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);The problem is it will slow the console program down like a dog as each character invokes a full call. I sometimes do it when I have a embedded target with UART debugger like on the Raspberry Pi.
In vino veritas