954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

spawning a child process

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.

zzmgd6
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

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().

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 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.

zzmgd6
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

zzmgd6
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

OpenClipboard() scroll down a bit and you will see a link to an example

File Mapping

popen() google links

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

zzmgd6
Newbie Poster
22 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You