Results 1 to 6 of 6

Thread: QProcess does not work in service

  1. #1
    Join Date
    Mar 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QProcess does not work in service

    Hi all,

    I tried starting my QProcess in my service but it doesn't work, although it works nicely in an other program without service.
    I want by my service to execute an other program.
    there is the same code i userd in the service and the simple program :

    Qt Code:
    1. if(m_pProcess == NULL)
    2. {
    3. m_pProcess = new QProcess;
    4. m_pProcess->setEnvironment( QProcess::systemEnvironment() );
    5. m_pProcess->setProcessChannelMode( QProcess::MergedChannels );
    6.  
    7. }
    8.  
    9. if(m_pProcess)
    10. {
    11. bool bValueStart = m_pProcess->startDetached("myProgram.exe");
    12. m_pProcess->waitForStarted();
    13. if(bValueStart)
    14. {
    15. qDebug()<< "OK";
    16. return OK;
    17.  
    18. }
    19. else
    20. {
    21. qDebug()<< "ERR";
    22. return ERR;
    23. }
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess does not work in service

    Try this:

    Qt Code:
    1. QString filename = "myProgram.exe";
    2.  
    3. if(QSysInfo::windowsVersion() < QSysInfo::WV_VISTA) {
    4. return QProcess::startDetached(filename);
    5. } else {
    6. int result = (int)::ShellExecuteA(0, "open", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
    7. if (result != ERROR_FILE_NOT_FOUND || ERROR_PATH_NOT_FOUND || ERROR_BAD_FORMAT || SE_ERR_ASSOCINCOMPLETE || SE_ERR_DDEFAIL || SE_ERR_FNF|| SE_ERR_OOM || SE_ERR_PNF || SE_ERR_SHARE) {
    8. if (SE_ERR_ACCESSDENIED == result) {
    9. return (int)::ShellExecuteA(0, "runas", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL) > 32;
    10. }
    11. } else {
    12. Tools::showMessageBox(QObject::tr("App"), QObject::trUtf8("Problem with run %1").arg(filename));
    13. }
    14. return result > 32;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    You should create a manifest file, sometimes you need administrator privileges...
    eg http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Regards

  3. The following user says thank you to Coder5546 for this useful post:

    hassinoss (6th June 2014)

  4. #3
    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 does not work in service

    Quote Originally Posted by yasjera View Post
    Hi all,

    I tried starting my QProcess in my service but it doesn't work, although it works nicely in an other program without service.
    I want by my service to execute an other program.
    Don't use relative paths to the program.
    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.


  5. #4
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess does not work in service

    Thanx for your replies , i had the same problem , but what i want more is if there is any way to do it in order to use it in other platforms not just windows

  6. #5
    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 does not work in service

    Yes. Don't use relative paths as the current working directory of a manually started program is different to when it is ran as a service.
    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.


  7. #6
    Join Date
    Nov 2012
    Location
    Poland/UK
    Posts
    28
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess does not work in service

    Qt Code:
    1. #ifdef Q_OS_UNIX
    2. QString filename = "./MyApp";
    3. #elif defined Q_OS_MAC
    4. QString filename = "MyApp.dmg"; // I'm not sure, I not using Mac Os
    5. #else
    6. QString filename = "MyApp.exe";
    7. #endif
    8.  
    9. #ifdef Q_OS_WIN
    10. if(QSysInfo::windowsVersion() < QSysInfo::WV_VISTA) {
    11. return QProcess::startDetached(filename); //... Windows 2000, XP
    12. } else { // Windows Vista, 7, 8
    13. int result = (int)::ShellExecuteA(0, "open", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
    14. if (SE_ERR_ACCESSDENIED == result)
    15. {
    16. return (int)::ShellExecuteA(0, "runas", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL) > 32;
    17. }else {
    18. qDebug() << "Problem with run application";
    19. }
    20. return result > 32;
    21. }
    22. #else
    23. return QProcess::startDetached(filename); //Linux, Mac
    24. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by Coder5546; 6th June 2014 at 11:08.

Similar Threads

  1. IPC using QProcess , connect and finish does not work
    By PstdEr in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2013, 00:43
  2. QProcess::start not work in windows 7
    By renocz in forum Qt Programming
    Replies: 0
    Last Post: 3rd April 2013, 14:47
  3. Replies: 2
    Last Post: 18th December 2010, 22:13
  4. How to build an application to work as a Window Service
    By danielperaza in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2008, 23:54

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.