利用CreateProcess和[管道]隐式调用一个控制台程序

在程序中利用CreateProcess创建子进程,在子进程中调用另一个控制台程序,并利用“管道”技术重定向子进程中控制台程序与主进程间的输入、输出

CString strAccount;
GetDlgItemText(IDC_EDIT_IN,strAccount);
if (strAccount.IsEmpty())
{
	MessageBox("请先输入要授权的账号!","提示",MB_OK|MB_ICONWARNING);
}
else
{
	CString strIn;
	CString strNum;
	for (int i=0;i<strAccount.GetLength();i++)
	{
		if (i%2 == 0)
		{
			strNum=strAccount.Mid(i,1);
			strIn+=strNum;
		}
	}
		
	PROCESS_INFORMATION pi={0};
	HANDLE hPipeOutputRead=NULL;
	HANDLE hPipeOutputWrite=NULL;
	HANDLE hPipeInputRead=NULL;
	HANDLE hPipeInputWrite=NULL;
	BOOL bTest;
	DWORD dwNumberOfBytesRead=0;
	DWORD dwNumberOfBytesWrite=0;

	char* sIn;
	sIn=strIn.GetBuffer(strIn.GetLength());
	strIn.ReleaseBuffer();
	CHAR szMsg[100];
	memset(szMsg,0,sizeof(szMsg));
	CHAR szBuffer[256];
	memset(szBuffer,0,sizeof(szBuffer));

	//创建管道 
	SECURITY_ATTRIBUTES sa={0};
	sa.nLength = sizeof(sa);
	sa.bInheritHandle = TRUE;
	sa.lpSecurityDescriptor = NULL;

	//为标准输出重定向创建管道
	CreatePipe(&hPipeOutputRead,  // read handle
		&hPipeOutputWrite, // write handle
		&sa,      // security attributes
		0      // number of bytes reserved for pipe - 0 default
		);

	//为标准输入重定向创建管道
	CreatePipe(&hPipeInputRead,  // read handle
		&hPipeInputWrite, // write handle
		&sa,      // security attributes
		0      // number of bytes reserved for pipe - 0 default
		);

	//创建进程 
	//使子进程使用hPipeOutputWrite作为标准输出使用hPipeInputRead作为标准输入,并使子进程在后台运行
	STARTUPINFO si = {0};
	si.cb = sizeof(si);
	si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
	si.wShowWindow = SW_HIDE;
	si.hStdInput   = hPipeInputRead;
	si.hStdOutput  = hPipeOutputWrite;
	si.hStdError   = hPipeOutputWrite;

	CreateProcess ("./tool/111.exe", NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);

	//创建子进程后句柄已被继承,为了安全应该关闭它们
	CloseHandle(hPipeOutputWrite);
	CloseHandle(hPipeInputRead);
		
	sprintf(szMsg,"%s
",sIn);

	WriteFile(
		hPipeInputWrite,      // handle of the write end of our pipe
		&szMsg,               // address of buffer that send data
		strlen(szMsg),        // number of bytes to write
		&dwNumberOfBytesWrite,// address of number of bytes read
		NULL                  // non-overlapped.
		);

	CString strData;

	while(1)
	{
		bTest=ReadFile(
			hPipeOutputRead,      // handle of the read end of our pipe
			&szBuffer,            // address of buffer that receives data
			sizeof(szBuffer),     // number of bytes to read
			&dwNumberOfBytesRead, // address of number of bytes read
			NULL                  // non-overlapped.
			);

		if (bTest){
			szBuffer[dwNumberOfBytesRead] = 0;  // null terminate
			CString strMsg;
			strMsg.Format("%s",szBuffer);
			strData+=strMsg;
		}
		else
		{
			break;
		}
	}

	CString strOut;
	AfxExtractSubString(strOut,strData,2,'
');
	SetDlgItemText(IDC_EDIT_OUT,strOut);

	WaitForSingleObject (pi.hProcess, INFINITE);

	//关闭剩余的句柄
	CloseHandle(pi.hProcess);
	CloseHandle(hPipeOutputRead);
	CloseHandle(hPipeInputWrite);

	MessageBox("Step1已完成,请执行Step2!","提示",MB_OK);
}
原文地址:https://www.cnblogs.com/zhouwanqiu/p/8483148.html