C
C#16mo ago
lars

❔ Output to console if run from console - otherwise do not create console window?

I've got an Windows app that doesn't have a UI. I'd like to not have a console window opened when it's launched fron File Explorer, but if the user launches the app from a terminal (eg. ./myapp.exe), then I'd want Console.WriteLine calls (for debugging) to go to that terminal window. Is there some way to achieve this? If I set the OutputType to Exe, then it spawns a console window when launched from File Explorer. However, when run from a terminal window, the behavior is correct - Console.WriteLine output goes to that existing terminal window. If I set the OutputType to WinExe, the behavior is correct when launched from File Explorer - no console window is spawned. However, when run from a terminal window, Console.WriteLine calls don't appear at all
6 Replies
nukleer bomb
nukleer bomb16mo ago
Try using AttachConsole(ATTACH_PARENT_PROCESS);:
const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;

[DllImport("kernel32.dll")]
static extern bool AttachConsole(uint dwProcessId);
const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;

[DllImport("kernel32.dll")]
static extern bool AttachConsole(uint dwProcessId);
OutputType == WinExe
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
lars
larsOP16mo ago
that's working perfectly. thank you! are you suggesting calling AttachConsole conditionally based on Environment.UserInteractive?
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
lars
larsOP16mo ago
cool i'll investigate that 👍 thanks both
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server