Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing output from C++ to PHP

I am creating a PHP file to pass values to a c++ .exe which will then calculate an output and return that output. However, I cannot seem to get the output from the .exe back into the PHP file.

PHP Code:

$path = 'C:enter code here\Users\sumit.exe';
$handle = popen($path,'w');
$write = fwrite($handle,"37");
pclose($handle);

C++ Code:

#include "stdafx.h"
#include <iostream>
using namespace std;

// Declaation of Input Variables:
int main()
{
int num;
cin>> num;

std::cout<<num+5;
return 0;
}
like image 281
Wayne Avatar asked Jan 22 '26 15:01

Wayne


2 Answers

I'd advise neither system nor popen but proc_open command: php.net

Call it like

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr, also a pipe the child will write to
);
proc_open('C:enter code here\Users\sumit.exe', $descriptorspec, $pipes);

After that you'll have $pipes filled with handles to send data to program ([0]) and recieve data from program ([1]). Also you will have [2] which you can use to get stderr from the program (or just close if you don't use stderr).

Don't forget to close process handle with proc_close() and pipe handles with fclose(). Note that your program will not know the output is complete before you close $pipes[0] handle or write some whitespace character. I advise closing the pipe.

Using command line arguments in system() or popen() is valid, though if you intend to send large amounts of data and/or raw data, you will have trouble with command line length limits and with escaping special chars.

like image 92
Aneri Avatar answered Jan 25 '26 09:01

Aneri


In your C++ code I am not seeing anything for passing variables in you need

 int main(int argc, char* argv[])

instead of

 int main()

Remember argc is the count of variables and it includes the path to the file, so your arguments begin at 1 with each argv being a c-string of that argument. If you need a decimal atof is your friend or atoi for an integer.

Then you are using popen. The PHP documentation says that it can be only used for reading or writting. It is not bi-directional. You want to use proc_open to have bi-directional support.

Anyways, This is how I would write your C++ code:

#include "stdafx.h"
#include <iostream>

// Declaation of Input Variables:
int main(int arc, char* argv[])
{
   int num;
   num = atoi(argv[1]);

   std::cout<<num+5;
   return 0;
 }

Note: I removed using namespace std because I noticed you were still trying to use the namespace in the main function (i.e. std::cout) and it better to keep it out of a global namespace.

like image 40
Travis Pessetto Avatar answered Jan 25 '26 09:01

Travis Pessetto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!