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. Issues running .bat on button click
Forum Updated to NodeBB v4.3 + New Features

Issues running .bat on button click

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 850 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.
  • S Offline
    S Offline
    Suroh6
    wrote on 4 Jun 2017, 16:05 last edited by
    #1

    My goal is to run a .bat that I've added as a resource file on a button click. The purpose of this is to get the current windows username via "whoami".

    QString getUser()
    {
       QObject *Parent;
       QProcess *p = new QProcess(Parent);
       p->start(":/CheckWin.bat");
       p->waitForFinished(100);
       WinUser = p->readAllStandardOutput();
       p->terminate();
       return WinUser;
    }
    

    I've tried a few different ways of doing this the above is my latest attempt. Another example is as follows.

    QString getUser()
    {
       QFileInfo program = "C:\\Windows\\System32\\cmd.exe";
       QStringList args;
       args << "whoami/r"; // not sure if /r is even needed
       QObject *Parent;
       QProcess *p = new QProcess(Parent);
       p->execute(program.absoluteFilePath(), args);
       p->waitForBytesWritten(100);
       WinUser = p->readAllStandardOutput();
       p->terminate();
       return WinUser;
    }
    

    Both methods compile without error but both lock the program up. It appears like a crash and is completely frozen. Can anyone tell me what I'm doing wrong?

    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 4 Jun 2017, 18:08 last edited by Chris Kawa 6 Apr 2017, 18:09
      #2

      .bat files are not executables. You can't "run" them so the first example won't work.
      Your handling of the process is also very bad:
      QObject *Parent; - this has an undefined value
      QProcess *p = new QProcess(Parent); - this has a garbage parent so is an undefined behavior and also leaks memory
      p->terminate(); - if the wait finished there's nothing to terminate anymore.
      execute() is a static method and doesn't operate on the instance you called it on.

      As for the second example - don't assume C: drive exists or that Windows is installed in Windows directory.
      To pass a command to cmd you need a /c parameter and the command to execute as another parameter.

      But in this case all this is unnecessary. You don't need cmd. You can run whoami directly:

      QString getUser()
      {
          QProcess p;
          p.start("whoami");
          if (p.waitForStarted() && p.waitForFinished())
              return p.readAllStandardOutput();
      
          p.kill(); //terminate() doesn't work on applications that have no event loop
          return QString(); //error occured
      }
      
      S 1 Reply Last reply 4 Jun 2017, 18:49
      0
      • C Chris Kawa
        4 Jun 2017, 18:08

        .bat files are not executables. You can't "run" them so the first example won't work.
        Your handling of the process is also very bad:
        QObject *Parent; - this has an undefined value
        QProcess *p = new QProcess(Parent); - this has a garbage parent so is an undefined behavior and also leaks memory
        p->terminate(); - if the wait finished there's nothing to terminate anymore.
        execute() is a static method and doesn't operate on the instance you called it on.

        As for the second example - don't assume C: drive exists or that Windows is installed in Windows directory.
        To pass a command to cmd you need a /c parameter and the command to execute as another parameter.

        But in this case all this is unnecessary. You don't need cmd. You can run whoami directly:

        QString getUser()
        {
            QProcess p;
            p.start("whoami");
            if (p.waitForStarted() && p.waitForFinished())
                return p.readAllStandardOutput();
        
            p.kill(); //terminate() doesn't work on applications that have no event loop
            return QString(); //error occured
        }
        
        S Offline
        S Offline
        Suroh6
        wrote on 4 Jun 2017, 18:49 last edited by
        #3

        @Chris-Kawa I see, thanks, mate I don't do much work with windows!

        1 Reply Last reply
        0

        1/3

        4 Jun 2017, 16:05

        • Login

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