你好,游客 登录
背景:
阅读新闻

进程通信之三 父进程传参数与子进程返回值 - MoreWindows

[日期:2013-03-21] 来源:  作者: [字体: ]

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165733

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8646902

欢迎关注微博:http://weibo.com/MoreWindows

 

前面已经写了使用WM_COPYDATA消息来完成进程之间的通信

1.《进程通信之一使用WM_COPYDATA C++C#实现

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

然后用了三篇文章来讲解如何使用管道技术来完成进程通信功能。

1.《进程通信之二管道技术第一篇输入输出的重定向

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

2.《进程通信之二管道技术第二篇匿名管道

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

3.《进程通信之二管道技术第三篇命名管道

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

 

    本篇介绍一个简单而又实用的进程通信方式——父进程向子进程传入参数并获取子进程返回值。这个非常简单:

1.父进程向子进程传入参数可以由CreateProcess()函数来完成,注意子进程是通过GetCommandLine()来获取这个参数而且不是char *argv[]

2.父进程要获取子进程的返回值可以在等待子进程结束后通过GetExitCodeProcess并传入子进程句柄来获取子进程中mainWinMain函数的返回值。

下面就结出实例代码,首先来看子进程的程序代码:

//进程通信之三 父进程传参数与子进程返回值
//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[])
{
	// 通过GetCommandLine()获取父进程通过CreateProcess传给子进程的参数
	// 注意这与argv[]不同如strlen(argv[1])会出错
	srand((unsigned int)time(NULL));
	return strlen(GetCommandLine()) + rand() % 10;
}

然后是父进程的程序代码,代码中的AdjustProcessCurrentDirectory();函数可以参考《Windows VC++ 调整进程当前目录为程序可执行文件所在目录》(http://blog.csdn.net/morewindows/article/details/8683519):

//进程通信之三 父进程传参数与子进程返回值
//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>
// 启动子进程并传入参数,等待子进程结束后获取其返回值。
BOOL GetChildProcessExitCode(const char *pstrChildProcessExeFileName, 
							 char *pstrConmandLine, 
							 DWORD *pdwExitCode, BOOL fHide = TRUE)
{
	//子进程启动信息设置
    STARTUPINFO si;  
    si.cb = sizeof(STARTUPINFO);  
    GetStartupInfo(&si);    
    si.wShowWindow = fHide ? SW_HIDE : SW_SHOW;
    si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;  

	// 运行子进程并等待其结束
	PROCESS_INFORMATION pi;   
	CreateProcess(pstrChildProcessExeFileName, pstrConmandLine, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi);  
	WaitForSingleObject(pi.hProcess, INFINITE);  
	
	// 获取子进程返回值
	BOOL flag = GetExitCodeProcess(pi.hProcess, pdwExitCode);

	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);

	return flag;
}

int main()
{
	printf("    进程通信之三 父进程传参数与子进程返回值\n");        
	printf(" - http://blog.csdn.net/morewindows/article/details/8683830 -\n");
	printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");  

	AdjustProcessCurrentDirectory();
	
	const char *pstrChildProcessExeFileName = "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++)
    {
		// 运行子进程并获取返回值
		DWORD dwExitCode;
		if (GetChildProcessExitCode(pstrChildProcessExeFileName, szCommandLine, &dwExitCode, TRUE))
			printf("子进程返回值为 %d\n", dwExitCode - nCommandLineLen);
		else
			printf("GetExitCodeProcess()失败  %d\n", GetLastError());

		Sleep(1000);
	}
	getch();
	return 0;
}

程序运行结果如下:

 

 本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5165733

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8646902

欢迎关注微博:http://weibo.com/MoreWindows


 






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻