PDA

View Full Version : exit /b 23 in a .bat file returns 0 from QProcess::exitCode



Agroni
14th September 2012, 16:10
I have a batch file (ppp.bat) with only two lines in it:


start .
exit /b 23


The C++ code to call QProcess::start is this:



QString program = "c:\\ppp.bat";

QProcess *myProcess = new QProcess(this);

myProcess->start(program);
myProcess->waitForStarted();

myProcess->waitForFinished(1000);

QString strExitCode( "ExitCode is %1 ExitStatus is %2");

strExitCode = strExitCode.arg( myProcess->exitCode()).arg( (int) myProcess->exitStatus());

ui->textEdit->append( strExitCode);



It always prints 0 for exitCode.

What is needed to have this function return 23?

Oh the OS is Windows XP SP 3 and the Qt is 4.8.1 compiled with VS2010.

Thanks.

d_stranz
14th September 2012, 19:44
Perhaps you need to execute your bat file through a call to "cmd": "cmd c:\\ppp.bat" I believe that this is what is actually happening when Windows executes a bat file, and in your case the error code is probably being returned to the temporary MS-DOS cmd process that then eats it and returns the normal 0 exit code when it exits.

Agroni
14th September 2012, 23:41
That didn't work.

What I tried later was



QString program = "cmd /C \"call c:\\ppp.bat\" ";


Which has no consistent results. Even if this code is ran repeatedly w/o any change. It sometimes returns the right exit code. But sometimes not.

I trusted this documentation here:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/exit.mspx

The second remark says that exit /b will set the ERRORLEVEL to the given exitcode. And will also set the exitcode of the process when cmd.exe quits.

All I am seeing is random behavior.

d_stranz
15th September 2012, 03:13
When you get to running the actual command you want to execute, will this be a bat file also? Or will it be an EXE instead? If it is to be an EXE, then just mock up a dummy one that returns 23 on exit and see if you get something consistent there. It could just be something weird in the bat file execution interface that is causing this.