I understand how to use CreateProcess, ShellExecute(Ex), _spawn, exec methods to start a child process from a parent process. Question: Is there a way I can return values back to the parent process from the child process? For example, the child process sets an integer value and I want the parent to be aware of this value when the child process completes execution.

Sample code is always appreciated.

Thank you in advance.

Regards.

Recommended Answers

All 6 Replies

Depends on whether the child process is a console application or an MS-Windows program. Console: just return the int value from main(). MS-Windows: PostQuitMessage()

On the parent process side: After calling CreateProcess(), call WaitForSingleObject() then when that returns call GetExitCodeProcess() to get the exit status of the spawned program. Note: this does not work well with Mobile 5 wireless MFC programs because the MFC code will return 0 regardless of the parameter in PostQuitMessage().

Thank you for the fast reply.

This is for console apps, not MFC or .net, I am dealing with legacy code and libraries that I can not change. I write in C++ and wrap any WIN32/third party libraries funcs into my class's member functions (methods).

The int return value was kind a bad example, because you are exactly right. Just return the int from the child process main.

I need the int value, but I also have an array of strings. I try to alway use the C++ type for strings, std::string, std:vector<std::string> for an array of strings. I figured that I can write out a file and then have the parent read the file when the child process is finsh execution, but I really don't want to do that unless I absolutely have to. Is there a way to pass a pointer from the parent to the child and have the child populate it?

Thanks again for the reply.

Regards.

>>Is there a way to pass a pointer from the parent to the child and have the child populate it?
No because the two probrams are in different address spaces. You could copy the strings to the clipboard or put them into a mem file. Or you could open a pipe to the two programs and parent could read the strings directly through the pipe.

Thanks again for the rapid reply!

Could you give the primary functions for each or the following;

Copy to clipboard? I may be able to google this, I know I have seen this once upon a time.

use a mem file???

using pipe? I read a little on this one, but did not get enough sample code to pull it off.
It appears the parent opens one end of thepipe then the child opens the other end, and when completed the parent is to close both ends of the pipe. Correct?

Regards.

Thank you again for the rapid reply!

and the sample functions, I will give each a try.
I have read a little on popen() on a few sites. Did not fully understand it, but I will research it again more in depth.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.