🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #6938

Re: Console stays open after redirected CreateProcess.

#6938
From: Bob Gonder
Date: Fri, 25 Mar 2005 20:21
81 lines
2925 bytes
To be a bit more precise:

main()
	{RedirectStdout to Pipe}
	{CreateProcess /Wait}
	{Read/Process Pipe}
	return
Correctly pipes the screen and
closes the main console.

WinMain()
	AllocConsole
	{RedirectStdout to Pipe}
	{CreateProcess /Wait}
	{Read/Process Pipe}
	FreeConsole
	return
Correctly pipes the screen and
leaves the Allocated console open!
(Without {CreateProcess /Wait}, the console closes)
(Am I doing something wrong?)

WinMain()
	{RedirectStdout to Pipe}
	{CreateProcess /Wait}
	{Read/Process Pipe}
	return
Does Not pipe the screen (pipe is empty),
closes with no left-over console.
(I also don't understand this?)


{RedirectStdout to Pipe}
	STARTUPINFO si={0};
	PROCESS_INFORMATION pi={0};
	HANDLE	hStdinRd, hStdinWr, hStdoutRd, hStdoutWr; 
	SECURITY_ATTRIBUTES	saAttr;

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

	CreatePipe(&hStdoutRd, &hStdoutWr, &saAttr, 0);
	CreatePipe(&hStdinRd, &hStdinWr, &saAttr, 0);

	si.hStdInput = hStdinRd;
	si.hStdOutput = hStdoutWr;
	si.hStdError = hStdoutWr;

{CreateProcess /Wait}
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESTDHANDLES;

	if( CreateProcess( 0, command, 0, 0, TRUE, 0, 0, 0, &si, &pi))
	{
		WaitForSingleObject( pi.hProcess, INFINITE );
		CloseHandle( pi.hProcess );
		CloseHandle( pi.hThread );
		Trace("\n Program ran");
	}else{
		Trace("\n Run Aborted");
	};
	CloseHandle( hStdoutWr );
	CloseHandle( hStdinRd );
	CloseHandle( hStdinWr );

{Read/Process Pipe}
	char	buf[128];
	DWORD	BytesRead, written;
	HANDLE Out = CreateFile( "Redirect.txt", GENERIC_WRITE,
		 0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
	do{
	     ReadFile( hStdoutRd, buf, sizeof(buf)-1,&BytesRead,NULL);
	     if( BytesRead )
	     {
		WriteFile(Out, buf, ReadBytes, &written, NULL);
	     };
	}while( BytesRead );
	CloseHandle( Out );
	CloseHandle( hStdoutRd );
Technical Details
Message-ID: <mbq841917m5njqbkgdub6ds8gnuh76mkfs@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <vi36411dhum1dho5li05dbbsj044qur13j@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,3a2a64440564cd69
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Console stays open after redirected CreateProcess.
Date: Fri, 25 Mar 2005 12:21:53 -0800
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <mbq841917m5njqbkgdub6ds8gnuh76mkfs@4ax.com>
References: <vi36411dhum1dho5li05dbbsj044qur13j@4ax.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 67.77.36.21
X-Trace: newsgroups.borland.com 1111782111 67.77.36.21 (25 Mar 2005 12:21:51 -0800)
Lines: 84
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail