0

I have a executable program myApp (or myApp.exe in Windows). I want to call this executable file in a C/C++ program, using system("myApp") or QProcess:execute("myApp") in Qt.

I want to get multiple return values from myApp, so when calling this executable, I passed pointers as arguments, like this (in Qt)

float *pStates = new float[nState];
float *pControl = new float[nControl];
pControl[0] = 0.1;
pControl[1] = 0.2;

QString proStr;
proStr = "myApp ";
for (int i = 0; i < nState; i++)
{
    proStr += (QString::number((uint64_t)pStates + i * sizeof(float)) + " ");
}
for (int i = 0; i < nControl; i++)
{
    proStr += (QString::number(pControl[i]) + " ");
}
QProcess::execute(proStr);

The proStr string might look like

myApp 140483171852384 140483171852388 140483171852392 140483171852396 140483171852400 0.1 0.2 

Here 140483xxx numbers are pointer or physical address of pStates.

And the code of myApp looks like

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    if (argc != 8)
    {
        return 1;
    }

    float *x = atoi(argv[1]);
    float *y = atoi(argv[2]);
    float *z = atoi(argv[3]);
    float *psi = atoi(argv[4]);
    float *v = atoi(argv[5]);
    float u = atof(argv[6]);
    float a = atof(argv[7]);

    *x = 1;
    *y = 2;
    *z = 3;
    *psi = 1.23;
    *v = 3.21;

    return 0;
}

But this seems not working.

So, how can I get multiple return values from an external executable?

Alexander Zhang
  • 207
  • 2
  • 9
  • You could have the other program write its output on stdout, and then you invoke it with `popen` – M.M Apr 05 '17 at 03:16
  • Each process has its own virtual address​ space. It is not meaningful to pass an address to a different process. – rici Apr 05 '17 at 04:46

1 Answers1

1

You can let the other process dump results as text to the standard output or a file and then parse that output to obtain results.

You can also use more complicated inter-process-communication schemes ranging from memory mapped files (i.e. Sharing memory between two processes (C, Windows)) to TCP/IP or even full HTTP communication.

If you really need the external process to update your in-memory values the other process must have permissions to write to memory of your process and use corresponding methods to update data in another process. You would also need to be very careful to synchronize access to such data as you'd not know when the external process stores the data.

On Windows you'd use something like WriteProcessMemory C++ and pass process handle of you original process as arguments to process you start. Make sure to read on permissions needed to perform such operation.

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • The file method is nice and generic. If "myApp" is in OP's control (presumably so), one of the parameters to it on the command-line could be the name of the file to which to write. – Phil Brubaker Apr 05 '17 at 04:40