PDA

View Full Version : QProcess problem with windows batch file



bood
5th January 2006, 09:54
QT4.1.0-rc1 + Qt-Win Free, VS2003, WinXP
I'm using QProcess::start to execute a batch file in windows
The batch file's under a child folder(bin), core code like this:


process1.start("bin/test.bat");
process1.waitForFinished();
QString out1=process1.readAll();


Everything seems normal until I added a line before calling start:


process1.setWorkingDirectory(QDir::currentPath()+"/bin");


I cannot get anything from out1 then...
And I find that there won't be any problem when replace the batch file with an exe one

You can get an example from attachment
Note: make sure the current directory is the dir contains the 'bin' when running the exefile

p.s. seems I cannot upload the attachment here, system problem?

sunil.thaha
5th January 2006, 10:13
Print the contents of the QDir::currentPath(). See if this is Exactly what you want.

I think you might need the


QString QCoreApplication::applicationDirPath();

bood
5th January 2006, 10:41
Thx for reminding
But this is really what I want, just run my program in right working directory.
And in fact, in my opinion, setWorkingDirectory should not affect the start, it only affects the new process' env

wysota
5th January 2006, 11:07
Thx for reminding
setWorkingDirectory should not affect the start, it only affects the new process' env

It affects the place QProcess looks for the file. Look, that you have given a relative path (dir/file) to that file, so QProcess looks for it there and only there.

bood
5th January 2006, 12:49
It affects the place QProcess looks for the file. Look, that you have given a relative path (dir/file) to that file, so QProcess looks for it there and only there.

I don't think so...

QProcess always looks for the file from the working directory of the process in which QProcess runs. And what QProcess::setWorkingDirectory sets is the working directory of the created process, they are different.

And if you're right, there's no reason that it works well when dealing with exefile. You can try replacing the test.bat with an ordinary exefile, and change the macro EXE defined in main.cpp correspondly. You'll find everything works fine.

wysota
5th January 2006, 14:30
Oh, you're calling a QProcess::setWorkingDirectory() method... You're right then, it doesn't influence where QProcess looks for the program.

How does your script work?

bood
5th January 2006, 15:21
Oh, you're calling a QProcess::setWorkingDirectory() method... You're right then, it doesn't influence where QProcess looks for the program.

How does your script work?

Did you download my example in my first post?

The example has two QProcess, one without setWorkingDirectory called before calling start(QProcess1), while the other does(QProcess2). And both QProcess start a batch file called test.bat under `bin` directory(the batch file just has an echo command to output a string), then readAll is called to collect the output which is later presented in a QMessageBox. You'll find the output collected from QProcess1 is empty while the other works as I expected.

wysota
5th January 2006, 15:54
The process here isn't started at all, so it looks like setWorkingDirectory influences the way QProcess starts the process. The problem is with the script itself, so it seems.

jacek
5th January 2006, 16:05
in my opinion, setWorkingDirectory should not affect the start, it only affects the new process' env
I agree with you. AFAIR is worked this way in Qt3, but apparently in Qt4 QProcess changes the current directory just before it executes the program.

bood
6th January 2006, 03:37
I agree with you. AFAIR is worked this way in Qt3, but apparently in Qt4 QProcess changes the current directory just before it executes the program.
It does not neither in QT4, see my explanation above.


The process here isn't started at all, so it looks like setWorkingDirectory influences the way QProcess starts the process. The problem is with the script itself, so it seems.
You mean my example given above? But where's the problem:(

bood
6th January 2006, 07:49
OK ,Ok, I give up...
I manage to find that it's not the problem of QT4, but rather the trick of M$.
When calling the CreateProcess to run a batch file, the working directory passing in does affect, and in an inexplicable way (maybe cmd.exe related). I've decided to use the absolute path instead.
So if anyone wants the answer, ask M$ for the source code of windows...:P, Seriously, if anyone knows what's going on, please tell me.

wysota
6th January 2006, 09:08
It's not a problem with MS Windows. I experience the same behaviour under Linux (using #!/bin/sh echo hello as a script). But this is definitely connected with scripts. I guess this might be a standard behaviour and has nothing to do with Qt.