Issues running .bat on button click
-
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?
-
.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 inWindowsdirectory.
To pass a command tocmdyou need a/cparameter and the command to execute as another parameter.But in this case all this is unnecessary. You don't need cmd. You can run
whoamidirectly: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 } -
.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 inWindowsdirectory.
To pass a command tocmdyou need a/cparameter and the command to execute as another parameter.But in this case all this is unnecessary. You don't need cmd. You can run
whoamidirectly: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 }@Chris-Kawa I see, thanks, mate I don't do much work with windows!