locked
Passing Command Line Argument RRS feed

  • Question

  • Hi,

    I am a beginner using Visual Studio Express 2010 to create a small console app to use as a utility accompanying another primary app that can pass a command line to the utility. I would like to set it up so that the utility will not run independently but only run and load its only form (Form1) if it is launched by the primary app using a "/run" command line parameter. (I can set up the primary app to launch and pass a command line parameter. I need help with the utility's code.)

    The utility already has some VB code, so I need to figure out what code to add to receive and act on the " /run" command line and even more challenging for me, where to place this new code so it works.

    pseudo-code:

    initial event to read the command line, then if it is " /run" load Form1, else do not open and run utility app.

    Something like that.

    Any help appreciated.

    Regards,


    Thursday, December 15, 2011 1:15 PM

Answers

  • Thanks for your continued help. I'm making some progress.

    OK, I think I found the problem and solved it. There was also a timer repainting the Form, so if I place the code to check the command arg in the timer event that calls the repaint function, it works. If "run" is present, then all runs OK and repaints, if not, a blank (un-repainted) form.

    About as good as I can do for now. Maybe I can refine this as I learn more.

     

    Much thanks.

    • Edited by saratogacoach Thursday, December 15, 2011 4:58 PM
    • Marked as answer by saratogacoach Thursday, December 15, 2011 4:59 PM
    Thursday, December 15, 2011 4:01 PM

All replies

  • See http://msdn.microsoft.com/en-us/library/z2d603cy%28v=VS.90%29.aspx.  That should get you started.
    Serial Port      Random       Check Internet Connection      Microsoft® Community Contributor 2011
    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein
    Thursday, December 15, 2011 1:36 PM
  • Hi,

    Thank you for this suggestion. I read it carefully and tried to use the code sample in my app. Unfortunately, while it didn't throw any debugging erors, it also didn't work by displaying a message box (part of the sample code).

    (1) need for console app to read command line (if any passed)

    (2) if command line = " /run" Then load Form1

         else close app (if someone clicked on the EXE without also passing a command line to " /run")

    So, I'm still stuck and need help getting this to work.

    Regards,

    Thursday, December 15, 2011 1:47 PM
  • Can you post the code that you used to check the command line arguments?
    Thursday, December 15, 2011 1:49 PM
  • Hi,

    Yes, taken from the MS sample (link http://msdn.microsoft.com/en-us/library/z2d603cy%28v=VS.90%29.aspx):

     

    Private Sub ParseCommandLineArgs()
        Dim inputArgument As String = "/input="
        Dim inputName As String = ""
    
        For Each s As String In My.Application.CommandLineArgs
            If s.ToLower.StartsWith(inputArgument) Then
                inputName = s.Remove(0, inputArgument.Length)
            End If
        Next
    
        If inputName = "" Then
            MsgBox("No input name")
        Else
            MsgBox("Input name: " & inputName)
        End If
    End Sub
    

     


    I also set up the debugger to look for / input=

    The app ran OK, no errors, but no message box either. So, can't get it working correctly. I must be doing something wrong.



    Thursday, December 15, 2011 1:58 PM
  • In the debugger have you set the command line arguments for testing?

    http://msdn.microsoft.com/en-us/library/1ktzfy9w.aspx


    Serial Port      Random       Check Internet Connection      Microsoft® Community Contributor 2011
    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein
    • Edited by dbasnett Thursday, December 15, 2011 2:13 PM
    Thursday, December 15, 2011 2:07 PM
  • The code works for me. When you type the arguments, are you sure you had no blanks?

    If you enter "/input=MyInput" on the command line, that whole string will be the first element in the CommandLineArgs array and your code will assign "MyInput to the inputName variable.

    If you enter "/input = MyInput", you will have three elements in the CommandLineArgs array, "/input", "=", and "MyInput" and your code will assign an empty string to the inputName variable.

    If you want to process the entire command line argument as a single string, you can use the String.Join method to combine the elements of the array into one string.

    Thursday, December 15, 2011 2:18 PM
  • Hi,

    I have re-checked the code and don't see any typo's or blanks. I have set up the debugger for command line testing and entered "/input=" (and also " /input" and /input= and /input), none of which display a message box.

    However, since the goal is receiving a command line when the app is launched, getting the argument, and if it = " /run" to load Form1 or else Me.Close() or some other way to stop and close the app (prevent it from running unless launched with this command parameter), can you suggest code to accomplish this?

    Again, thank you for your continued help.

    Regards,

     

    Thursday, December 15, 2011 2:51 PM
  • Start here

     

            For Each s As String In My.Application.CommandLineArgs
                Debug.WriteLine(s)
            Next

    Do you see the arguments?


    Serial Port     Random       Check Internet Connection     Microsoft® Community Contributor      My internet telepathy is down!
    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein
    Thursday, December 15, 2011 2:53 PM
  • Hi,

    Sorry to be so limited in my skills for this. I'm not even sure how to test your above code. Don't know where to put it. When I tried adding it in the code that is already there for Form1, it displayed errors. So, unfortunately, I need to know exactly where to place it, whether it also needs a sub, etc.

    Thank you for your help.

    Regards,



    Thursday, December 15, 2011 3:02 PM
  • Since you said you are doing console

     

    Module Module1

        Sub Main()
            For Each s As String In My.Application.CommandLineArgs
                Console.WriteLine(s)
            Next
            Console.ReadKey()
        End Sub

    End Module


    Serial Port     Random       Check Internet Connection     Microsoft® Community Contributor      My internet telepathy is down!
    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein
    Thursday, December 15, 2011 3:29 PM
  • I placed that code at the very top of the code window (above all other code that is already in the app). Added a command to the debugging command line dialog. No errors when debugging, but nothing happened other than the utility app ran as normal (Form1 window displayed).

    You can download and view the source app files.


    Thursday, December 15, 2011 3:40 PM
  • I don't download code.  You can paste the code here if you want.  To test the code I posted create a new console app, and copy / paste what I provided. Remember to add command line args to the debugger.

    Serial Port     Random       Check Internet Connection     Microsoft® Community Contributor      My internet telepathy is down!
    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein
    • Edited by dbasnett Thursday, December 15, 2011 3:48 PM
    Thursday, December 15, 2011 3:47 PM
  • Thanks for your continued help. I'm making some progress.

    OK, I think I found the problem and solved it. There was also a timer repainting the Form, so if I place the code to check the command arg in the timer event that calls the repaint function, it works. If "run" is present, then all runs OK and repaints, if not, a blank (un-repainted) form.

    About as good as I can do for now. Maybe I can refine this as I learn more.

     

    Much thanks.

    • Edited by saratogacoach Thursday, December 15, 2011 4:58 PM
    • Marked as answer by saratogacoach Thursday, December 15, 2011 4:59 PM
    Thursday, December 15, 2011 4:01 PM