|
|
Home » Community » Newbie corner » send/receive arguments to console(cmd/terminal) in GUI app
|
Re: send/receive arguments to console(cmd/terminal) in GUI app [message #33039 is a reply to message #33026] |
Mon, 04 July 2011 08:14   |
|
Hi Dave,
Welcome to the forum
Do I understand correctly that you want to execute command line applications from within your app? There is several ways to do that.
First option is the Sys function:int Sys(const char *cmd, String& output);
String Sys(const char *cmd); It just executes the command in cmd (which can also contain parameters, just as you would write them to cmd/xterm). The first version returns the exit code of the command and stores all the output it produces in the String output. The second variant returns the output directly (it is meant for really simple commands that never fail ).
Second option is to use LocalProcess class. It allows finer control over the executed process. You can interact with it using Read() and Write() functions. For details please refer to the documentation.
Best regards,
Honza
|
|
|
|
Re: send/receive arguments to console(cmd/terminal) in GUI app [message #33074 is a reply to message #33070] |
Tue, 05 July 2011 23:39   |
|
dave wrote on Tue, 05 July 2011 22:28 | like you said, the string sys function worked just fine. it was heartning to see calc.exe run, but things were short lived. when i tried to give other commands, it failed to execute and GUI stopped responding, even though its process was running (as seen in task manager).
when calc.exe did run, the GUI app was left unresponsive till calc.exe was closed.
|
It is because Sys() waits till the executed program ends, so it can return its output. If you wish to use your app in between, use LocalProcess:
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class App:public TopWindow {
typedef App CLASSNAME;
Array<LocalProcess> p;
Button b;
public:
App(){
Add(b.HCenterPos().VCenterPos());
b.SetLabel("Push me!");
b<<=THISBACK(Launch);
}
void Launch(){
// we add a new process to the array and start a command
p.Add().Start("calc.exe");
}
~App(){
// if you wish the processes run after closing the app, detach them like this:
for(int i=0;i<p.GetCount();i++){
p[i].Detach();
}
}
};
GUI_APP_MAIN{
App().Sizeable().Run();
}
dave wrote on Tue, 05 July 2011 22:28 | also the cmd window remains hidden. can i show the cmd window?
| IIRC it is Windows itself who decides whether to open the cmd or not. If a GUI app is executed the cmd prompt is not displayed and the apps output/input is not bound to it. (Try executing calc.exe from cmd, if I'm correct it will start the calculator and immediately let you work with the shell). I haven't use windows in a while though, so I might be slightly misinterpreting some things.
You might also try to execute whatever you want as "cmd /c your_command", that should open the cmd.
Honza
|
|
|
|
Re: send/receive arguments to console(cmd/terminal) in GUI app [message #33191 is a reply to message #33076] |
Thu, 14 July 2011 14:49  |
dave
Messages: 14 Registered: July 2011
|
Promising Member |
|
|
local process is the way to go.
for example..
int SysConsole::System(const char *cmd)
{
if(!IsOpen())
Open();
list.Add(AttrText(cmd).SetFont(font().Bold()).Ink(LtBlue));
int ii = list.GetCount();
LocalProcess p;
if(!p.Start(cmd))
return -1;
String out;
while(p.IsRunning()) {
String h = p.Get();
out.Cat(h);
int lf = out.ReverseFind('\n');
if(lf >= 0) {
AddResult(out.Mid(0, lf + 1));
out = out.Mid(lf + 1);
}
ProcessEvents();
Sleep(h.GetCount() == 0); // p.Wait would be much better here!
}
out.Cat(p.Get());
AddResult(out);
ProcessEvents();
int code = p.GetExitCode();
if(code)
while(ii < list.GetCount()) {
list.Set(ii, 0, AttrText((String)list.Get(ii, 1)).SetFont(font).Ink(LtRed));
ii++;
}
return code;
}
regards
dave
|
|
|
Goto Forum:
Current Time: Tue Apr 29 01:37:31 CEST 2025
Total time taken to generate the page: 0.00768 seconds
|
|
|