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. [Solved] Sending input to system("command") prompt Windows

[Solved] Sending input to system("command") prompt Windows

Scheduled Pinned Locked Moved C / C++ / MFC
helpwindows-adminquestionannouncement
9 Posts 5 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.
  • J Offline
    J Offline
    JohnCodding
    wrote on last edited by
    #1

    In my program I'm using system("") command to add a variable into User variables using reg add. In the future I'll want to update the variable value to something else, but if the variable exists it will prompt the user to input Y/N to override it. Is there a way to automatically give the input instead of the user? I was thinking that maybe I could use system("y"), but it still waits for the input from the user, and then gives the error that y is not a command. I can't use Windows.h to modify registry, so I want to find a solution to this. Maybe I'll need system("") for something else in the future that will have the same problem with having to input something. Solution:

    Victor Nijegorodov wrote:

    According to the reg add | Microsoft Docs you can add option /f that will cause the adding the registry entry without prompting for confirmation.

    L D 2 Replies Last reply
    0
    • J JohnCodding

      In my program I'm using system("") command to add a variable into User variables using reg add. In the future I'll want to update the variable value to something else, but if the variable exists it will prompt the user to input Y/N to override it. Is there a way to automatically give the input instead of the user? I was thinking that maybe I could use system("y"), but it still waits for the input from the user, and then gives the error that y is not a command. I can't use Windows.h to modify registry, so I want to find a solution to this. Maybe I'll need system("") for something else in the future that will have the same problem with having to input something. Solution:

      Victor Nijegorodov wrote:

      According to the reg add | Microsoft Docs you can add option /f that will cause the adding the registry entry without prompting for confirmation.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I do not understand how system("") can add a variable to anything. I think we need more details.

      Greg UtasG J 2 Replies Last reply
      0
      • L Lost User

        I do not understand how system("") can add a variable to anything. I think we need more details.

        Greg UtasG Offline
        Greg UtasG Offline
        Greg Utas
        wrote on last edited by
        #3

        He said he's using system("reg add") to do it. I've never used either one, but I assume it's a way for a program to add something to the registry. I think he's now looking for a way for a program to enter "y" on the user's behalf, perhaps to confirm a system command invoked by system(). But I don't know of a way to fake console input. Do you?

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

        <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
        <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

        L 1 Reply Last reply
        0
        • L Lost User

          I do not understand how system("") can add a variable to anything. I think we need more details.

          J Offline
          J Offline
          JohnCodding
          wrote on last edited by
          #4

          system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValue\"");

          Result: "The operation completed successfully." And the variable is added with the given value. But then when wanting to update it:

          system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValueUpdated\"");

          Result: "Value TestVariable exists, overwrite(Yes/No)?" If Y is given as input, then "The operation completed successfully.", and the value is updated. But I want to skip the step where the user has to give the Y input, and send it automatically.

          J V 2 Replies Last reply
          0
          • J JohnCodding

            system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValue\"");

            Result: "The operation completed successfully." And the variable is added with the given value. But then when wanting to update it:

            system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValueUpdated\"");

            Result: "Value TestVariable exists, overwrite(Yes/No)?" If Y is given as input, then "The operation completed successfully.", and the value is updated. But I want to skip the step where the user has to give the Y input, and send it automatically.

            J Offline
            J Offline
            JohnCodding
            wrote on last edited by
            #5

            At first I was thinking, well if I can't modify it, what if I try to first delete it and then add it with the new value, but I get in the same situation, and that is I have to give it an input because when you want to delete a variable, you have to input Y/N.

            system("reg delete \"HKEY_CURRENT_USER\\Environment\" /v TestVariable");

            Result: "Delete the registry value TestVariable (Yes/No)?"

            1 Reply Last reply
            0
            • J JohnCodding

              system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValue\"");

              Result: "The operation completed successfully." And the variable is added with the given value. But then when wanting to update it:

              system("reg add \"HKEY_CURRENT_USER\\Environment\" /v TestVariable /t REG_EXPAND_SZ /d \"TestValueUpdated\"");

              Result: "Value TestVariable exists, overwrite(Yes/No)?" If Y is given as input, then "The operation completed successfully.", and the value is updated. But I want to skip the step where the user has to give the Y input, and send it automatically.

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              According to the [reg add | Microsoft Docs](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/reg-add) you can add option /f that will cause the adding the registry entry without prompting for confirmation.

              J 1 Reply Last reply
              0
              • V Victor Nijegorodov

                According to the [reg add | Microsoft Docs](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/reg-add) you can add option /f that will cause the adding the registry entry without prompting for confirmation.

                J Offline
                J Offline
                JohnCodding
                wrote on last edited by
                #7

                Yeah, that works. I have no idea how I managed to skip that one line that I needed... Thanks!

                1 Reply Last reply
                0
                • Greg UtasG Greg Utas

                  He said he's using system("reg add") to do it. I've never used either one, but I assume it's a way for a program to add something to the registry. I think he's now looking for a way for a program to enter "y" on the user's behalf, perhaps to confirm a system command invoked by system(). But I don't know of a way to fake console input. Do you?

                  Robust Services Core | Software Techniques for Lemmings | Articles
                  The fox knows many things, but the hedgehog knows one big thing.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I thought system("") looked a bit silly. I do wish people would just copy and paste the actual code they are using to make their questions clear.

                  1 Reply Last reply
                  0
                  • J JohnCodding

                    In my program I'm using system("") command to add a variable into User variables using reg add. In the future I'll want to update the variable value to something else, but if the variable exists it will prompt the user to input Y/N to override it. Is there a way to automatically give the input instead of the user? I was thinking that maybe I could use system("y"), but it still waits for the input from the user, and then gives the error that y is not a command. I can't use Windows.h to modify registry, so I want to find a solution to this. Maybe I'll need system("") for something else in the future that will have the same problem with having to input something. Solution:

                    Victor Nijegorodov wrote:

                    According to the reg add | Microsoft Docs you can add option /f that will cause the adding the registry entry without prompting for confirmation.

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

                    Why are you not using the registry API directly rather than going through (antiquated) system() calls?

                    "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