1

I am in the process of coding a board game with C++. After compiling, there will be an executable file that runs the game. I wish to call this executable from a JavaScript program I am creating to handle the input and output of the executable. Is it possible to run the executable from JavaScript?

I know there are other answers such as this one: Running .exe from Javascript, but I am trying to handle input to the executable, and thus will need to be able to interact with the user from JavaScript and then feed that input into the executable. Thus, I need to be able to "pause" the running of the executable while I wait for input to give it from the user. Is this possible?

EDIT:: I will be running this from a JavaScript server

Community
  • 1
  • 1
  • Depends on the environment. If you're planning on running it in a browser then the answer is no, although you may be able to compile your original program using [Emscripten](https://github.com/kripken/emscripten) to make it work. If you plan to run this on the server-side using Node.js then it's possible. – Mike Cluck Aug 18 '16 at 15:18
  • What environment/os/host are you running the JavaScript inside? – Alex K. Aug 18 '16 at 15:18
  • I will be running this from a JavaScript server using nodeJS @AlexK. – Elias Marcopoulos Aug 18 '16 at 15:21
  • Seems a DLL rather than an EXE would be a better choice. http://stackoverflow.com/questions/30364047/nodejshow-to-call-c-dll-function-through-nodejs – Alex K. Aug 18 '16 at 15:27

1 Answers1

0

You won't be able to do this from a browser but if you use nodejs you will be able to achieve what you want with this lines of code

var exec = require('child_process').exec;
var child = exec('path/to/exe', function(error, stdout, stderr, callback) {
        //do something here
});
miqe
  • 3,209
  • 2
  • 28
  • 36
  • That won't work. OP wants to be able to feed input while the program is running. `exec` will run the program to completion then give you the output. – Mike Cluck Aug 18 '16 at 15:23
  • I thought @Elias Marcopoulos wanted that, give an `input` to the `exe` and get an `output` from the exe right? – miqe Aug 18 '16 at 15:28
  • "need to be able to interact with the user from JavaScript and then feed that input into the executable. Thus, I need to be able to "pause" the running of the executable while I wait for input to give it from the user" – Mike Cluck Aug 18 '16 at 15:28
  • It is actually possible to run executables in a browser, since there are several [x86 emulators in JavaScript](https://softwarerecs.stackexchange.com/questions/25162/x86-emulator-in-javascript-to-run-linux-windows-or-dos) that can run native binaries. – Anderson Green Feb 02 '22 at 00:41