PDA

View Full Version : QProcess does not work in service



yasjera
5th June 2014, 10:37
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 :


if(m_pProcess == NULL)
{
m_pProcess = new QProcess;
m_pProcess->setEnvironment( QProcess::systemEnvironment() );
m_pProcess->setProcessChannelMode( QProcess::MergedChannels );

}

QStringList args;
if(m_pProcess)
{
bool bValueStart = m_pProcess->startDetached("myProgram.exe");
m_pProcess->waitForStarted();
if(bValueStart)
{
qDebug()<< "OK";
return OK;

}
else
{
qDebug()<< "ERR";
return ERR;
}

}

Coder5546
5th June 2014, 11:33
Try this:




QString filename = "myProgram.exe";

if(QSysInfo::windowsVersion() < QSysInfo::WV_VISTA) {
return QProcess::startDetached(filename);
} else {
int result = (int)::ShellExecuteA(0, "open", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
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) {
if (SE_ERR_ACCESSDENIED == result) {
return (int)::ShellExecuteA(0, "runas", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL) > 32;
}
} else {
Tools::showMessageBox(QObject::tr("App"), QObject::trUtf8("Problem with run %1").arg(filename));
}
return result > 32;
}
}

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

Regards

wysota
5th June 2014, 12:37
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.

hassinoss
6th June 2014, 10:53
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

wysota
6th June 2014, 10:59
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.

Coder5546
6th June 2014, 11:03
#ifdef Q_OS_UNIX
QString filename = "./MyApp";
#elif defined Q_OS_MAC
QString filename = "MyApp.dmg"; // I'm not sure, I not using Mac Os
#else
QString filename = "MyApp.exe";
#endif

#ifdef Q_OS_WIN
if(QSysInfo::windowsVersion() < QSysInfo::WV_VISTA) {
return QProcess::startDetached(filename); //... Windows 2000, XP
} else { // Windows Vista, 7, 8
int result = (int)::ShellExecuteA(0, "open", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
if (SE_ERR_ACCESSDENIED == result)
{
return (int)::ShellExecuteA(0, "runas", filename.toUtf8().constData(), 0, 0, SW_SHOWNORMAL) > 32;
}else {
qDebug() << "Problem with run application";
}
return result > 32;
}
#else
return QProcess::startDetached(filename); //Linux, Mac
#endif