user1873947
user1873947

Reputation: 1821

How to run another app in C++ and communicate with it, cross platform

I want to run another program from my C++ code. system() returns int, as every program can only return int to the os. However, the other program I want to call will generate a string that I need in my base app. How can I send it to the parent process?

The two apps will be in the same folder, so I think that the child app can save the string to "temp.txt" and then the main app may read and delete it (it's not performance critical process, I will call another process just to call open file dialog in my main opengl app). However this is a bit ugly solution, are there better cross platform solutions?

Thanks

Upvotes: 2

Views: 1726

Answers (3)

rekire
rekire

Reputation: 47985

You could use popen(), this opens a process where you can write and read data. AFIK this is also cross plattform

// crt_popen.c
/* This program uses _popen and _pclose to receive a 
 * stream of text from a system process.
 */
#include <stdio.h>
#include <stdlib.h>

int main(void) {

   char   psBuffer[128];
   FILE   *pPipe;

        /* Run DIR so that it writes its output to a pipe. Open this
         * pipe with read text attribute so that we can read it 
         * like a text file. 
         */

   if((pPipe = _popen("dir *.c /on /p", "rt")) == NULL)
      exit(1);

        /* Read pipe until end of file. */

   while(!feof(pPipe)) {
      if(fgets(psBuffer, 128, pPipe) != NULL)
         printf(psBuffer);
   }

        /* Close pipe and print return value of pPipe. */

   printf("\nProcess returned %d\n", _pclose(pPipe));

   return 0;
}

Upvotes: 5

Gene Bushuyev
Gene Bushuyev

Reputation: 5538

Though there is no standard way of achieving interprocess communication, there is a relatively pain free library, ported to many OS/compilers: Boost.Interprocess. It covers most necessities:

  • Shared memory.
  • Memory-mapped files.
  • Semaphores, mutexes, condition variables and upgradable mutex types to place them in shared memory and memory mapped files.
  • Named versions of those synchronization objects, similar to UNIX/Windows sem_open/CreateSemaphore API.
  • File locking.
  • Relative pointers.
  • Message queues.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490713

Although it's not part of the C++ standard, nearly all reasonably current systems provide a popen (or _popen, in Microsoft's case) that will let you spawn a child process and read from its standard output as a C-style FILE * in the parent. At least if memory serves, popen is included in POSIX, so you can expect it to be present in essentially all Unix-like systems (and, as implied above, it's also available on Windows, at least with most compilers).

In other words, about the only place you'd likely encounter that it's not available would be something like a small embedded system where it might well be pretty meaningless (e.g., no file system to find the other executable in, and quite possibly no ability to create new processes either).

Upvotes: 4

Related Questions