Attach interactive console to embedded python script - c++

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

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.

Invoking a console application from C++ program

I have an console application 'app.exe', which i want to invoke from a C++ program and then communicate with it as if it was a command line. Essentially I want to make a C++ wrapper around another console application so that I could pass input to it at will and receive output.
In pseudo-code something like:
std:string input("...some parameters..."), output;
Process app("app.exe");
app.InputOutput(input, output);
std::cout<<output;
This must have been answered already, but I seem to lack proper terminology to look it up.
In case it matters, I am running Eclipse CDT on Windows 10 with GCC 5.3.0
EDIT: I need to be able to repeatedly send some values to 'app.exe' and repeatedly receive response, rather than just invoke it with parameters. This is needed for a small personal project so I do not care about it being platform-specific.
I used this code as a starting point, in an MFC dialog, to display output from a called process. It was rather painless as this is well documented. He tells you why he is doing what. It should be suitable as you are working with the Windows platform. But as Alf points out, cross platform is something else.
You can use the system function to invoke a shell (command line) command.
That command can be to execute a program with the arguments you want.
system returns the process exit code, but for other results there is no direct support. One easy way to access the output, for a program that just does a job and ends, is to redirect the program's output to a file.
Otherwise you'll have to use communication mechanisms such as pipes or Windows mailslots, that are not supported by the C++ standard library, i.e. you're then into platform-specific code.

Wrapping executable's input and output in c++

There is a program in java (Minecraft) and for the server part of it, it opens up in a terminal and prompts the user for commands, as well as give feedback for loading progress and other stuff. How can I make a c++ "wrapper" to automatically send commands to the terminal, and receive the response?
I could automate commands such as say and kick with GUI elements. I am running a mac with OSX Lion.
It presumably reads and writes stdin/stdout. You should look up executing a binary (in this case java.exe) in c++ and how to read and write to it. Alternatively, you could write a wrapper in Java that gives you control without having to parse the text output.
The answer to this question would depend on the operating system as different systems use different approach how to deal with the standard input and output channels. On a UNIX system you'd create a pipe(2) (or two if you want to capture standard output and standard error separately), fork(2) the "server", use close(2) and dup(2) to put the various file descriptors into place, and then execve(2) the actual program. After this you can read/write to various descriptors.

Ahk script and C++ communication

I wish to use the functions of autohotkey within a C++ program.
I am currently running my scripts triggered by the c++ program- I just run them as a .bat file. This works well but the problem is that I cannot return values from the script to the c++ program.
I wish to be able to read the position of the mouse from the script and make decisions based upon this in my C++ program. My scripts do quite complex things- so doing this in autohotkey is the best solution for me- I have knowledge of C, but little of C++.
I have read about the Autohotkey .DLL - I know how to trigger it but not how to read values from it. If anyone could instruct me or even post example code of a .dll being loaded and a value sent to a script and a value returned- I would be eternally grateful!!
I have spent hours on this and to no avail!
to return a value, could this possibly work http://www.autohotkey.net/~tinku99/ahkdll/functions/ahkgetvar.htm
I'm not sure about the dll, but you could just write your own application in Autohotkey and package it along with your C++.
The communication takes place over a hidden window with an edit control and a button. You use one application to set text in the edit box, and then to click the submit button. The other application owns the window can process whatever is put into the edit control - as if you were passing a variable. Basically, that's it.
Check out this thread where I've explained it in more detail: How to send a command to a running application via commandline
Now, that's not quite what you wanted, but the effect is the same, and you already know all the api.

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

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.