Home » Developing U++ » UppHub » Added SysExec package
| Added SysExec package [message #16047] |
Sun, 25 May 2008 09:49  |
mdelfede
Messages: 1310 Registered: September 2007
|
Ultimate Contributor |
|
|
A small package with some SysExec() functions to launch external commands and gather their output and error data.
Not the state of the art, but handy sometimes 
The provided functions are :
bool SysExec(String const &command, String const &args, const VectorMap<String, String> &Environ, String &OutStr, String &ErrStr);
bool SysExec(String const &command, String const &args, String &OutStr, String &ErrStr);
bool SysExec(String const &command, String const &args, const VectorMap<String, String> &Environ, String &OutStr);
bool SysExec(String const &command, String const &args, String &OutStr);
bool SysExec(String const &command, String const &args);
Parameters are :
command : command executable name
args : a line of arguments, space separated
Environ : a VectorMap containing environment data to pass
OutStr : a reference to a string that will contain command output
ErrStr : a reference to a string that will contain command error output
Commands will return true on success, false otherwise.
Caveats :
1- it will block application if launched command hangs
2- no timeout provided... but could be easily added
3- no mean to have some 'progress' indication of running app, but could be easily added
Path will be searched for command, so a complete path is not necessary.
It "should" work on windows too, but I didn't test it yet.
Ciao
Max
[Updated on: Sun, 25 May 2008 09:50] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
| Re: Added SysExec package [message #18924 is a reply to message #16047] |
Fri, 31 October 2008 23:38   |
 |
koldo
Messages: 3458 Registered: August 2008
|
Senior Veteran |
|
|
Sorry mdelfede
From then I have done some functions. Some of them are in http://www.ultimatepp.org/forum/index.php?t=msg&th=3942& amp;.
Between them I have included these functions:
// LaunchFile("Sheet.xls") will open the file with the program asigned by default to open xls files
bool LaunchFile(const String& file);
// LaunchCommand("mplayer myclip.avi", MyCallback) will launch the command line program with args without opening a window, and all the output will be sent to MyCallback(String &) function
int LaunchCommand(const char *cmd, void (*readCallBack)(String &));
// LaunchCommand("mplayer myclip.avi",str) will do the same but sending the output to String str
int LaunchCommand(const char *cmd, String &ret);
The LaunchCommand functions are based in the second sample code included in the August post.
I am using them extensively in funtions working with mplayer, mencoder, ffmpeg, sox and other for a program that handles video and audio (see a screenshot in the post I have included above).
These functions parse the output of the command by "readCallBack" that handles a progress bar and a command window (in the screenshot), this one just for debugging, but goes very well. The functions work well in Windows and Linux.
I want to do a higher level class to handle extern processes in a very simple way and with progress handling included.
Best regards
Koldo
Best regards
Iñaki
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: Added SysExec package [message #19029 is a reply to message #19022] |
Thu, 06 November 2008 15:49   |
mdelfede
Messages: 1310 Registered: September 2007
|
Ultimate Contributor |
|
|
| captainc wrote on Thu, 06 November 2008 00:38 | I've used this for signals on Windows before:
#include <signal.h>
bool run=true;
void sighandler(int sig)
{
run=false;
};
CONSOLE_APP_MAIN
{
signal(SIGABRT, sighandler); // register signal, if it is hit, call sighandler function
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
while(run)
{
// my code logic
}
}
Maybe provide a wrapper around singal.h or look at it's source, use it as a model, and modify to suit Upp.
|
Well, very interesting... I didn't know Windows provided quite a similar signal mechanism as Posix.
I could try to make it fit to my Signals package.....
Max
|
|
|
|
|
|
|
|
|
|
| Re: Added SysExec package [message #19121 is a reply to message #16047] |
Thu, 13 November 2008 12:23   |
 |
koldo
Messages: 3458 Registered: August 2008
|
Senior Veteran |
|
|
Hello Max
It is Ok. SystemLog is useful for me and I will use it in the Sys functions I am doing now.
One question: How to send the log to certain file?
I have tried SysExec but compiling with MSC9 I get a Window error when executing
result = _spawnvpe(_P_WAIT, command, argv, envv);
with this:
"Microsoft Visual C++ Debug Library. Program: ... File: ..\spawnvpe.c Line: 84 Expression: **argv != _T('\0') ...
The program is:
String out;
SysExec("mencoder", "", out);
Compiling with MinGW the program works well.
Previously I had to change
bool SysExec(String const &command, String const &args, String OutStr)
{
String ErrStr;
return SysExec(command, args, Environment(), OutStr, ErrStr);
} as the third argument was declared in the SysExec.h file as "String &OutStr".
Ah!, I have used SVN 625.
Best regards
Koldo
Best regards
Iñaki
|
|
|
|
| Re: Added SysExec package [message #19135 is a reply to message #19121] |
Fri, 14 November 2008 16:51   |
mdelfede
Messages: 1310 Registered: September 2007
|
Ultimate Contributor |
|
|
| koldo wrote on Thu, 13 November 2008 12:23 | Hello Max
It is Ok. SystemLog is useful for me and I will use it in the Sys functions I am doing now.
One question: How to send the log to certain file?
I have tried SysExec but compiling with MSC9 I get a Window error when executing
result = _spawnvpe(_P_WAIT, command, argv, envv);
with this:
"Microsoft Visual C++ Debug Library. Program: ... File: ..\spawnvpe.c Line: 84 Expression: **argv != _T('\0') ...
The program is:
String out;
SysExec("mencoder", "", out);
Compiling with MinGW the program works well.
Previously I had to change
bool SysExec(String const &command, String const &args, String OutStr)
{
String ErrStr;
return SysExec(command, args, Environment(), OutStr, ErrStr);
} as the third argument was declared in the SysExec.h file as "String &OutStr".
Ah!, I have used SVN 625.
Best regards
Koldo
|
It must be &OutStr, as is an output parameter, it gather execution command output. If you set without the &, it's passed as a value parameter.....
Max
|
|
|
|
|
|
| Re: Added SysExec package [message #19194 is a reply to message #19192] |
Wed, 19 November 2008 09:44   |
mdelfede
Messages: 1310 Registered: September 2007
|
Ultimate Contributor |
|
|
| koldo wrote on Wed, 19 November 2008 08:05 | Sorry Max
I did the change in the declaration as in SysExec.cpp it is defined as:
| Quote: | bool SysExec(String const &command, String const &args, String OutStr)
{
String ErrStr;
return SysExec(command, args, Environment(), OutStr, ErrStr);
} // END SysExec()
|
Best regards
Koldo
|
It was my mistake... The correct one is
bool SysExec(String const &command, String const &args, String &OutStr)
Ciao
Max
|
|
|
|
|
|
|
|
| Re: Added SysExec package [message #30816 is a reply to message #30802] |
Mon, 24 January 2011 09:54   |
|
|
| mdelfede wrote on Sun, 23 January 2011 18:36 | Added SysExecGui package, whith gui frontend for password reading (in Linux) and same functions (without password reading) for windows, to have an uniform interface.
It uses SysExec package, adding CtrlLib dependency for gui apps; for non-gui apps, use SysExec which don't have CtrlLib dependencies.
|
Hi Max,
This is a very nice package and definitely useful. I have just one proposal: what about merging SysExec and SysExecGui into one package? I think it makes things easier for user. All you would need to do is:
1) Move files from SysExecGui to SysExec
2) Put #ifdef flagGUI guard around the code in SysExecGui.cpp
3) Put #include SysExecGui.h at the end of SysExec.h (again only #ifdef flagGUI)
4) Add package CtrlLib into SysExec with "when GUI" condition
5) Check the include paths for SysExecGui leftover and few other simple to solve details.
It would make life a little bit easier for people (like me) who sometimes write apps that can be compiled both with GUI and CLI or who start with CLI and later switch to GUI But it is just a proposal, if you think it is not a clean solution feel free to leave it as is, after all it is not that hard to make it work the same way on the user package level 
Best regards,
Honza
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Apr 25 23:47:36 GMT+2 2026
Total time taken to generate the page: 0.01363 seconds
|