write to stdout with win32 gui app - winapi

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.

Related

golang: optional console on windows

I am writing a service program which is expected to run in background. On Windows it will open a console window when run. I want it to go to background directly without that console window, so I used the -ldconf "-H=windowsgui" option, which worked as expected.
However, there is a catch. The program has a command line option -help, which output command line usage in the console. If I use -H=windowsgui, the help text is NOT printed even I start it in cmd.exe prompt.
It seems that the windowsgui option is not what I want. Is there anyway that the -help still works at commant line, and the console window will not persist if the program runs normally. I do not care if there is a console window pops up, provided that it disappears shortly without user intervention. i.e. I want a way on windows which is similar to the & operator on Linux.
P.S. if provided solution uses any other tools, I want that tool to be a Windows component, not any 3rd-party program. Thanks.

How can I show the console for a Windows GUI application only when it is started from the console? [duplicate]

My goal is to have it so when the executable is just double clicked no console spawns but also have it able to print to the console when the user launches it from the command line.
I have the following Windows-specific code set to not spawn a console:
#![windows_subsystem = "windows"]
fn main() {
println!("Hello world");
}
Unfortunately, it never hooks stdout/stderr to anything since its set to not create a console on startup. Is there any way to achieve my goal despite this?
This is not really specific to Rust, but instead is the way Windows works: you either write for the "console" subsystem and your program opens a console if it is started without one, or you write for the "windows" subsystem and your program detaches itself from the console if it is started from the command line.
The PATHEXT environment variable — which CLI shells use for the file extensions to add when searching PATH for a command — normally includes ".COM" before ".EXE", in which case one can create a console version of the application with a ".com" file extension (e.g. "app.com"). This can be a standalone version or just a launcher that spawns the main executable (e.g. "app.exe", a GUI app). A launcher can pass a command-line option that allows the main application to attach back to the console via AttachConsole.
Without a launcher, it's a bad idea to attach back to the console since the parent application (e.g. a CLI shell or a full TUI app) normally will not wait for a GUI application to exit, in which case the result is chaotic interleaved I/O with two applications competing for access.

How do I have a hidden console when using windows_subsystem?

My goal is to have it so when the executable is just double clicked no console spawns but also have it able to print to the console when the user launches it from the command line.
I have the following Windows-specific code set to not spawn a console:
#![windows_subsystem = "windows"]
fn main() {
println!("Hello world");
}
Unfortunately, it never hooks stdout/stderr to anything since its set to not create a console on startup. Is there any way to achieve my goal despite this?
This is not really specific to Rust, but instead is the way Windows works: you either write for the "console" subsystem and your program opens a console if it is started without one, or you write for the "windows" subsystem and your program detaches itself from the console if it is started from the command line.
The PATHEXT environment variable — which CLI shells use for the file extensions to add when searching PATH for a command — normally includes ".COM" before ".EXE", in which case one can create a console version of the application with a ".com" file extension (e.g. "app.com"). This can be a standalone version or just a launcher that spawns the main executable (e.g. "app.exe", a GUI app). A launcher can pass a command-line option that allows the main application to attach back to the console via AttachConsole.
Without a launcher, it's a bad idea to attach back to the console since the parent application (e.g. a CLI shell or a full TUI app) normally will not wait for a GUI application to exit, in which case the result is chaotic interleaved I/O with two applications competing for access.

How to limit the buffer size of a pipe (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.

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.

Resources