3

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;
}

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

you are writing into exe file, you should pass your argument like

system("C:enter code here\Users\sumit.exe 37");

1 Comment

You need to change the definition of main() in the sample, too. int main(int argc, char **argv) or int main(int argc, char *argv[])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.