Results 1 to 3 of 3

Thread: QProcess start a console program

  1. #1
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default QProcess start a console program

    I used QProcess to start an external console program called client.exe.
    Qt Code:
    1. QProcess *CommProcess = new QProcess();
    2. CommProcess->start("E:/work/client.exe");
    3. if (CommProcess->state() == QProcess::Starting)
    4. cout << "client program is starting" << endl;
    5. else if (CommProcess->state() == QProcess::Running)
    6. cout << "client program is running" << endl;
    7. else if (CommProcess->state() == QProcess::NotRunning)
    8. cout << "client program is NOT running" << endl
    9. << "Error Code : " << CommProcess->error() << endl;
    To copy to clipboard, switch view to plain text mode 

    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 ?

  2. #2
    Join Date
    Aug 2006
    Posts
    44
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QProcess start a console program

    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.

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

    Shawn (9th November 2007)

  4. #3
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QProcess start a console program

    I used QProcess::waitForReadyRead() like this
    Qt Code:
    1. if (CommProcess->waitForReadyRead(10000))
    2. {
    3. QString str = CommProcess->readAllStandardOutput().constData();
    4. cout << str.toStdString() << endl;
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. I start a external fortram program, how to terminate?
    By dolphins in forum Qt Programming
    Replies: 3
    Last Post: 28th October 2007, 14:07
  2. QProcess and console programs
    By bond_e in forum Qt Programming
    Replies: 10
    Last Post: 13th July 2007, 09:39
  3. Questions about kill() and start() of QProcess
    By mp33919 in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2007, 14:00
  4. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 05:19
  5. How to run a console program "silently"?
    By fullmetalcoder in forum Qt Programming
    Replies: 9
    Last Post: 23rd July 2006, 11:03

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.