PDA

View Full Version : QProcess start a console program



Shawn
9th November 2007, 03:53
I used QProcess to start an external console program called client.exe.

QProcess *CommProcess = new QProcess();
CommProcess->start("E:/work/client.exe");
if (CommProcess->state() == QProcess::Starting)
cout << "client program is starting" << endl;
else if (CommProcess->state() == QProcess::Running)
cout << "client program is running" << endl;
else if (CommProcess->state() == QProcess::NotRunning)
cout << "client program is NOT running" << endl
<< "Error Code : " << CommProcess->error() << endl;

It turns out with "client program is running". Seems everything is OK.
But actually it doesn't. Because I create a shared memory area in client.exe and open it in the current qt program. But I failed to open that area in current program.
If I run client.exe(both directly or using Visual C++ debugging model) before starting my qt program, everything just turns out OK.
Can anybody give me some hint ?

Eldritch
9th November 2007, 05:31
Just because the program has 'started' doesn't mean the shared memory has been set up yet. I haven't looked at the details of QProcess' implementation, but it seems like a more robust solution for you here would be to:

1) start process and determine that the launch was successful
2) attempt for some period of time via a polling loop to connect to the shared memory

You should have a reasonable notion of how long client.exe takes to set up its shared memory in most cases. Since you're dealing w/ interprocess communication here, on a shared resource, assuming that any specific time will always be enough is less than optimal. I'd suggest having a 'timeout' parameter, and if you fail to be able to connect to the shared memory after that amount of time, give up.

Shawn
9th November 2007, 12:38
I used QProcess::waitForReadyRead() like this

if (CommProcess->waitForReadyRead(10000))
{
QString str = CommProcess->readAllStandardOutput().constData();
cout << str.toStdString() << endl;
}
it works, but come out with another question - the channels. The console program and my qt program both are based on console. And both of them has some output to console. There must be some conflicts.
All I want to do is to open a new cmd window, call a console program, show its output in that cmd window.
What else should I do ?
I am really confused with those streams and chanels.:confused: