Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » send/receive arguments to console(cmd/terminal) in GUI app
send/receive arguments to console(cmd/terminal) in GUI app [message #33026] Fri, 01 July 2011 22:47 Go to next message
dave is currently offline  dave
Messages: 14
Registered: July 2011
Promising Member
hi,
my GUI app needs to send and receive arguments to the console(cmd in windows/terminal in linux).
I'm stuck!
will somebody help me out.
thanx in advance.

dave
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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Dave,

Welcome to the forum Cool

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 Wink ).

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 #33070 is a reply to message #33026] Tue, 05 July 2011 22:28 Go to previous messageGo to next message
dave is currently offline  dave
Messages: 14
Registered: July 2011
Promising Member
thank u honza,

i appreciate your help. i had considered these options before but could'nt work it out.

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.

is there anything else i need to do or consider?

also the cmd window remains hidden. can i show the cmd window?

regards,

dave
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 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 #33076 is a reply to message #33026] Wed, 06 July 2011 00:06 Go to previous messageGo to next message
dave is currently offline  dave
Messages: 14
Registered: July 2011
Promising Member
now thats the nicest explanation anyone can receive. my doubts are laid to rest.

hope this explanation benefits a lot of ppl out there. thank you honza. you are the man of the year.

dave

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 Go to previous message
dave is currently offline  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
Previous Topic: Looking for an idea to hide cursor in SqlArray
Next Topic: Problem with GUI_APP_MAIN - example
Goto Forum:
  


Current Time: Sun Apr 28 05:54:11 CEST 2024

Total time taken to generate the page: 0.03022 seconds