Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to own console for time of program execution?

I'm trying to write a program, that works in console or GUI mode, depending on execution parameters. I've managed to write following sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace wfSketchbook
{
    static class Program
    {
        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AttachConsole(int processId);

        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AllocConsole();

        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool FreeConsole();

        private const int ATTACH_PARENT_PROCESS = -1;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (!AttachConsole(ATTACH_PARENT_PROCESS))
                    AllocConsole();
                Console.WriteLine("Welcome to console!");
                Console.ReadKey();
                FreeConsole();
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}

It generally works, however when program is called from the system command-line, the cmd seems not to be aware, that program works in console mode and exits immediately:

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>wfSketchbook.exe test

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>Welcome to console!

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>

I'd rather expect following output:

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>wfSketchbook.exe test

Welcome to console!

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>

How may I correct this problem?

like image 252
Spook Avatar asked Nov 23 '25 14:11

Spook


2 Answers

There is no ideal solution for this. Cmd.exe is only going to automatically wait for the program to complete if it can see that the .exe is a console mode app. Which won't be the case for your app. One workaround is to tell it to wait:

start /wait yourapp.exe [arguments]

The other is to always use AllocConsole(). Which the side-effect that it creates a second console window. Changing the application type to Console then calling FreeConsole() is not ideal either, the flash of the window is quite noticeable. Pick your poison.

like image 74
Hans Passant Avatar answered Nov 25 '25 04:11

Hans Passant


There isn't any reliable way to make a Windows application a console and a GUI. Your program is a Windows application - so Windows launches you outside of the console window - when your program starts you aren't attached to the console window.

You could change your project output to a Console application in the project's properties. But then you would always get a console window. Windows could see that your application was marked as a console application and create a console even before you ran.

See this blog post for more information and links to some work arounds.

like image 38
shf301 Avatar answered Nov 25 '25 02:11

shf301



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!