Command line interface and Windows form in same program

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

    Command line interface and Windows form in same program

    Hi,

    I've written a program which has both a command line interface
    and Windows form interface, however it isn't quite working
    correctly.

    When run from command line with no arguments it should display
    the Windows form. The form is being displayed but the command
    only returns when the form is closed. I want the command line to
    return immediately, leaving the form displayed.

    When run from Windows Explorer (by double-clicking on
    TheProgram.exe) it should display the Windows form only. The
    form is being displayed but a console window is also being
    displayed.

    When run from the command line with the correct number of
    arguments it should operate on the supplied arguments and display
    a message. This is working correctly.

    If the correct number of arguments is supplied it should display
    the command help. This is working correctly.

    In Project - Properties, the Output type is 'Windows Application'
    and Startup object is 'Sub Main'.

    Sub Main is basically:

    arguments = Environment.Get CommandLineArgs ()

    If arguments.Lengt h = 1 Then
    'No command line arguments so display the Windows
    form
    Dim frmMain As New frmWindows_Form
    Application.Run (frmMain)

    ElseIf arguments.Lengt h <3 Then
    'Required arguments not supplied, so display the help
    Output_Command_ Help()

    Else
    'Run command


    Any help much appreciated. Thanks.


  • Jwe

    #2
    Re: Command line interface and Windows form in same program

    If the correct number of arguments is supplied it should display
    the command help. This is working correctly.

    should be:

    If an incorrect number of arguments is supplied it should display
    the command help. This is working correctly.

    "Jwe" <jwe@mail.comwr ote in message
    news:ce2dndMpKc VZ6zHanZ2dneKdn ZydnZ2d@pipex.n et...
    Hi,
    >
    I've written a program which has both a command line interface
    and Windows form interface, however it isn't quite working
    correctly.
    >
    When run from command line with no arguments it should display
    the Windows form. The form is being displayed but the command
    only returns when the form is closed. I want the command line
    to return immediately, leaving the form displayed.
    >
    When run from Windows Explorer (by double-clicking on
    TheProgram.exe) it should display the Windows form only. The
    form is being displayed but a console window is also being
    displayed.
    >
    When run from the command line with the correct number of
    arguments it should operate on the supplied arguments and
    display a message. This is working correctly.
    >
    If the correct number of arguments is supplied it should
    display the command help. This is working correctly.
    >
    In Project - Properties, the Output type is 'Windows
    Application' and Startup object is 'Sub Main'.
    >
    Sub Main is basically:
    >
    arguments = Environment.Get CommandLineArgs ()
    >
    If arguments.Lengt h = 1 Then
    'No command line arguments so display the Windows
    form
    Dim frmMain As New frmWindows_Form
    Application.Run (frmMain)
    >
    ElseIf arguments.Lengt h <3 Then
    'Required arguments not supplied, so display the
    help
    Output_Command_ Help()
    >
    Else
    'Run command
    >
    >
    Any help much appreciated. Thanks.
    >

    Comment

    • zacks@construction-imaging.com

      #3
      Re: Command line interface and Windows form in same program

      On Feb 8, 10:35 am, "Jwe" <j...@mail.comw rote:
      Hi,
      >
      I've written a program which has both a command line interface
      and Windows form interface, however it isn't quite working
      correctly.
      >
      When run from command line with no arguments it should display
      the Windows form.  The form is being displayed but the command
      only returns when the form is closed.  I want the command line to
      return immediately, leaving the form displayed.
      >
      When run from Windows Explorer (by double-clicking on
      TheProgram.exe) it should display the Windows form only.  The
      form is being displayed but a console window is also being
      displayed.
      >
      When run from the command line with the correct number of
      arguments it should operate on the supplied arguments and display
      a message.  This is working correctly.
      >
      If the correct number of arguments is supplied it should display
      the command help.  This is working correctly.
      >
      In Project - Properties, the Output type is 'Windows Application'
      and Startup object is 'Sub Main'.
      >
      Sub Main is basically:
      >
              arguments = Environment.Get CommandLineArgs ()
      >
              If arguments.Lengt h = 1 Then
                  'No command line arguments so display the Windows
      form
                  Dim frmMain As New frmWindows_Form
                  Application.Run (frmMain)
      >
              ElseIf arguments.Lengt h <3 Then
                  'Required arguments not supplied, so display the help
                  Output_Command_ Help()
      >
              Else
                  'Run command <--- WHATDOES THIS DO?
      >
      Any help much appreciated.  Thanks.
      See note 4 lines up.

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: Command line interface and Windows form in same program

        WinWord.exe, for example, can be started as a command line. I believe all
        you would need to do in this case is to create a winforms application. If
        the user starts it from a command line it should behave as you expect. Just
        include your logic for the argument checks and write to Console.out.

        "Jwe" wrote:
        Hi,
        >
        I've written a program which has both a command line interface
        and Windows form interface, however it isn't quite working
        correctly.
        >
        When run from command line with no arguments it should display
        the Windows form. The form is being displayed but the command
        only returns when the form is closed. I want the command line to
        return immediately, leaving the form displayed.
        >
        When run from Windows Explorer (by double-clicking on
        TheProgram.exe) it should display the Windows form only. The
        form is being displayed but a console window is also being
        displayed.
        >
        When run from the command line with the correct number of
        arguments it should operate on the supplied arguments and display
        a message. This is working correctly.
        >
        If the correct number of arguments is supplied it should display
        the command help. This is working correctly.
        >
        In Project - Properties, the Output type is 'Windows Application'
        and Startup object is 'Sub Main'.
        >
        Sub Main is basically:
        >
        arguments = Environment.Get CommandLineArgs ()
        >
        If arguments.Lengt h = 1 Then
        'No command line arguments so display the Windows
        form
        Dim frmMain As New frmWindows_Form
        Application.Run (frmMain)
        >
        ElseIf arguments.Lengt h <3 Then
        'Required arguments not supplied, so display the help
        Output_Command_ Help()
        >
        Else
        'Run command
        >
        >
        Any help much appreciated. Thanks.
        >
        >
        >

        Comment

        • Jwe

          #5
          Re: Command line interface and Windows form in same program

          Hi, thanks for your reply, however I'm not trying to run any
          external program like Winword in the way you describe. This is a
          single .exe generated by a single VB.Net project. The project
          was started as a Windows Application (to get the Form in the
          project) and then changed to a console app.

          "Family Tree Mike" <FamilyTreeMike @discussions.mi crosoft.com>
          wrote in message
          news:815AD2A3-2322-41F6-BB7B-67FE6DDB3A43@mi crosoft.com...
          WinWord.exe, for example, can be started as a command line. I
          believe all
          you would need to do in this case is to create a winforms
          application. If
          the user starts it from a command line it should behave as you
          expect. Just
          include your logic for the argument checks and write to
          Console.out.
          >
          "Jwe" wrote:
          >
          >Hi,
          >>
          >I've written a program which has both a command line interface
          >and Windows form interface, however it isn't quite working
          >correctly.
          >>
          >When run from command line with no arguments it should display
          >the Windows form. The form is being displayed but the command
          >only returns when the form is closed. I want the command line
          >to
          >return immediately, leaving the form displayed.
          >>
          >When run from Windows Explorer (by double-clicking on
          >TheProgram.exe ) it should display the Windows form only. The
          >form is being displayed but a console window is also being
          >displayed.
          >>
          >When run from the command line with the correct number of
          >arguments it should operate on the supplied arguments and
          >display
          >a message. This is working correctly.
          >>
          >If the correct number of arguments is supplied it should
          >display
          >the command help. This is working correctly.
          >>
          >In Project - Properties, the Output type is 'Windows
          >Application'
          >and Startup object is 'Sub Main'.
          >>
          >Sub Main is basically:
          >>
          > arguments = Environment.Get CommandLineArgs ()
          >>
          > If arguments.Lengt h = 1 Then
          > 'No command line arguments so display the Windows
          >form
          > Dim frmMain As New frmWindows_Form
          > Application.Run (frmMain)
          >>
          > ElseIf arguments.Lengt h <3 Then
          > 'Required arguments not supplied, so display the
          >help
          > Output_Command_ Help()
          >>
          > Else
          > 'Run command
          >>
          >>
          >Any help much appreciated. Thanks.
          >>
          >>
          >>

          Comment

          • Jwe

            #6
            Re: Command line interface and Windows form in same program

            If I change the project to a Console Application, the behaviour
            is different:

            1. Run TheProgram.exe from a command prompt with no arguments:
            the Windows form is displayed and the command prompt returns
            immediately. This works as expected.

            2. Run TheProgram.exe by double-clicking it in Windows Explorer:
            the Windows form is display and no console window is opened.
            This works as expected.

            BUT:

            3. Run TheProgram.exe from a command prompt with correct number
            of arguments: runs as expected, but does not display the expected
            message.

            4. Run TheProgram.exe from a command prompt with incorrect number
            of arguments: does not display the command help.

            Thanks for any tips etc.

            "Jwe" <jwe@mail.comwr ote in message
            news:ce2dndMpKc VZ6zHanZ2dneKdn ZydnZ2d@pipex.n et...
            Hi,
            >
            I've written a program which has both a command line interface
            and Windows form interface, however it isn't quite working
            correctly.
            >
            When run from command line with no arguments it should display
            the Windows form. The form is being displayed but the command
            only returns when the form is closed. I want the command line
            to return immediately, leaving the form displayed.
            >
            When run from Windows Explorer (by double-clicking on
            TheProgram.exe) it should display the Windows form only. The
            form is being displayed but a console window is also being
            displayed.
            >
            When run from the command line with the correct number of
            arguments it should operate on the supplied arguments and
            display a message. This is working correctly.
            >
            If the correct number of arguments is supplied it should
            display the command help. This is working correctly.
            >
            In Project - Properties, the Output type is 'Windows
            Application' and Startup object is 'Sub Main'.
            >
            Sub Main is basically:
            >
            arguments = Environment.Get CommandLineArgs ()
            >
            If arguments.Lengt h = 1 Then
            'No command line arguments so display the Windows
            form
            Dim frmMain As New frmWindows_Form
            Application.Run (frmMain)
            >
            ElseIf arguments.Lengt h <3 Then
            'Required arguments not supplied, so display the
            help
            Output_Command_ Help()
            >
            Else
            'Run command
            >
            >
            Any help much appreciated. Thanks.
            >

            Comment

            • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

              #7
              Re: Command line interface and Windows form in same program

              Sorry, I see how I confused you. I'm trying to say to switch back to a
              winform app, and just lauch it from a command line. WinWord was just an
              example. You would launch MyApplication.e xe if that is your built
              executable. I thought however that System.IO.Conso le.WriteLine would still
              work, but it doesn't seem to in my quick test. Sorry!


              "Jwe" wrote:
              Hi, thanks for your reply, however I'm not trying to run any
              external program like Winword in the way you describe. This is a
              single .exe generated by a single VB.Net project. The project
              was started as a Windows Application (to get the Form in the
              project) and then changed to a console app.
              >
              "Family Tree Mike" <FamilyTreeMike @discussions.mi crosoft.com>
              wrote in message
              news:815AD2A3-2322-41F6-BB7B-67FE6DDB3A43@mi crosoft.com...
              WinWord.exe, for example, can be started as a command line. I
              believe all
              you would need to do in this case is to create a winforms
              application. If
              the user starts it from a command line it should behave as you
              expect. Just
              include your logic for the argument checks and write to
              Console.out.

              "Jwe" wrote:
              Hi,
              >
              I've written a program which has both a command line interface
              and Windows form interface, however it isn't quite working
              correctly.
              >
              When run from command line with no arguments it should display
              the Windows form. The form is being displayed but the command
              only returns when the form is closed. I want the command line
              to
              return immediately, leaving the form displayed.
              >
              When run from Windows Explorer (by double-clicking on
              TheProgram.exe) it should display the Windows form only. The
              form is being displayed but a console window is also being
              displayed.
              >
              When run from the command line with the correct number of
              arguments it should operate on the supplied arguments and
              display
              a message. This is working correctly.
              >
              If the correct number of arguments is supplied it should
              display
              the command help. This is working correctly.
              >
              In Project - Properties, the Output type is 'Windows
              Application'
              and Startup object is 'Sub Main'.
              >
              Sub Main is basically:
              >
              arguments = Environment.Get CommandLineArgs ()
              >
              If arguments.Lengt h = 1 Then
              'No command line arguments so display the Windows
              form
              Dim frmMain As New frmWindows_Form
              Application.Run (frmMain)
              >
              ElseIf arguments.Lengt h <3 Then
              'Required arguments not supplied, so display the
              help
              Output_Command_ Help()
              >
              Else
              'Run command
              >
              >
              Any help much appreciated. Thanks.
              >
              >
              >
              >
              >
              >

              Comment

              • Jwe

                #8
                Re: Command line interface and Windows form in same program


                "Jwe" <jwe@mail.comwr ote in message
                news:ce2dndMpKc VZ6zHanZ2dneKdn ZydnZ2d@pipex.n et...
                Hi,
                >
                I've written a program which has both a command line interface
                and Windows form interface, however it isn't quite working
                correctly.
                >
                When run from command line with no arguments it should display
                the Windows form. The form is being displayed but the command
                only returns when the form is closed. I want the command line
                to return immediately, leaving the form displayed.
                >
                When run from Windows Explorer (by double-clicking on
                TheProgram.exe) it should display the Windows form only. The
                form is being displayed but a console window is also being
                displayed.
                >
                When run from the command line with the correct number of
                arguments it should operate on the supplied arguments and
                display a message. This is working correctly.
                >
                If the correct number of arguments is supplied it should
                display the command help. This is working correctly.
                >
                In Project - Properties, the Output type is 'Windows
                Application' and Startup object is 'Sub Main'.
                >
                Sub Main is basically:
                >
                arguments = Environment.Get CommandLineArgs ()
                >
                If arguments.Lengt h = 1 Then
                'No command line arguments so display the Windows
                form
                Dim frmMain As New frmWindows_Form
                Application.Run (frmMain)
                >
                ElseIf arguments.Lengt h <3 Then
                'Required arguments not supplied, so display the
                help
                Output_Command_ Help()
                >
                Else
                'Run command
                >
                >
                Any help much appreciated. Thanks.
                >

                has a solution to the problem (incorporated in the code below)
                which works for Visual Studio 2005 (Visual Basic 2005 Express,
                Framework 2.0), but not for Visual Studio .NET 2003 (VB 2003
                ..NET, Framework 1.1).

                Here's how to reproduce the problem using either Visual Basic
                ..NET 2003 or Visual Basic 2005:

                Start a new Windows Application project - WindowsApplicat ion1.
                This gives Form1.vb.
                Add a Module to the project - Module1.vb
                Replace the empty Module1 code with the following code:

                Imports System.Environm ent

                Module Module1

                Declare Function AttachConsole Lib "kernel32" (ByVal
                dwProcessId As Int32) As Boolean
                Declare Function FreeConsole Lib "kernel32.d ll" () As Boolean

                Sub Main()
                Dim args As String() = GetCommandLineA rgs()
                If args.Length = 1 Then
                'No arguments so show windows form
                Dim frmMain As New Form1
                Application.Run (frmMain)
                Else
                AttachConsole(-1)
                Console.WriteLi ne("Arguments supplied")
                FreeConsole()
                End If
                End Sub

                End Module


                For VB 2003, in Project - Properties, leave Output type as
                Windows Application and change Startup object to Sub Main.
                For VB 2005, in Project - Properties, untick Enable application
                framework, leave Output type as Windows Application and change
                Startup object to Sub Main.

                Build the project, then open a command prompt and run
                WindowsApplicat ion1.exe from it as follows:

                1. With no arguments. The form is displayed and the command
                prompt returns immediately, allowing other commands to be
                entered. Required behaviour.

                2. With any number of arguments. The 'Arguments supplied' is
                displayed for the VB 2005 application, but not for VB 2003.

                3. Run it from Windows Explorer. The form is displayed and a
                separate console window is not opened. Required behaviour.

                So case 2 isn't working as required for VB 2003.

                The AttachConsole and FreeConsole functions described in the link
                above make case 2 work for VB 2005.

                For the VB 2003 application, it fixes case 2 for a Debug build,
                but has no effect for a Release build, which is exactly the same
                findings as


                I'm interested in why it doesn't work for the VB 2003 Release
                build, and any ideas for alternative fixes.

                cheers,


                Comment

                Working...