PDA

View Full Version : QProcess cmd



Nykoo
30th March 2008, 12:15
I am learning how to use QProcess to communicate with cmd.exe on WinXP. I wrote the following code, to send and receive messages and commands from my app to cmd.exe. This is a console application, not useful at all I know. I just wrote the simplier code possible to show you the error:


#include <QByteArray>
#include <QProcess>
#include <iostream>
#include <string>

using namespace std;

int main(int argc,char** argv)
{
QProcess cmd;
cmd.start("cmd");
if (!cmd.waitForStarted())
return false;
cmd.waitForReadyRead();
QByteArray result = cmd.readAll();
cout << result.data();

string str;
getline(cin,str);
while(str != string("exit"))
{
cmd.write(str.c_str());
cmd.write("\n");
cmd.waitForReadyRead();
result = cmd.readAll();
cout << result.data();
getline(cin,str);
}
}

Everything works well, except that the commands I send are delayed. For instance:

1) I launch my console application, this appears:


Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>

2) I write "help" then press enter. Now the prompt looks like that:


Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>
help

3) Then I press enter again, and my prompt displays:


Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>
help
Pour plus d'informations sur une commande spécifique, entrez le nom de la commande HELP.
ASSOC Affiche ou modifie les applications associées aux extensions de...


It prints the cmd help, but only when I press enter twice. I can't figure out why there is this delay.

When I press enter my program is at the line 24 and wait to be able to read the cmd.exe output (cmd.waitForReadyRead();). When it is possible, result takes this output and prints it to my console application. I don't understand why result prints "help" and not the actual help of cmd.

Thanks in advance I hope I was clear ;)

Nykoo
31st March 2008, 10:50
Oh my bad! Sorry in fact I just have to wait to cmd to send the help content without press enter again. I was just too hurry.

But I still can't see why the word that I type (here the word "help") reapears below:


Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

[path_to_my_current_dir]>
help

It's better with a picture:
I type "help" on the white line and after it looks like that:
http://www.enregistrersous.com/images2/15724002120080331114602.jpg (http://www.enregistrersous.com/images2/2/15724002120080331114602.html)

The green QTextEdit only receive data with the function cmd.readAll(); cmd is a QProcess. So it shouldn't receive the word "help" but only the help content that follows.