How to limit the buffer size of a pipe (windows)? - windows

I am trying to control and read the output of a 3rd party console application, of which source code I cannot change.
I want to use QProcess for this, but this should not matter, as the issue is the same when just using cmd:
The 3rd party app seems to never call flush().
Therefore, directly calling it in cmd.exe works fine(Output appears in cmd window), but when calling e.g.
3rdPartyApp.exe > Output.txt
Output.txt stays empty until 3rdPartyApp.exe terminates or quits.
After 3rdPartyApp.exe quits or is terminated, all stdout can be found in Output.txt .
Question:
What can I do to create an environment where the buffer size of the pipe is limited, like when calling directly in cmd.exe, which seems to limit the buffer size to one line?

What you can do is create your own Console type application that will run 3rd party process and "handle" buffering of it.
Then instead redirecting output of 3rd party application, you simply redirect output of proxy console app.
How to do it? You can read it here: https://www.codeproject.com/Articles/16163/Real-Time-Console-Output-Redirection
The author provides explaination and ready to use Console app called RTConsole.exe.
I had big time issue to get unbuffered output from 3rd party pyhton executable spawned from my .NET app and this RTConsole.exe saved me.

Related

Redirect windows console output from another process to file

I have a DLL which does as plugin into the TS3 client.
The problem is, the plugin provokes a crash that I cannot seem to find in my code directly.
So my idea is to set the Console Output of the console windows in the background (-console option) to a file, because when the program crashes there is no way I can read the console output as the console disappears immediately.
Is there a way to set the output of a crashing console to a file?
Because so far, when using the stdout operator (ts3client_win64 -console > output.txt) it does not write anything to the file. (I assume it cannot close the file handle and loses all its data?) But I want to keep the console output when the crash occurs.
It also has to be said that I can not just run it with a batch file and a pause statement, because when starting the application it opens its own console window (and thats the one of which I want the output).
It is the type of crash one would get from failed (safe) string operations in C like strtok_s or strcpy_s.

Resize external command line window

What is better way to change size of command line windows in next case:
I started cmd process from my application
I set my widget as parent of cmd window
Now I need to resize command line window after widget resize event. But I have maximal size setted within my application.
Most examples used hStdOut of console, but how can (if can) I get it by cmd process id/window handle (now I haven't any other data)?
Also, sending of mode command in window is unsuitable - user can start any programm in command line window (e.g. run ssh client), so I can not be sure that command really will started.
Maybe that's an overkill, but why not to inject DLL which will use standard API to resize process console? If you started the process as you stated, you have process handle, so you can WaitForInputIdle and then Inject DLL which will call SetConsoleWindowInfo from it's DllMain. DLL can be very tiny, 1Kb or so without CRT.

How can I read the stderr of a GUI process that is launched by another process on Windows?

I want to see the text a program prints to stderr.
I can't see it because the program can only be launched by its launcher so nothing is shown when it's run from the command prompt; the program runs and then the command prompt immediately returns for input instead of blocking and displaying the output.
Can you think of any technique, even an ugly hack, that will let me read what's being printed to stderr?
I have a few ideas but I don't know how to carry them out easily:
Read the memory of the process at the right location
Modify the memory of the process to change the stderr handle to one I can read
Hook the API calls that write to stderr
Hook CreateProcess of the launcher and change the output handles

How do you display php output into phpstorm's console?

Once a debugger (xdebug) is enabled in phpstorm, you can then run your php script (without debugging) from within phpstorm.
How do you view the output of the script in phpstorms console?
I want to see php's output before it gets read by the browser - basically what you would see if you clicked "view source" in the browser.
Here is a video of a jetbrains dev. doing this (2mins:05sec into video) - he just doesnt explain HOW to configure phpstorm to do it.
Currently there is no such option in PhpStorm: to show web server buffer (what was sent to the browser) when debugging web page (note: this should work ONLY when debugging).
Such option did exist for a very short period about 4 years ago (separate Script Output tab in the debugger tool window: http://youtrack.jetbrains.com/issue/WI-2826 ).. but had quite a few issues and it was removed until properly implemented.
http://youtrack.jetbrains.com/issue/WI-18214 --> http://youtrack.jetbrains.com/issue/WI-4466
On that video, especially around 2:05 as you have suggested, ordinary CLI script is executed (Run/Debug Configuration of PHP Script type is used) and therefore ALL such output can be easily caught (standard std output).
With CLI scipt it's easy -- IDE is the parent here (he one who initiated script execution) so std output can easily be collected. If you execute your web page script in CLI environment, you will get your HTML in normal console output.
With web page it works differently (the whole process): script output is first sent to the web server (parent) which in turn sends it to the browser. Since IDE does not directly participate in script execution, such interception can only happen during debugging where debugger can send a copy of collected script output back to IDE.
Most web frameworks buffer the output before sending the results to the webserver. To view the contents of this buffer, first insert a breakpoint at the desired location, then when the breakpoint is reached, then goto the Debug=>Console tab and execute the ob_get_contents() function. PHP's output buffering supported nested levels, so keep this in mind when choosing your breakpoint. Obviously if the CMS/framework doesn't use output buffering then this method doesn't apply.

write to stdout with win32 gui app

If a win32 GUI app is run from the command line
e.g gfxexe.exe
first what is the simplest way to detect that it has been run from a command line
(cmd32.exe and possible other 3rd part command line apps).
second, and most importantly, if the app has been launched from the command line, how
can i print something to it.. e.g , this app can't be run from a command line.
Simplest Method I could come up with is. First, Create a console application, then Have your console application spawn your Window application, and Finally, Use Mapped Memory or some other form of Inter-Process Communication.

Resources