• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1098
  • Last Modified:

Console app that can launch Windows Form

I'm trying to create an app with the following behavior:
1) If no command line argumentsare passed to it, it launches GUI
2) If command line arguments are passed to it, it operates in the console mode.

I can do this by having the console app launch GUI if there are no arguments, but there are several problems with that:
1) If launching the app from the explorer, a console window appears besides the windows form and stays there. If it's closed, the form is closed too.
2) If launching GUI from the console window, the console is waiting for the windows form to be closed (unlike when launching notepad i.e.).

Is there a good solution to this?

1 Solution
Create a batch file which when invoked with arguments calls the console app version of the application and calls the winform version otherwise.
ildasm is capable of doing this without a batch file.

You can do this by compiling your application as windows-only (no console) and creating a console manually when needed by using console api:

This code is not .net, however it may be adapted:

Create a Windows Form application and attach to an existing console or create your own console. Here is a demo.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
    static class Program
        static extern bool AttachConsole(int dwProcessId);
        static extern bool FreeConsole();
        const int ATTACH_PARENT_PROCESS = -1;
        public struct KEYDBINPUT
            public Int16 wVk;
            public Int16 wScan;
            public Int32 dwFlags;
            public Int32 time;
            public Int32 dwExtraInfo;
            public Int32 __filler1;
            public Int32 __filler2;

        public struct INPUT
            public Int32 type;
            public KEYDBINPUT ki;

        public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);
      /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            string[] args = Environment.GetCommandLineArgs();
            bool console_mode = false;
            if (args.Length > 1)
                console_mode = true;
                foreach (string arg in args)
            if (!console_mode)
                Application.Run(new Form1());


        static void ReleaseConsole()
            // simulate return pressing
            INPUT input = new INPUT();
            input.type = 1; // INPUT_KEYBOARD
            input.ki.wVk = 0x0D; // VK_RETURN
            SendInput(1, ref input, Marshal.SizeOf(input));
            input.ki.dwFlags = 2; // KEYEVENTF_KEYUP
            SendInput(1, ref input, Marshal.SizeOf(input));
gromulAuthor Commented:
Thanks for a good solution!

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now