Public feed #887341

Unnamed

public text v1 · immutable
Raw Edit
·published 2008-02-01 07:16 UTC
rendered paste body
// cons.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cons.h"
#include "vector"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;
UINT func(LPVOID);

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{

				SECURITY_ATTRIBUTES sa;
				sa.nLength = sizeof(SECURITY_ATTRIBUTES);
				sa.lpSecurityDescriptor = NULL;
				sa.bInheritHandle = TRUE;

				HANDLE hStdOutRead,hStdOutWrite;
				::CreatePipe( &hStdOutRead, &hStdOutWrite, &sa, 0 ) ;
		

		PROCESS_INFORMATION pi;
		STARTUPINFO si;

		// Set up the start up info struct.
		::ZeroMemory(&si, sizeof(STARTUPINFO));
		si.cb = sizeof(STARTUPINFO);
		si.hStdOutput = hStdOutWrite;
		si.hStdInput = NULL;
		si.hStdError = hStdOutWrite;
		si.wShowWindow = SW_HIDE;
		si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; 

		CString m_csCmdLine("beep");
		//AfxBeginThread(func,&hStdOutWrite);
		CreateProcess(
			NULL,
			(LPTSTR)(LPCTSTR)m_csCmdLine,
			NULL, NULL,
			TRUE,
			CREATE_NEW_CONSOLE,
			NULL, NULL,
			&si,
			&pi);
		TRACE("After CreatePipe()");
		DWORD dwAvail = 10;
		DWORD readed=10L;
		char buffer[15];
		for (int i=0;i<14;++i)	buffer[i]='_';	buffer[14]='\0';
		ReadFile(hStdOutWrite,buffer,dwAvail,&readed,NULL);
			cout<<readed<<endl;

		cout<<buffer;
      

	}

	return nRetCode;
}
UINT func(LPVOID pParam)
{
	HANDLE* hStdOutRead=(HANDLE*)pParam;
	char buffer[100];
	for (;;)
	{
		DWORD dwAvail = 0;
		DWORD readed=0L;
		if (!::PeekNamedPipe( hStdOutRead, NULL, 0, NULL,
			&dwAvail, NULL))            // error
			break;

		ReadFile(hStdOutRead,buffer,dwAvail,&readed,NULL);
		buffer[10]='\n';
		cout<<endl<<buffer[0];
	}
	return 0;
}