Hello Stephen,

Windows uses special flag in every executable binary to determine if 
executable is a GUI or command line program.

go program uses "-H windows" linker flag to set that flag. See 
debug/pe.TestBuildingWindowsGUI for details.

Once windows executable is built, go has no control over how this program 
executes.

When command line program is executed by clicking in explorer window 
Windows automatically starts a new console window and the console is used 
for stdout output (I did not check that). If command line program is 
started from existing cmd.exe console, new process just uses the same 
console.

When you click on GUI executable in Windows explorer, no console windows is 
started (I did not check that). Same for GUI executable started from 
cmd.exe console - new GUI process is not attached to parent console (I did 
not check that).

If you want GUI with console window, you have to create console window in 
your code.

Alex 


On Friday, 29 April 2022 at 17:34:29 UTC+10 stephen.t....@gmail.com wrote:

> I've read through my post again. I'll try to simplify my question:
>
> Where is stdout for a Windows "GUI binary"? (see -H option)
>
> https://pkg.go.dev/cmd/link
>
> If I run from the command prompt I would have thought that stdout is the 
> console I'm running from but if the executable is a "GUI binary" this does 
> not seem to be the case.
>
> If I create a "console binary" then stdout is indeed the console I'm 
> running from. But this means that when I run the binary from a desktop 
> icon, Windows will open up a console window, even if there is nothing to 
> output.
>
> Context for this is an application that has a GUI by default and 
> optionally, a terminal interface (using stdout).
>
> I'm not a Windows person and I'm sorry if this is a dumb question but this 
> is a complete mystery to me. Do I really need two separate binaries?
>
> On Monday, April 25, 2022 at 9:48:18 AM UTC+1 stephen.t....@gmail.com 
> wrote:
>
>> Hello,
>>
>> I'm cross-compiling my Go application to Windows and I'm having 
>> difficulty understanding how Windows interacts with stdout.
>>
>> I'm compiling the Windows binary with the "-H=windowsgui" LD flag. This 
>> works great but I've found that the program will never print to stdout, 
>> even if the program is launched from the command prompt
>>
>> The alternative LD flag "-H=windows" will print to stdout but with the 
>> downside that a "DOS" window will always open, even if the program is 
>> launched from the desktop icon and even when there is no stdout activity.
>>
>> For the most part, compiling with "-H=windowsgui" is fine but 
>> occasionally it's useful for users to run with the program's -log option, 
>> which outputs to stdout.
>>
>> My solution for now is to have two Windows binaries. One for regular use 
>> and another for diagnosing problems. This is a poor solution.
>>
>> What I require is a single binary that can:
>>
>> 1) Launch from the desktop icon without a "DOS" window opening.
>> 2) To launch from a command prompt and for stdout to be echoed to the 
>> command prompt window.
>>
>> Is there a Go compilation option that I've missed. Or is the solution 
>> more complex?
>>
>> Regards
>>
>> Stephen
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ea7da703-d5e6-464e-add7-f741cdf12464n%40googlegroups.com.

Reply via email to