Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Parsing the Command Line

Parsing the Command Line

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminjsonquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    The wininet library in Windows contains APIs for "cracking" and parsing network URIs into their component parts. Are you aware of any facility in Windows that performs this function for command lines? (Something that I can call from an application, not the parsing that is done when my application is launched.)

    The difficult we do right away... ...the impossible takes slightly longer.

    D G 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      The wininet library in Windows contains APIs for "cracking" and parsing network URIs into their component parts. Are you aware of any facility in Windows that performs this function for command lines? (Something that I can call from an application, not the parsing that is done when my application is launched.)

      The difficult we do right away... ...the impossible takes slightly longer.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Are you talking about the contents of argc and argv?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      Richard Andrew x64R 1 Reply Last reply
      0
      • D David Crow

        Are you talking about the contents of argc and argv?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Yes, and the executable name. I imagine each argument separated into an array, and the executable name separated.

        The difficult we do right away... ...the impossible takes slightly longer.

        D 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Yes, and the executable name. I imagine each argument separated into an array, and the executable name separated.

          The difficult we do right away... ...the impossible takes slightly longer.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Richard Andrew x64 wrote:

          I imagine each argument separated into an array, and the executable name separated.

          All of that is contained in the argv vector. For the executable name, look at the 0th item. For WinMain(), you may have to refer to __argc and __argv instead.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          Richard Andrew x64R 1 Reply Last reply
          0
          • D David Crow

            Richard Andrew x64 wrote:

            I imagine each argument separated into an array, and the executable name separated.

            All of that is contained in the argv vector. For the executable name, look at the 0th item. For WinMain(), you may have to refer to __argc and __argv instead.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            Thank you David. I didn't make it clear enough at first. I meant that I want my application to be able to parse the command line of a separate process. You know how some registry keys have command lines in them? I was wondering if there is something built into Windows that can make it easy to parse them.

            The difficult we do right away... ...the impossible takes slightly longer.

            D 1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Thank you David. I didn't make it clear enough at first. I meant that I want my application to be able to parse the command line of a separate process. You know how some registry keys have command lines in them? I was wondering if there is something built into Windows that can make it easy to parse them.

              The difficult we do right away... ...the impossible takes slightly longer.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Ahh, that's a horse of a different color. If you tried separating the tokens using a space as the delimiter, an issue that I see is that the path\executable name can itself contain a space. This is usually resolved by surrounding that token with quotes. So as you are parsing the whole string, you'd need to keep track of whether you were in a quote or not. If so, then spaces do not count.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                The wininet library in Windows contains APIs for "cracking" and parsing network URIs into their component parts. Are you aware of any facility in Windows that performs this function for command lines? (Something that I can call from an application, not the parsing that is done when my application is launched.)

                The difficult we do right away... ...the impossible takes slightly longer.

                G Offline
                G Offline
                Graham Breach
                wrote on last edited by
                #7

                If you have the command line as a string you can use the CommandLineToArgvW function[^] to split it up.

                Richard Andrew x64R D 2 Replies Last reply
                0
                • G Graham Breach

                  If you have the command line as a string you can use the CommandLineToArgvW function[^] to split it up.

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #8

                  Bingo! Thank you!

                  The difficult we do right away... ...the impossible takes slightly longer.

                  1 Reply Last reply
                  0
                  • G Graham Breach

                    If you have the command line as a string you can use the CommandLineToArgvW function[^] to split it up.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Awesome. I had no idea such a routine existed. :thumbsup:

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups