Console app

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael C

    #1

    Console app

    Is it possible to have an app that is both a console app and a windows app?
    If there are no command line switches then it will run as a console app, if
    there are command line switches then it will be a windows app. If I make it
    a windows app then I can't write to the console. If I make it a console app
    then it starts a console if the user runs it from the start menu so from
    what I see it's not possible.

    Thanks in advance,
    Michael


  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Console app

    Michael C wrote:
    Is it possible to have an app that is both a console app and a windows app?
    Yes. Just open a windows form from a console application.
    If there are no command line switches then it will run as a console app, if
    there are command line switches then it will be a windows app.
    That's not possible. You can't turn a non-console application into one
    after it has started, and you have to start the application before you
    can check the command line.
    If I make it
    a windows app then I can't write to the console. If I make it a console app
    then it starts a console if the user runs it from the start menu so from
    what I see it's not possible.
    You could have a console application start a windows version of the
    program instead if it doesn't find anything in the command line.

    You could fake the console window by opening a windows form that acts
    like a console window.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • jpuopolo

      #3
      Re: Console app

      Michael:

      When you build the project, you can specifiy whether the project is in
      console mode. All this means is that Windows will create a default console
      by default. I typically write WinForms applications that I specificy as
      Console. This enables me to do things likes Console.WriteLi ne() in my
      application to do simply tracing and debugging. When you turn off Console,
      it simply means that Windows will not create a default Console.

      As the previous reply states, you cannot choose this at run-time -- the
      subsystem (Windows/Console) needs to be decided at compile/link time.

      Note that from a WinForms-type application, you can always create your own
      Console explicitly.

      jpuopolo


      "Michael C" <nospam@nospam. comwrote in message
      news:OFecNO%231 HHA.3940@TK2MSF TNGP05.phx.gbl. ..
      Is it possible to have an app that is both a console app and a windows
      app? If there are no command line switches then it will run as a console
      app, if there are command line switches then it will be a windows app. If
      I make it a windows app then I can't write to the console. If I make it a
      console app then it starts a console if the user runs it from the start
      menu so from what I see it's not possible.
      >
      Thanks in advance,
      Michael
      >
      >

      Comment

      • Michael A. Covington

        #4
        Re: Console app


        "Michael C" <nospam@nospam. comwrote in message
        news:OFecNO%231 HHA.3940@TK2MSF TNGP05.phx.gbl. ..
        Is it possible to have an app that is both a console app and a windows
        app? If there are no command line switches then it will run as a console
        app, if there are command line switches then it will be a windows app. If
        I make it a windows app then I can't write to the console. If I make it a
        console app then it starts a console if the user runs it from the start
        menu so from what I see it's not possible.
        Yes. Write it as a windows app but tell the compiler it's a console app.
        Then it will have access to the console, and it can refrain from actually
        opening any windows if that's what you want.

        - The other Michael C.


        Comment

        • Michael C

          #5
          Re: Console app

          "Michael A. Covington" <look@ai.uga.ed u.for.addresswr ote in message
          news:O$HEenD2HH A.2064@TK2MSFTN GP03.phx.gbl...
          Yes. Write it as a windows app but tell the compiler it's a console app.
          Then it will have access to the console, and it can refrain from actually
          opening any windows if that's what you want.
          Thanks for the reply. The problem with this approach is that when the user
          starts the exe from say the start menu then a dos box appears. I'm presuming
          it's not possible to do what I want because everything is done by the time
          main gets called and I have no control over it before then.

          Michael


          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: Console app

            Michael C wrote:
            Is it possible to have an app that is both a console app and a windows app?
            If there are no command line switches then it will run as a console app, if
            there are command line switches then it will be a windows app. If I make it
            a windows app then I can't write to the console. If I make it a console app
            then it starts a console if the user runs it from the start menu so from
            what I see it's not possible.
            It is not easy, but if you are wiling to code for it, then it
            can be done.

            See the code below (it is not production grade, but it is a
            starting point).

            Arne

            =============== =============== ===============

            using System;
            using System.Text;
            using System.Drawing;
            using System.Windows. Forms;
            using System.Runtime. InteropServices ;

            namespace E
            {
            public class MainForm : Form
            {
            private Label lbl;
            public MainForm()
            {
            InitializeCompo nent();
            }
            public void InitializeCompo nent()
            {
            lbl = new Label();
            SuspendLayout() ;
            lbl.Location = new Point(50, 50);
            lbl.Size = new Size(200, 25);
            lbl.Text = "This is a GUI app";
            Text = "Main form";
            Size = new Size(300, 300);
            Controls.Add(lb l);
            ResumeLayout(fa lse);
            }
            }
            public class MyConsole : IDisposable
            {
            private const uint STD_INPUT_HANDL E = 0xfffffff6;
            private const uint STD_OUTPUT_HAND LE = 0xfffffff5;
            private const uint STD_ERROR_HANDL E = 0xfffffff4;
            [DllImport("kern el32.dll")]
            public static extern bool AllocConsole();
            [DllImport("kern el32.dll")]
            public static extern bool FreeConsole();
            [DllImport("kern el32.dll")]
            public static extern int GetStdHandle(ui nt nStdHandle);
            [DllImport("kern el32.dll")]
            public static extern bool WriteConsole(in t hConsoleOutput,
            string lpBuffer,
            int nNumberOfCharsT oWrite,
            ref int
            lpNumberOfChars Written,
            int lpReserved);
            [DllImport("kern el32.dll")]
            public static extern bool ReadConsole(int hConsoleInput,
            StringBuilder lpBuffer,
            int nNumberOfCharsT oRead,
            ref int lpNumberOfChars Read,
            int lpReserved);
            private int stdin;
            private int stdout;
            public MyConsole()
            {
            AllocConsole();
            stdin = GetStdHandle(ST D_INPUT_HANDLE) ;
            stdout = GetStdHandle(ST D_OUTPUT_HANDLE );
            }
            public void WriteLine(strin g s)
            {
            int len = 0;
            WriteConsole(st dout, s + "\r\n", s.Length + 2, ref len, 0);
            }
            public string ReadLine()
            {
            int len = 0;
            StringBuilder sb = new StringBuilder() ;
            ReadConsole(std in, sb, 256, ref len, 0);
            return sb.ToString(0, sb.Length - 2);
            }
            public void Dispose()
            {
            FreeConsole();
            }
            }
            public class MainClass
            {
            [STAThread]
            public static void Main(string[] args)
            {
            if(args[0] == "GUI")
            {
            Application.Run (new MainForm());
            }
            else if(args[0] == "Console")
            {
            using(MyConsole console = new MyConsole())
            {
            console.WriteLi ne("This is a console app");
            }
            }
            }
            }
            }

            Comment

            • Michael C

              #7
              Re: Console app

              "Arne Vajhøj" <arne@vajhoej.d kwrote in message
              news:46b7beac$0 $90267$14726298 @news.sunsite.d k...
              It is not easy, but if you are wiling to code for it, then it
              can be done.
              >
              See the code below (it is not production grade, but it is a
              starting point).
              Thanks for the reply. This is a good solution but isn't exactly what I need.
              If someone types MyApp.exe Console at the command line then it will create a
              new command window instead of using the existing one.

              BTW, your code can be simplified by just calling AllocConsole and then using
              the standard dotnet console commands.

              Michael


              Comment

              • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                #8
                Re: Console app

                Michael C wrote:
                "Arne Vajhøj" <arne@vajhoej.d kwrote in message
                news:46b7beac$0 $90267$14726298 @news.sunsite.d k...
                >It is not easy, but if you are wiling to code for it, then it
                >can be done.
                >>
                >See the code below (it is not production grade, but it is a
                >starting point).
                >
                Thanks for the reply. This is a good solution but isn't exactly what I need.
                If someone types MyApp.exe Console at the command line then it will create a
                new command window instead of using the existing one.
                I thougth you said that the users would be starting the EXE from
                the start menu ??

                But if they have a console already, then use AttachConsole
                instead of AllocConsole.

                Arne

                Comment

                • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                  #9
                  Re: Console app

                  Michael C wrote:
                  BTW, your code can be simplified by just calling AllocConsole and then using
                  the standard dotnet console commands.
                  Actually I did not even try that.

                  I prefer not to write code that work or not work depending
                  on the implementation of the Console class.

                  Arne

                  Comment

                  • Michael C

                    #10
                    Re: Console app

                    "Arne Vajhøj" <arne@vajhoej.d kwrote in message
                    news:46b7ce51$0 $90273$14726298 @news.sunsite.d k...
                    I thougth you said that the users would be starting the EXE from
                    the start menu ??
                    They might start from the start menu but they might start from an existing
                    console.
                    But if they have a console already, then use AttachConsole
                    instead of AllocConsole.
                    Problem is by that time the console has already gone back to the prompt so
                    the users can type additional commands. I think something is needed to
                    modify the code that runs before main is called. This is probably possible
                    somehow but difficult.

                    Michael


                    Comment

                    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                      #11
                      Re: Console app

                      Michael C wrote:
                      "Arne Vajhøj" <arne@vajhoej.d kwrote in message
                      news:46b7beac$0 $90267$14726298 @news.sunsite.d k...
                      >It is not easy, but if you are wiling to code for it, then it
                      >can be done.
                      >>
                      >See the code below (it is not production grade, but it is a
                      >starting point).
                      >
                      Thanks for the reply. This is a good solution but isn't exactly what I need.
                      If someone types MyApp.exe Console at the command line then it will create a
                      new command window instead of using the existing one.
                      Hm.

                      I just tried.

                      On my system it does not create a new console window in that scenario.

                      Arne

                      Comment

                      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                        #12
                        Re: Console app

                        Arne Vajhøj wrote:
                        Michael C wrote:
                        >"Arne Vajhøj" <arne@vajhoej.d kwrote in message
                        >news:46b7beac$ 0$90267$1472629 8@news.sunsite. dk...
                        >>It is not easy, but if you are wiling to code for it, then it
                        >>can be done.
                        >>>
                        >>See the code below (it is not production grade, but it is a
                        >>starting point).
                        >>
                        >Thanks for the reply. This is a good solution but isn't exactly what I
                        >need. If someone types MyApp.exe Console at the command line then it
                        >will create a new command window instead of using the existing one.
                        >
                        Hm.
                        >
                        I just tried.
                        >
                        On my system it does not create a new console window in that scenario.
                        Nah. That is with /t:exe, but that gives a console with start run.

                        Arne

                        Comment

                        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                          #13
                          Re: Console app

                          Michael C wrote:
                          Problem is by that time the console has already gone back to the prompt so
                          the users can type additional commands. I think something is needed to
                          modify the code that runs before main is called. This is probably possible
                          somehow but difficult.
                          You need a new /t: type that neither start a new process
                          or create a console window.

                          Arne

                          Comment

                          • Michael A. Covington

                            #14
                            Re: Console app


                            "Michael C" <nospam@nospam. comwrote in message
                            news:un9V4NI2HH A.5164@TK2MSFTN GP05.phx.gbl...
                            "Michael A. Covington" <look@ai.uga.ed u.for.addresswr ote in message
                            news:O$HEenD2HH A.2064@TK2MSFTN GP03.phx.gbl...
                            >Yes. Write it as a windows app but tell the compiler it's a console app.
                            >Then it will have access to the console, and it can refrain from actually
                            >opening any windows if that's what you want.
                            >
                            Thanks for the reply. The problem with this approach is that when the user
                            starts the exe from say the start menu then a dos box appears. I'm
                            presuming it's not possible to do what I want because everything is done
                            by the time main gets called and I have no control over it before then.
                            The other thing it could do is the Win32 AllocConsole call to get a console.


                            Comment

                            • Michael C

                              #15
                              Re: Console app

                              "Michael A. Covington" <look@ai.uga.ed u.for.addresswr ote in message
                              news:OfAFUPU2HH A.748@TK2MSFTNG P04.phx.gbl...
                              >Thanks for the reply. The problem with this approach is that when the
                              >user starts the exe from say the start menu then a dos box appears. I'm
                              >presuming it's not possible to do what I want because everything is done
                              >by the time main gets called and I have no control over it before then.
                              >
                              The other thing it could do is the Win32 AllocConsole call to get a
                              console.
                              I tried that and it does work but if you run the app from a console then it
                              creates a new console to send the text to, when the app closes the new
                              console disappears. If your app just sends a simple response then you just
                              get a flicker of this new console on the screen.
                              >
                              >

                              Comment

                              Working...