Results 1 to 12 of 12

Thread: QProcess problem with windows batch file

  1. #1
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QProcess problem with windows batch file

    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:
    Qt Code:
    1. process1.start("bin/test.bat");
    2. process1.waitForFinished();
    3. QString out1=process1.readAll();
    To copy to clipboard, switch view to plain text mode 

    Everything seems normal until I added a line before calling start:
    Qt Code:
    1. process1.setWorkingDirectory(QDir::currentPath()+"/bin");
    To copy to clipboard, switch view to plain text mode 

    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?
    Attached Files Attached Files
    Last edited by bood; 6th January 2006 at 07:52.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    Print the contents of the QDir::currentPath(). See if this is Exactly what you want.

    I think you might need the
    Qt Code:
    1. QString QCoreApplication::applicationDirPath();
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    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
    Last edited by bood; 5th January 2006 at 12:52.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess problem with windows batch file

    Quote Originally Posted by bood
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    Quote Originally Posted by wysota
    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.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess problem with windows batch file

    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?
    Last edited by wysota; 5th January 2006 at 14:35.

  7. #7
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    Quote Originally Posted by wysota
    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.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess problem with windows batch file

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    Quote Originally Posted by bood
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    Quote Originally Posted by jacek
    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.

    Quote Originally Posted by wysota
    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
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  11. #11
    Join Date
    Jan 2006
    Location
    Shanghai, China
    Posts
    52
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess problem with windows batch file

    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.
    1. Users don't have the manual, and if they did, they wouldn't read it.
    2. In fact, users can't read anything, and if they could, they wouldn't want to.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess problem with windows batch file

    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.

Similar Threads

  1. Qt + Portmidi in Windows... libraries problem
    By hipogrito in forum Newbie
    Replies: 2
    Last Post: 19th June 2009, 01:01
  2. Draging a non-existing file to the Windows Desktop
    By klaus1111 in forum Qt Programming
    Replies: 13
    Last Post: 20th September 2007, 12:47
  3. Replies: 3
    Last Post: 4th September 2007, 15:40
  4. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 01:25
  5. QProcess +standard error + windows
    By antonio.r.tome in forum Qt Programming
    Replies: 0
    Last Post: 18th April 2006, 15:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.