Process Communication 3 parent process passing parameters and sub-process return values

Source: Internet
Author: User

This article supporting procedures: http://download.csdn.net/detail/morewindows/5165733

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8646902

Welcome to Weibo: http://weibo.com/MoreWindows

 

Previously, wm_copydata is used to complete communication between processes.

1. Implementation of wm_copydata C ++ and C # In Process Communication

Http://blog.csdn.net/morewindows/article/details/6804157

Then I used three articles to explain how to use pipeline technology to complete the process communication function.

1. Process Communication II pipeline technology article 1 redirection of Input and Output

Http://blog.csdn.net/morewindows/article/details/7390350

2. Process Communication Second Pipeline Technology Second anonymous Pipeline

Http://blog.csdn.net/morewindows/article/details/7390441

3. "Process Communication Second Pipeline Technology Article 3 named Pipeline"

Http://blog.csdn.net/morewindows/article/details/8260087

 

This article introduces a simple and practical way for processes to communicate. A parent process sends parameters to a child process and obtains the returned values of the child process. This is very simple:

1. The parameter passed by the parent process to the child process can be completed by the CreateProcess () function. Note that the child process obtains this parameter through getcommandline () and is not a char * argv [].

2. To obtain the return value of a sub-process, the parent process can get the return value of the main or winmain function in the sub-process by getexitcodeprocess and passing in the sub-process handle after the sub-process ends.

The following is the sample code. First, let's look at the program code of the sub-process:

// Process Communication 3 parent process parameters and sub-process return values // http://blog.csdn.net/morewindows/article/details/8683830//By morewindows (http://blog.csdn.net/MoreWindows) # include <windows. h> # include <stdio. h> # include <stdlib. h> # include <time. h> # include <string. h> int main (INT argc, char * argv []) {// use getcommandline () obtain the parameters that the parent process passes to the child process through CreateProcess. // note that this is different from argv []. For example, strlen (argv [1]) may encounter an error srand (unsigned INT) time (null); Return strlen (getcommandline () + rand () % 10 ;}

Then there is the parent process of the program code, the code in the adjustprocesscurrentdirectory (); function can refer to the Windows VC ++ adjust the current directory of the process as the directory where the executable files of the Program (http://blog.csdn.net/morewindows/article/details/8683519 ):

// Process Communication 3 parent process parameters and sub-process return values // http://blog.csdn.net/morewindows/article/details/8683830//By morewindows (http://blog.csdn.net/MoreWindows) # include <windows. h> # include <stdio. h> # include <string. h> # include <conio. h> // start the sub-process and input parameters. Wait until the sub-process ends and obtain the returned value. Bool getchildprocessexitcode (const char * pstrchildprocessexefilename, char * pstrconmandline, DWORD * pdwexitcode, bool fhide = true) {// startinfo Si; SI. CB = sizeof (startupinfo); getstartupinfo (& Si); SI. wshowwindow = fhide? Sw_hide: sw_show; SI. dwflags = startf_useshowwindow; // run the sub-process and wait for it to end process_information PI; CreateProcess (pstrchildprocessexefilename, pstrconmandline, null, null, true, null, & Si, & PI); waitforsingleobject (Pi. hprocess, infinite); // obtain the return value of the sub-process bool flag = getexitcodeprocess (Pi. hprocess, pdwexitcode); closehandle (Pi. hprocess); closehandle (Pi. hthread); Return flag;} int main () {printf ("Process Communication 3 parent process parameters and sub-process return value \ n "); printf ("-http://blog.csdn.net/morewindows/article/details/8683830-\ n"); printf ("-- by morewindows (http://blog.csdn.net/MoreWindows) -- \ n"); adjustprocesscurrentdirectory (); const char * character = "childprocess.exe"; const int run_childprocess_number = 5; char szcommandline [30] = "morewindows"; int ncommandlinelen = strlen (szcommandline); For (INT I = 0; I <run_childprocess_number; I ++) {// run the sub-process and obtain the returned DWORD dwexitcode; If (getchildprocessexitcode (pstrchildprocessexefilename, szcommandline, & dwexitcode, true )) printf ("subprocess return value: % d \ n", dwexitcode-ncommandlinelen); elseprintf ("getexitcodeprocess () failed % d \ n", getlasterror ()); sleep (1000);} getch (); Return 0 ;}

The program running result is as follows:

 

This article supporting procedures: http://download.csdn.net/detail/morewindows/5165733

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8646902

Welcome to Weibo: http://weibo.com/MoreWindows

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.