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 » U++ Library support » U++ Core » executing commands and viewing it's result
executing commands and viewing it's result [message #23088] Wed, 16 September 2009 23:36 Go to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

hello all

how can i execute a command in CMD with U++ and get it's result back

ex.

string result = neededfun("dir");
Re: executing commands and viewing it's result [message #23089 is a reply to message #23088] Wed, 16 September 2009 23:52 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
There is SysExec package in Bazaar. I think Max wrote it, so if you need some advices you may ask him.
Re: executing commands and viewing it's result [message #23090 is a reply to message #23088] Thu, 17 September 2009 00:42 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

Thanks

i found this command called system but i get this error

error C3861: 'System': identifier not found
Re: executing commands and viewing it's result [message #23091 is a reply to message #23090] Thu, 17 September 2009 02:34 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 TeCNoYoTTa

I would suggest you function String Sys(const char *cmd), which is defined directly in Core package.

Quote:

error C3861: 'System': identifier not found

This sounds like if you forgot to include the header and/or add the package in which System is defined.

Bye
Honza

[Updated on: Thu, 17 September 2009 02:43]

Report message to a moderator

Re: executing commands and viewing it's result [message #23092 is a reply to message #23091] Thu, 17 September 2009 02:47 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

thanks but it's not found also

i included Core/Core.h BTW
Re: executing commands and viewing it's result [message #23094 is a reply to message #23092] Thu, 17 September 2009 06:57 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
TeCNoYoTTa wrote on Thu, 17 September 2009 03:47

thanks but it's not found also

i included Core/Core.h BTW


You have to edit assembly in which you work and put an entry pointing to Bazaar. i.e.
Quote:

/home/andrei/upp/MyApps;/home/andrei/upp/uppsrc;/home/andrei /upp/bazaar

after that you may access packages from bazaar, so SysExec should be accessible.

Create a project, add SysExec package to it and use it freely.
#include <Core/Core.h>
#include <SysExec/SysExec.h>
using namespace Upp;

CONSOLE_APP_MAIN
{
	String output;
	SysExec("ls", "/home/andrei", output);
	Cout() << output;
}
Re: executing commands and viewing it's result [message #23096 is a reply to message #23094] Thu, 17 September 2009 07:12 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

thanks but i tried this code and i got this error
	String output;
	SysExec("dir", "c://", output);
	Cout() << output;


HanafiBardo.obj : error LNK2019: unresolved external symbol "bool __cdecl Upp::SysExec(class Upp::String const &,class Upp::String const &,class Upp::
	String &)" (?SysExec@Upp@@YA_NABVString@1@0AAV21@@Z) referenced in function _main
D:\Program Files\U++\out\MSC9.Force_size\HanafiBardo.exe : fatal error LNK1120: 1 unresolved externals


i dont know where to edit assembly but i copy/paste sysexec folder in U++MyApp folder
Re: executing commands and viewing it's result [message #23098 is a reply to message #23096] Thu, 17 September 2009 08:28 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
Here is explained how to setup assembly.
Did you included <SysExec/SysExec.h> ? (case sensitive)
Re: executing commands and viewing it's result [message #23099 is a reply to message #23098] Thu, 17 September 2009 09:03 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

thanks but this code

	String output="";
	String error="";
	SysExec("dir","C:\\",output,error);
	Cout() << output << "\n"<<error;


this is it's output

Error spawning process

<--- Finished in (0:00.48), exitcode: 0 --->

Re: executing commands and viewing it's result [message #23101 is a reply to message #23099] Thu, 17 September 2009 09:19 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
May you add
Cout() << errno;
after SysExec call and post the number resulted?
Re: executing commands and viewing it's result [message #23102 is a reply to message #23099] Thu, 17 September 2009 09:32 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello TeCNoYoTTa

You can also use the LaunchCommand function in Bazaar/SysInfo package Smile

String LaunchCommand(const char *cmd)

where cmd is just the command line string. It returns the command output.

It is documented and if you see the code in the same SysInfo.cpp you will see many examples of its use.

This function does work for Windows and Linux. The exception is in basic OS commands in Windows like "dir" and "copy". If you want them to work out of a command line and hidden it will be needed something different.
It is not usually necessary as this commands are so basic that their functionality is included in libraries (list files in folder, copy files, ...).

All other console programs will run perfectly.

Best regards
Koldo


Best regards
Iñaki
Re: executing commands and viewing it's result [message #23104 is a reply to message #23088] Thu, 17 September 2009 10:23 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
"dir" is part of cmd.exe in Windows, so you have to call it from C++ like "cmd.exe -some_options_to_run_DIR_command".
Re: executing commands and viewing it's result [message #23106 is a reply to message #23104] Thu, 17 September 2009 11:01 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Yes, mr_ped is right

To do a "dir" in Windows you would have to do:

String str = LaunchCommand("cmd.exe /c dir c:\\");

This is because "dir" is not a program.

Best regards
Koldo


Best regards
Iñaki
Re: executing commands and viewing it's result [message #23124 is a reply to message #23088] Thu, 17 September 2009 20:52 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

I downloaded the latest version of U++ but i get this error :S :S
when compiling sysinfo
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:11:21: error: rpcsal.h: No such file or directory
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:12:21: error: Wbemidl.h: No such file or directory
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:2267:1: error: unterminated #if
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp: In function 'bool GetWMIInfo(Upp::String, Upp::Array<Upp::String>&, Upp::Array<Upp::Value>**)':
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:493: error: 'IWbemLocator' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:493: error: 'pIWbemLocator' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:494: error: 'CLSID_WbemAdministrativeLocator' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:501: error: 'IWbemServices' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:501: error: 'pWbemServices' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:518: error: 'IEnumWbemClassObject' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:518: error: 'pEnumerator' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:525: error: 'WBEM_FLAG_RETURN_IMMEDIATELY' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:535: error: 'IWbemClassObject' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:535: error: 'pClassObject' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:539: error: 'WBEM_INFINITE' was not declared in this scope
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp: In function 'bool TakeWindowPlacement(HWND__*, RECT&, POINT&, POINT&, long int&)':
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:2345: error: 'struct WINDOWPLACEMENT' has no member named 'show'
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:2345: error: expected `;' at end of input
D:\Program Files\U++\bazaar\SysInfo\SysInfo.cpp:2345: error: expected `}' at end of input
Re: executing commands and viewing it's result [message #23125 is a reply to message #23090] Thu, 17 September 2009 20:54 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

TeCNoYoTTa wrote on Thu, 17 September 2009 00:42

Thanks

i found this command called system but i get this error

error C3861: 'System': identifier not found



i tried Sys
	String result = Sys("dir c:/");
	Cout() << result << "\n";


but always return empty string
Re: executing commands and viewing it's result [message #23126 is a reply to message #23088] Thu, 17 September 2009 21:09 Go to previous messageGo to next message
TeCNoYoTTa is currently offline  TeCNoYoTTa
Messages: 138
Registered: July 2008
Location: Egypt
Experienced Member

Thanks All

String ExecuteCMD(String p_CMD)
{
	One<SlaveProcess> sp = StartProcess(p_CMD);
	String Result;
	String k;
	while(sp->IsRunning())
	{
		sp->Read(k);
		Result += k;
	}
	return Result;
}


this function did it
Re: executing commands and viewing it's result [message #23129 is a reply to message #23126] Thu, 17 September 2009 22:23 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello TeCNoYoTTa

You are right. The implementation of LaunchCommand in SysInfo is like yours just using LocalProcess instead of One<SlaveProcess>.

Just in case, SysInfo is complete for Linux and Windows with MSC.

However with MinGW you have to add these files from the MSC install or from other sources:

To Upp\MinGW\include:
- Files Rpcsal.h, DispEx.h, WbemCli.h, WbemDisp.h, Wbemidl.h, WbemProv.h and WbemTran.h
- They can be taken from: \Microsoft SDKs\Windows\v6.1\Include
To Upp\MinGW\lib:
- File wbemuuid.lib
- It can be taken from \Microsoft SDKs\Windows\v6.1\Lib

To simplify it all you can get an updated MinGW 4.4.0 with all includes and libs from http://sourceforge.net/projects/upp/files/upp/1517/mingw_4_4 _0.7z/download

Best regards
Koldo


Best regards
Iñaki
Re: executing commands and viewing it's result [message #23133 is a reply to message #23129] Fri, 18 September 2009 05:50 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
In the usvn package, there is the SysConsole class. You might want to check it.

It is sort of waiting for being moved to CtrlLib or something like that, after perhaps some refurbishing. Maybe you can try it out of usvn context to check what is missing for general use?

Mirek
Re: executing commands and viewing it's result [message #23137 is a reply to message #23088] Fri, 18 September 2009 09:32 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello all

Some posts summarized in one...

For Mirek: About SysConsole

Quote:

In the usvn package, there is the SysConsole class. You might want to check it.

It is sort of waiting for being moved to CtrlLib or something like that, after perhaps some refurbishing. Maybe you can try it out of usvn context to check what is missing for general use?

I have tested it and I find it useful. In fact I have a class with the same functionality...

Please move it to the main library

I will substitute my class with yours to see if anything else could be necessary.

One thing I would do now is to manage '\r':
Some programs as mencoder or ffmpeg show in a line the status of the process, printing lines one over other


For TeCNoYoTTa: You do not need ExecuteCMD

... as Sys() function just does the same:

int Sys(const char *cmd, String& out)
{
	out.Clear();
	LocalProcess p;
	if(!p.Start(cmd))
		return -1;
	while(p.IsRunning()) {
		out.Cat(p.Get());
		Sleep(1); // p.Wait would be much better here!
	}
	out.Cat(p.Get());
	return p.GetExitCode();
}

For Mirek and everybody: To organize us a little

I know it is not the first time I tell it, I see many times that different people do the same thing.
It would be great to have a place to put the "THINGS TO DO" so people that have time can take them and do it. If somebody has not time to follow can ask for help and other people can "pass the baton on to the next relay runner".

We had three implementations of Sys(), now we have two implementations of cryptography.

The reason I announce the things I will do months before is just to coordinate with others, but it is not the best way.

Mirek: Thank you for having Bazaar inside Upp. Other projects do not have the same.

However perhaps it is the time to have some kind of better Bazaar inside or outside Upp web page:
- With a project list with description: All the Bazaar packages visible
- With all documentation: Now Bazaar documentation is not in the web page
- With a TO DO board to organize the next things to do. For Bazaar and Upp main packages

Opinions?

Best regards
Koldo


Best regards
Iñaki
Re: executing commands and viewing it's result [message #23142 is a reply to message #23137] Fri, 18 September 2009 12:17 Go to previous messageGo to previous message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
Would be nice to have a page where are listed all Bazaar packages, their author and a short description and also the projects which someone is doing and will be in bazaar when they are finished so we remove duplicate efforts as you explained with SysExec/SysConsole/ExecuteCMD etc.
Previous Topic: Splitting a string by a string?
Next Topic: *Map::IsPicked() is missing
Goto Forum:
  


Current Time: Thu Mar 28 11:53:09 CET 2024

Total time taken to generate the page: 0.03371 seconds