• 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?

Thanks
0
gromul
Asked:
gromul
1 Solution
 
surajgupthaCommented:
Create a batch file which when invoked with arguments calls the console app version of the application and calls the winform version otherwise.
0
 
pjasnosCommented:
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:

http://www.codeproject.com/useritems/ConsoleAdapter.asp
0
 
OnegaZhangCommented:
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
    {
        [DllImport("kernel32.dll")]
        static extern bool AttachConsole(int dwProcessId);
        [DllImport("kernel32.dll")]
        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;
        }

        [DllImport("user32")]
        public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);
      /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string[] args = Environment.GetCommandLineArgs();
            bool console_mode = false;
            if (args.Length > 1)
            {
                console_mode = true;
                AttachConsole(ATTACH_PARENT_PROCESS);
                foreach (string arg in args)
                {
                    Console.WriteLine(arg);
                }
                ReleaseConsole();
            }
           
            if (!console_mode)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());

            }
           
        }

        static void ReleaseConsole()
        {
            FreeConsole();
            // 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));
        }
    }
}
0
 
gromulAuthor Commented:
Thanks for a good solution!
0

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