JeffS
JeffS

Reputation: 337

Call CreateProcess() and get return value of a string

I have a C++ application that calls an EXE written in C# which grabs some information, and I want to pass it back as a string to the calling C++ application.

So my question is, "is it possible to get a return value of string data from an EXE called with CreateProcess()?

I hope I didn't violate any "question" etiquette here.

Upvotes: 0

Views: 1597

Answers (3)

Mathew Schlabaugh
Mathew Schlabaugh

Reputation: 181

No worries Jeff yes there is a way to get the data from one to the other though the EXE.

What you will want to do is write your output in the C# application to standard output.

Console.WriteLine("Your message here.");

Basically if you run the C# console application and it prints data to the screen all that data can be read in your C++ application.

In your C++ program you will want to read from standard input.

It may look something similar to this:

#include <iostream>
#include <string>

int main() {
    // Your code to run the application in a background process

    for (std::string line; std::getline(std::cin, line);) {
        std::cout << line << std::endl;
    }

    return 0;
}

The only rule you broke was not providing enough detail or code to get more specific about the solution. But if you need more help just provide more details. Happy coding!

Here is a link to more information about the c# code if you need it: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 596497

If the C# app outputs its data to STDOUT or STDERR, the C++ app can capture it by redirecting STDOUT/STDERR when it calls CreateProcess(). See Creating a Child Process with Redirected Input and Output on MSDN for details and examples.

Upvotes: 3

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

I don't see how another process can return a string. Processes don't return anything, they just run.

You can reference (or link) some DLL or EXE and then call a method returning a string. But this will run within your own process and not start another one.

See: Calling C# .NET methods from unmanaged C/C++ code (CodeProject).

Upvotes: 0

Related Questions