Console app that can launch Windows Form

Posted on 2007-09-27
Last Modified: 2013-12-04
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?

Question by:gromul
    LVL 21

    Expert Comment

    Create a batch file which when invoked with arguments calls the console app version of the application and calls the winform version otherwise.
    LVL 10

    Expert Comment

    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:
    LVL 10

    Expert Comment

    LVL 12

    Accepted Solution

    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
       = 0x0D; // VK_RETURN
                SendInput(1, ref input, Marshal.SizeOf(input));
       = 2; // KEYEVENTF_KEYUP
                SendInput(1, ref input, Marshal.SizeOf(input));

    Author Comment

    Thanks for a good solution!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now