Results 1 to 9 of 9

Thread: Problems launching a windows exe from Qt (using QProcess)

  1. #1
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Problems launching a windows exe from Qt (using QProcess)

    I'm using the following code to launch a process. The program exists and I can run it on the command line. The code snippet from the slot/method trying to launch the program:
    Qt Code:
    1. process = new QProcess; // process is a member of my class typed QProcess*
    2. connect(
    3. &process, SIGNAL (error(QProcess::ProcessError)),
    4. this, SLOT(sdLaunchError (QProcess::ProcessError)));
    5.  
    6. QString path("C:\\Program Files\\My Compay\\MyCompanyMonitor");
    7.  
    8. QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    9. process->setWorkingDirectory(path);
    10. QString fullPath = path + ";" + env.value("Path");
    11. env.insert("PATH", fullPath);
    12. process->setProcessEnvironment(env);
    13.  
    14. process->start("Monitor");
    To copy to clipboard, switch view to plain text mode 
    The error I get when I call errorString from sdLaunchError (the slot called when the QProcess::error signal is called) is "No such file or directory". The file is definitely there. I've tried changing the call to QProcess::setWorkingDirectory to the following (so that the path with spaces is enclosed in quotes):
    Qt Code:
    1. process.setWorkingDirectory("\"" + path + "\"");
    To copy to clipboard, switch view to plain text mode 
    I tried adding QIODevice::NotOpen to QProcess::start call. I tried changing the call to ::start to include the extension:
    Qt Code:
    1. process.start("Monitor.exe);
    To copy to clipboard, switch view to plain text mode 
    In a command prompt, I emulated this in the following way:
    C:
    cd \
    set PATH=C:\Program Files\My Company\MyCompanyMonitor;%PATH%
    Monitor

    And the program runs. What am I doing wrong? (As an aside, I've launched programs several times on Linux with no problems on other projects).

    I'm running with Qt 4.6.0 on Windows XP.

    Thanks in advance!

  2. #2
    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: Problems launching a windows exe from Qt (using QProcess)

    QProcess doesn't understand the "PATH" variable as you think it would. You are passing PATH to environment of the application, meaning that it will be effective once the application is launched. It won't take part in searching for the application to be launched. You need to provide an absolute path to QProcess.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems launching a windows exe from Qt (using QProcess)

    Whilst your OS may use '\' as a directory seperator, Qt uses '/'. You can convert between them for OS-specific function calls that require '\' by using QDir::toNativeSeperators()

  4. #4
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems launching a windows exe from Qt (using QProcess)

    I didn't realize that setting the path wouldn't impact the process - thanks for that info. When I pass the full path including the executable to ::start, it doesn't get parsed correctly. I also tried the following, which resulted in the same problem:
    Qt Code:
    1. String path("C:\\Program Files\\My Company\\MyCompanyMonitor");
    2.  
    3. QString fullPath = "\"" + path + "\\Monitor.exe\"";
    4.  
    5. bool ok = QProcess::startDetached(
    6. fullPath,
    7. "\"" + path + "\"");
    8.  
    9. if (!ok)
    10. {
    11. QMessageBox::critical(
    12. NULL, QString(), tr("Unable to run the Control Panel\n"));
    13. }
    To copy to clipboard, switch view to plain text mode 

    Note that I'm passing the full path. It still won't start the program

    I'll try switching to "/" and get back to (this always worked on Linux!).

    Thanks

  5. #5
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems launching a windows exe from Qt (using QProcess)

    Ok, I tried changing "\\" to "/". I tried adding "C:\\Program Files\\My Company\\MyCompanyMonitor" to the system path, restarted a command prompt and made sure the system path change worked (i.e., can I simply type "Monitor" and have it run) - and it worked.

    But my application still can't launch the executable...this is REALLY frustrating and puzzling. I've debugged into QProcess code and can't seem to find why CreateProcess fails... I'm not a windows guru, but it seems to me that this should work...

    Any help is greatly appreciated.

    Thanks

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems launching a windows exe from Qt (using QProcess)

    Maybe the file can be found, but can't be run? It could be failing to find a dependancy. If your debugging into QProcess code, you should post the result from GetLastError() after the CreateProcess. Even better, you could go here -> http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx and look up the error code.

  7. #7
    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: Problems launching a windows exe from Qt (using QProcess)

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems launching a windows exe from Qt (using QProcess)

    I managed to figure out the problem! I had to include the path to the program AND include quotes around it. And the working directory had to be correct in that it had to include "\" not "/" or create process returned invalid directory. So the working code looks like:
    Qt Code:
    1. if (_mon)
    2. return;
    3.  
    4. _mon = new QProcess(this);
    5.  
    6. QString path("C:\\Program Files\\My Company\\MyMonitor");
    7.  
    8. _mon->setWorkingDirectory(path);
    9.  
    10. QString program = "\"" + path + "/Monitor.exe" + "\"";
    11. _mon->start(program);
    12. ...
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2011
    Posts
    4
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problems launching a windows exe from Qt (using QProcess)

    I have encountered the same problem. The solution smacchia described works, although it is a little messy (it produces ""C:\\Program Files\\My Company\\MyMonitor/Monitor.exe"" as a path, with the quotes). The way I found it to work and also look nicer is to set the working directory to the directory of the application to launch and then simply call start("myapplication.exe").

    Qt Code:
    1. QProcess* process = new QProcess(this);
    2. process->setWorkingDirectory(path_of_application);
    3. process->start("application.exe");
    To copy to clipboard, switch view to plain text mode 

    I believe this shouldn't be necessary and it should run with the full path. This might be a bug, though I'm not sure about the exact circumstances yet. It may worth mentioning that the application I'm launching is a .NET application.

Similar Threads

  1. QProcess::finished problems again
    By mdicosimo in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2009, 22:43
  2. Windows focus / Windows Shutdown Problems
    By December in forum Qt Programming
    Replies: 6
    Last Post: 22nd October 2007, 15:10
  3. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 01:25
  4. need help for QProcess under windows
    By patcito in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2006, 20:54
  5. Qprocess never end in MS windows
    By antonio.r.tome in forum Qt Programming
    Replies: 12
    Last Post: 23rd February 2006, 13:35

Tags for this Thread

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.