Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
Shell commands


Message 1 in thread

I would like to execute shell commands from my program. QProcess works in
most cases, but not the following:

    QProcess::execute("dir > C:\dirlist.txt")

The file is never created.

I have no control over the strings that the user will actually use. They
need it to work exactly as if it had been executed on the command line. Is
there a better class to use? (I haven't found it yet if there is.) I cannot
find a way to tell QProcess to do it.

Keith


--
 [ signature omitted ] 

Message 2 in thread

On Wednesday 11 April 2007 18:38, Keith Esau wrote:
> I would like to execute shell commands from my program. QProcess works in
> most cases, but not the following:
>
>     QProcess::execute("dir > C:\dirlist.txt")

If I understand correctly "dir > C:\dirlist.txt" is the user string.
The problem with "dir" is, it's an built-in command of cmd.exe or command.com.
You may filter shell commands like "dir" and prepend the shell interpreter to 
your QProcess::execute() call like

QProcess::execute("cmd.exe /k dir > C:\dirlist.txt")

I'm not sure if cmd.exe can be called like that, I have no Windows to test, 
but you definitely have to use the interpreter in you QProcess call.

regards
Stefan

--
 [ signature omitted ] 

Message 3 in thread

I'm not sure this would solve the problem, but using DIR was just an
example. It would be 'ls' on Unix. The problem is that the output file is
not created.

Essentially, I want the Qt equivalent to the Unix system(const char*)
function that will work on Windows (CreateProcess(NULL, command, ...)). It
is up to my users to create appropriate strings for the platform, I just
need to execute them _without_ internal buffering or access to stdout and
stderr.


On 04-11-2007 12:01 PM, "Stefan Sander" wrote:

> On Wednesday 11 April 2007 18:38, Keith Esau wrote:
>> I would like to execute shell commands from my program. QProcess works in
>> most cases, but not the following:
>> 
>>     QProcess::execute("dir > C:\dirlist.txt")
> 
> If I understand correctly "dir > C:\dirlist.txt" is the user string.
> The problem with "dir" is, it's an built-in command of cmd.exe or command.com.
> You may filter shell commands like "dir" and prepend the shell interpreter to
> your QProcess::execute() call like
> 
> QProcess::execute("cmd.exe /k dir > C:\dirlist.txt")
> 
> I'm not sure if cmd.exe can be called like that, I have no Windows to test,
> but you definitely have to use the interpreter in you QProcess call.
> 
> regards
> Stefan


--
 [ signature omitted ] 

Message 4 in thread

On 11.04.07 12:18:45, Keith Esau wrote:
> I'm not sure this would solve the problem, but using DIR was just an
> example. It would be 'ls' on Unix. The problem is that the output file is
> not created.
> 
> Essentially, I want the Qt equivalent to the Unix system(const char*)
> function that will work on Windows (CreateProcess(NULL, command, ...)). It
> is up to my users to create appropriate strings for the platform, I just
> need to execute them _without_ internal buffering or access to stdout and
> stderr.

Uhm, system is different from CreateProcess I think, system really
executes "/bin/sh"+<what you give as param>, whereas I think
CreateProcess only executes what you give as param. For QProcess you
need to prepend the /bin/sh if you want the command to be run in a
shell. (Or cmd.exe for Windows)

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

On Wednesday 11 April 2007 11:18, Keith Esau wrote:
> I'm not sure this would solve the problem, but using DIR was just an
> example. It would be 'ls' on Unix. The problem is that the output file is
> not created.
>
> Essentially, I want the Qt equivalent to the Unix system(const char*)
> function that will work on Windows (CreateProcess(NULL, command, ...)). It
> is up to my users to create appropriate strings for the platform, I just
> need to execute them _without_ internal buffering or access to stdout and
> stderr.

system() is part of ANSI C (as well as POSIX, etc.) so the equivalent of 
system() on Windows is... well, system(), isn't it?

--
 [ signature omitted ] 

Message 6 in thread

Thanks everyone. As usual, I was trying to use a solution that was too
complex...

system() should work for what I need. I was looking at old code (that we
ported) and we didn't used to use system() on Windows, so I assumed Windows
didn't have it.

Keith

On 04-11-2007 12:25 PM, "Chris Thompson" wrote:

> On Wednesday 11 April 2007 11:18, Keith Esau wrote:
>> I'm not sure this would solve the problem, but using DIR was just an
>> example. It would be 'ls' on Unix. The problem is that the output file is
>> not created.
>> 
>> Essentially, I want the Qt equivalent to the Unix system(const char*)
>> function that will work on Windows (CreateProcess(NULL, command, ...)). It
>> is up to my users to create appropriate strings for the platform, I just
>> need to execute them _without_ internal buffering or access to stdout and
>> stderr.
> 
> system() is part of ANSI C (as well as POSIX, etc.) so the equivalent of
> system() on Windows is... well, system(), isn't it?


--
 [ signature omitted ] 

Message 7 in thread

On 4/11/07, Keith Esau <keith.esau@xxxxxxx> wrote:
> I'm not sure this would solve the problem, but using DIR was just an
> example. It would be 'ls' on Unix. The problem is that the output file is
> not created.
>
> Essentially, I want the Qt equivalent to the Unix system(const char*)
> function that will work on Windows (CreateProcess(NULL, command, ...)). It
> is up to my users to create appropriate strings for the platform, I just
> need to execute them _without_ internal buffering or access to stdout and
> stderr.

So use Windows' "system". It's in process.h. QProcess just exec's
programs - it doesn't
support shell constructs like I/O redirection.

http://msdn2.microsoft.com/en-us/library/277bwbdz(VS.71).aspx
-- 
 [ signature omitted ] 

Message 8 in thread

You can always do a #Define to determine if you need to create a shell.

#ifdef _WIN32
#define CMD = ""
#else
#define CMD = "/bin/sh"

// then in the code
Qstring sCMD = CMD;
QStringList slArgs;
if (sCMD.trimmed().size()) // were creating a shell
{
	slArgs << "-c" << "ls /tmp > /tmp/listing.txt";
}
else  // No shell being created, must enter a command
{
	sCMD = "DIR";
	slArgs << "C:\\temp > C:\\temp\listing.txt";
}
QProcess *pList = new QProcess;
pList ->start(sCmd, slArgs);
if (!pList ->waitForFinished())
{
	qDebug(pList ->errorString());
} 
else
{
	qDebut(pList ->readAll());
}
delete pList;

I didn't get a chance to actually try this, but I think it would work.

Karl

-----Original Message-----
From: Andrew Medico [mailto:a.medico@xxxxxxxxx] 
Sent: Wednesday, April 11, 2007 12:35 PM
To: Keith Esau
Cc: Qt Interest
Subject: Re: Shell commands

On 4/11/07, Keith Esau <keith.esau@xxxxxxx> wrote:
> I'm not sure this would solve the problem, but using DIR was just an
> example. It would be 'ls' on Unix. The problem is that the output file is
> not created.
>
> Essentially, I want the Qt equivalent to the Unix system(const char*)
> function that will work on Windows (CreateProcess(NULL, command, ...)). It
> is up to my users to create appropriate strings for the platform, I just
> need to execute them _without_ internal buffering or access to stdout and
> stderr.

So use Windows' "system". It's in process.h. QProcess just exec's
programs - it doesn't
support shell constructs like I/O redirection.

http://msdn2.microsoft.com/en-us/library/277bwbdz(VS.71).aspx
-- 
 [ signature omitted ] 

Message 9 in thread

I've tried this option

With Win XP shell windows doesn't appear, but it does when running in Win 98

Joel

-----Mensaje original-----
De: Karl Ruetz [mailto:Karl.Ruetz@xxxxxxxxxxxx]
Enviado el: miércoles, 11 de abril de 2007 13:57
Para: qt-interest@xxxxxxxxxxxxx
Asunto: RE: Shell commands


You can always do a #Define to determine if you need to create a shell.

#ifdef _WIN32
#define CMD = ""
#else
#define CMD = "/bin/sh"

// then in the code
Qstring sCMD = CMD;
QStringList slArgs;
if (sCMD.trimmed().size()) // were creating a shell
{
	slArgs << "-c" << "ls /tmp > /tmp/listing.txt";
}
else  // No shell being created, must enter a command
{
	sCMD = "DIR";
	slArgs << "C:\\temp > C:\\temp\listing.txt";
}
QProcess *pList = new QProcess;
pList ->start(sCmd, slArgs);
if (!pList ->waitForFinished())
{
	qDebug(pList ->errorString());
}
else
{
	qDebut(pList ->readAll());
}
delete pList;

I didn't get a chance to actually try this, but I think it would work.

Karl

-----Original Message-----
From: Andrew Medico [mailto:a.medico@xxxxxxxxx]
Sent: Wednesday, April 11, 2007 12:35 PM
To: Keith Esau
Cc: Qt Interest
Subject: Re: Shell commands

On 4/11/07, Keith Esau <keith.esau@xxxxxxx> wrote:
> I'm not sure this would solve the problem, but using DIR was just an
> example. It would be 'ls' on Unix. The problem is that the output file is
> not created.
>
> Essentially, I want the Qt equivalent to the Unix system(const char*)
> function that will work on Windows (CreateProcess(NULL, command, ...)). It
> is up to my users to create appropriate strings for the platform, I just
> need to execute them _without_ internal buffering or access to stdout and
> stderr.

So use Windows' "system". It's in process.h. QProcess just exec's
programs - it doesn't
support shell constructs like I/O redirection.

http://msdn2.microsoft.com/en-us/library/277bwbdz(VS.71).aspx
--
 [ signature omitted ] 

Message 10 in thread

"Keith Esau" <keith.esau@xxxxxxx> wrote in message
news:C24282A5.92A1%keith.esau@xxxxxxxxxx
> I'm not sure this would solve the problem, but using DIR was just an
> example. It would be 'ls' on Unix. The problem is that the output file is
> not created.
>
> Essentially, I want the Qt equivalent to the Unix system(const char*)
> function that will work on Windows (CreateProcess(NULL, command, ...)). It
> is up to my users to create appropriate strings for the platform, I just
> need to execute them _without_ internal buffering or access to stdout and
> stderr.

Well system isn't exactly a unix function.  For
example,
std::system("dir > c:/test.txt");

works on my box running win2k.  IIRC the
return values are different though.  This is going to
pop up a console box though.  I'm not sure how to
avoid that unless using CreateProcess with the correct
flags.

--
 [ signature omitted ] 

Message 11 in thread

That may be why the old code used CreateProcess, to minimize/hide the box.


On 04-11-2007 12:49 PM, "Duane Hebert" wrote:

> 
> "Keith Esau" <keith.esau@xxxxxxx> wrote in message
> news:C24282A5.92A1%keith.esau@xxxxxxxxxx
>> I'm not sure this would solve the problem, but using DIR was just an
>> example. It would be 'ls' on Unix. The problem is that the output file is
>> not created.
>> 
>> Essentially, I want the Qt equivalent to the Unix system(const char*)
>> function that will work on Windows (CreateProcess(NULL, command, ...)). It
>> is up to my users to create appropriate strings for the platform, I just
>> need to execute them _without_ internal buffering or access to stdout and
>> stderr.
> 
> Well system isn't exactly a unix function.  For
> example,
> std::system("dir > c:/test.txt");
> 
> works on my box running win2k.  IIRC the
> return values are different though.  This is going to
> pop up a console box though.  I'm not sure how to
> avoid that unless using CreateProcess with the correct
> flags.


--
 [ signature omitted ] 

Message 12 in thread

>    QProcess::execute("dir > C:\dirlist.txt")

I think the backslash is a problem. You should use C:\\dirlist.txt I 
think. If you use a single backslash, I think it'll act as escape 'd' or 
something.

-- 
 [ signature omitted ] 

Message 13 in thread

The string is not actually in the C++ code, so unless QString parses it (it
better not!), that won't happen.


On 04-11-2007 1:36 PM, "Anurag Singh Rana" wrote:

> 
>>    QProcess::execute("dir > C:\dirlist.txt")
> 
> I think the backslash is a problem. You should use C:\\dirlist.txt I
> think. If you use a single backslash, I think it'll act as escape 'd' or
> something.


--
 [ signature omitted ]