Two way communication between a C++ program and an unidentified program -


i create c++ program can launch program , communicate standard user.

here basic example : say, have program in kind of language (say python exampe, sould kind of program). program launch via console specific command (like "./myprogram.exe" or "python ./myprogram.py" or "java ./myprogram.jar"). wait input of user , give sum of precedent inputs.

example :

./myprogram.exe please enter numer. user > 4 4 user > 2 6 user > 9 15 

so program has memory.

now, want automatize user inputs , output reading within c++ program b. program b automaticaly send input, wait other program give output before sending input , on... without closing , starting again program because program has memory of inputs.

note : program (which tested) non changeable. want benchmark without modifying it.

do know how can perform such communication ?

thank you

thank advices, read pipe(), fork() , on worked in unix environment, , work on windows (sorry forgetting that...).

so after searching bit, finding documentations , creating codes, managed wanted.

warning : give solution people face same problem me , want thing work. may not safe, may not best solution, use @ own risk. , remember specific windows.

#include "iostream"  #include <windows.h>  #include <tchar.h> #include <stdio.h>  #include <strsafe.h>  #define bufsize 4096   handle g_hchildstd_in_rd = null; handle g_hchildstd_in_wr = null; handle g_hchildstd_out_rd = null; handle g_hchildstd_out_wr = null;  handle g_hinputfile = null;  using namespace std;  void createchildprocess(string cmdline); void writetopipe(string input); string readfrompipe(); void errorexit(ptstr);   int main() {     security_attributes saattr;      cout << "starting pipes..." << endl;      saattr.nlength = sizeof(security_attributes);     saattr.binherithandle = true;     saattr.lpsecuritydescriptor = null;      if (!createpipe(&g_hchildstd_out_rd, &g_hchildstd_out_wr, &saattr, 0))         cout << "error : stdoutrd createpipe" << endl;      if (!sethandleinformation(g_hchildstd_out_rd, handle_flag_inherit, 0))         cout << "error : stdout sethandleinformation" << endl;      if (!createpipe(&g_hchildstd_in_rd, &g_hchildstd_in_wr, &saattr, 0))         cout << "error : stdin createpipe" << endl;      if (!sethandleinformation(g_hchildstd_in_wr, handle_flag_inherit, 0))         cout << "error : stdin sethandleinformation" << endl;      cout << "creating child process..." << endl;      createchildprocess("python c:/users/me/desktop/benchmark/test.py");      writetopipe("5");      readfrompipe();     readfrompipe();      getchar();      return 0; }  void createchildprocess(string cmdline) {   tchar *szcmdline = new tchar[cmdline.size() + 1];     szcmdline[cmdline.size()] = 0;     std::copy(cmdline.begin(), cmdline.end(), szcmdline);      process_information piprocinfo;     startupinfo sistartinfo;      zeromemory(&piprocinfo, sizeof(process_information));      zeromemory(&sistartinfo, sizeof(startupinfo));     sistartinfo.cb = sizeof(startupinfo);     sistartinfo.hstderror = g_hchildstd_out_wr;     sistartinfo.hstdoutput = g_hchildstd_out_wr;     sistartinfo.hstdinput = g_hchildstd_in_rd;     sistartinfo.dwflags |= startf_usestdhandles;      if (!createprocess(null, szcmdline, null, null, true, 0, null, null, &sistartinfo, &piprocinfo))     {         cout << "error : createprocess" << endl;     }     else     {         closehandle(piprocinfo.hprocess);         closehandle(piprocinfo.hthread);     } }  void writetopipe(string input) {     dword dwwritten;     char chbuf[bufsize];      cout << "> " << input.c_str() << endl;      input += "\n";      if (!writefile(g_hchildstd_in_wr, input.c_str(), strlen(input.c_str()), &dwwritten, null))     {         cout << "error : writefile" << endl;     }        }  string readfrompipe() {        dword dwread, dwwritten;     char chbuf[bufsize];     bool bsuccess = false;     handle hparentstdout = getstdhandle(std_output_handle);      string output = "";     bool flag = false;      (;;)     {         bsuccess = readfile(g_hchildstd_out_rd, chbuf, bufsize, &dwread, null);         if (!bsuccess || dwread == 0) break;          (int = 0; < dwread; i++)         {             if (chbuf[i] == '\n')             {                 flag = true;                 break;             }             output += chbuf[i];         }         if (flag)         {             break;         }     }      cout << "< " << output.c_str() << endl;      return output; } 

and program test.py :

import sys  sys.stdout.write('loading module\n')  test = int(input())  sys.stdout.write(str(test+1)) sys.stdout.write('\n') 

what send string "5\n" test.py , read output (which "6"). work command java test.jar or python test.py or test.exe.

void createchildprocess(string cmdline) allow create child process specific command line.

void writetopipe(string input) allow send child process (a '\n' automaticaly added)

string readfrompipe() synchronous function output line of ouput of child process (the last char '\n' automaticaly deleted)

this solution partially based on documented code : https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -