Howto call external program and get it's output from another program - c++

Howto do this in c++:
Suppose that program A is a command line tool with some inputs (for example file paths and a number), according to it's inputs, it may get some other parameters during runtime. (if(condithin) cin<<something) I'd like to call A from another program B and want to see complete output of A during it's running. A's inputs must be entered (if necessary). B is a gui tool written with Qt and A must be shown in a plain text area and it's inputs must be shown in same place (like a console client).
I just don't know where to start. Reading something about IPC didn't help. I know it's possible because I see Dolphin's console window and python interpreter in Eric IDE...

use QProcess::execute method to start running A. you can form the argument list from B to pass to A. Use QProcess::readAllStandardOutput () to read the output of the process and display in B.

Since you use Qt, using QProcess is probably the best way to do it.

Related

How to call a C++ program from Clojure so that the programs stays open?

Let's say I want to call a C++ program from Clojure, like stockfish.
If I execute stockfish from the terminal, it stays open and interactive until the command is quit.
However, if I call from Clojure, it just calls it once and closes it.
I have used the programs macro of the me/conch package, like this:
user> (programs stockfish)
user> (stockfish "uci")
"Stockfish 030620 64 by T. Romstad ... \nuciok\n"
And the program stops. How can I get the process to stay open and to keep interacting until I tell it to quit?
This sort of problem has a canonical solution in the Unix expect tool, which has been replicated in, for example, Perl's Expect module. There might also be a Java version of Expect. If so, it might be a more direct solution.
About conch specifically, the README.md at https://github.com/Raynes/conch presents two ways of invoking a program. The first way is easy, but sends only one burst of standard input before closing the program, as you observed. The second way will be harder to work with, but you can send more input whenever you want by writing to the process' standard-input, and recover output whenever you want by reading from its standard-output. It's under the heading https://github.com/Raynes/conch#low-level-usage. It looks like a thin wrapper around the Java process API, which you might as well use directly.

Attach interactive console to embedded python script

I have a Python Script that I run from a C++ GUI Application.
I want to get the output of that Script into a Python Console and have the ability to manipulate them before calling another Python Function from C++.
My Question: Is that possible by just redirection stdin & stdout to Files?
Is there a better way using pure python?
Please note that I donĀ“t want to spawn the console from the C++ Programm but from outside the C++ Programm.
You should be able to adapt the approach in this answer to your needs. The example it links to uses UDP sockets for transferring commands to/from the interactive interpreter, but you could easily change that to pull data from stdin (or wherever) instead.
The key thing to take away from the example is the use of the builtin InteractiveConsole's push() method to determine whether the input is:
Well-formed Python snippet that may be evaluated as-is
A syntactically invalid snippet, or
A snippet that may become valid, but more input is needed

Need to hook a process in C++

i want to do a simple program that will run another program (easy task in C++), hook the process and, when the program is closed, run another program (needed to sync a file modified by the first program).
The second program prompt out a popout asking a simple Yes/No buttons.
There is any way?
I am writing an article about hooking : http://ntvalk.blogspot.nl/2013/11/hooking-explained-detouring-library.html
It describes the probably most common methods for hooking under Windows. (jmp/vtable/etc)
gldraphael already described a method of running another program.
Check this out: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044654269&id=1043284392
It lists the various ways you can run another program.

How to make a gui for command-line program?

Recently, i've got interested in making a front-end for command-line program.
I guess there's two way to do it.
First one is just including source code and calling main proc with arguments
(Of course, there should be some changes in source code).
Second one, which is there's no source code and just program, is just executing program internally then reading the command line with APIs.
Though I well know about the first solution, i don't know what APIs is needed to do the second solution.
I'm talking about the APIs that get a command-line string or something like that.
See this question for information on how to run an external application; basically, you need to call CreateProcess function. I'm not sure what you mean by "reading the command line", I suppose you mean reading the output of an executed program? As for capturing an external application's output, there's already another question asking for that, you will probably find this answer most helpful.
here is a codeProject project that I have used and can handle command line arguments for you (in the setup you describe). If you are not happy with it, you can use the direct WinApi calls using CommandLineToArgvW.
Enjoy!

Windows Console2 coolType

As you can see in u413.com: Text comes on the screen, letter by letter, which looks kind of cool,
I want to do the same thing in Console
I got the source code of version 1.2; because I dont need all the complexity of version 2;
I just need a simple Command Prompt, with text that comes on to the screen letter by letter.
I dont need most of the builtin functions offered by Console, like Transparency, Taskbar Icon, etc.
The source code base is pretty small with only about 5 files.
The main class file seems to be Console.cpp;
Since console is like a GUI application, things dont get written to the STDOUT.
but heres what happens;
A Handle is called;
And that handle apparently writes to the console;
m_hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
Now what I want to be able to do; Is somehow read the handle; see what text it has; And throw in a loop with a ::Sleep(20) method in it; to make sure that text appears letter by letter.
#Alf P. Steinbach I wrote the psuedo code for making the sleep command(in java) I also made use of it in every other java program I wrote, but the disadvantage was that it will work only for my programs and not every program run in the command prompt, but what I dont know is code to make a console, a windows console subsystem program, I wish it was possible with java, so that I could use it on linux too, but now, let me ask you exactly what I had in mind...
A program which simply, took input from the screen, send it to cmd.exe for processing, and sent back the reply, and all I have to do is throw in a sleep command between every character...
All I need is help, in getting this done, I wish you could start me up with this, and possible provide links and refrences to get this done...
This Console.cpp seems like a console emulator, meaning it runs OTHER programs. You don't need the source code, you can run existing console applications.
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are the windows handles for STDIN, STDOUT and STDERR. They basicly does the same with only a few differences in how to use it.
If you want to integrate the console into your code, then you have to understand it, and find its own output function and call that. But it won't be any standard handle or things like that.