Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Running commands that are part of the CMD shell with QProcess?

Running commands that are part of the CMD shell with QProcess?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.4k Views
  • 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.
  • Q Offline
    Q Offline
    Qub1
    wrote on last edited by
    #1

    Hey,

    I need to execute a command on Windows, which is part of the CMD shell. So it doesn't have its own file in C:\Windows\system32, it is only available within cmd.exe.

    I've tried simply using:
    @QProcess process;
    process.start("command");@
    where command is the command I'm trying to execute, however it returns an error that the file cannot be found.

    Is there any way to achieve this? Using cmd.exe /C command does not return an error, however it seems the command isn't executed. And simply using cmd.exe command makes the program hang.

    Thanks in advance!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      belab
      wrote on last edited by
      #2

      Hi Qub1,
      did you try to call cmd.exe with command args like the this?
      @
      process.start("cmd.exe command");
      @

      1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        Hi, "cmd.exe command" would work if you'd launch cmd.exe using MSVC's system() call, but Qt uses some exec... flavor.
        So you need to spell out the whole enchilada, for example, say you want to save the path env. variable to a file to in C:, you can do it like this:
        @
        process.start("cmd.exe",QStringList("/c echo %path% > c:\path.txt"));
        @

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qub1
          wrote on last edited by
          #4

          Okay I will try that and edit this post, thanks!

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qub1
            wrote on last edited by
            #5

            I've tried that and it did not work.
            My current code is this:
            @bool createSymbolicLink(const int &type, const QString &linkPath, const QString &targetPath) {
            QString arguments = "/C mklink ";
            if(type == SYMBOLICLINK_DIRECTORY) {
            arguments += "/D ";
            }
            arguments += """ + QDir::toNativeSeparators(linkPath) + "" "" + QDir::toNativeSeparators(targetPath) + """;
            QProcess mklink;
            mklink.start("cmd.exe", QStringList(arguments));

            return mklink.waitForFinished();
            }
            @

            It does return true, so the program executed, however the command isn't executed (I've tested).

            1 Reply Last reply
            0
            • hskoglundH Offline
              hskoglundH Offline
              hskoglund
              wrote on last edited by
              #6

              Hi when I do the mklink command on my Windows 7, I have to do it using an administrator cmd.exe window, i.e. I think the mklink always requires administrator rights. So check that you start your Qt app as administrator.

              Also you can do a qDebug() << arguments; before to mklink.start just to see the command line.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved