CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    [RESOLVED] Desperately Stuck...CreateProcess & Pipes

    For some reason, the following code isn't doing what I expected in two areas:

    First, the pipe is only reading the first line of the output.
    Second, the text control I have isn't displaying any text at all, even though I can see the first line read ok.

    Any help would be appreciated in getting to read all the text.

    Code:
    	CButton *btnOk = reinterpret_cast<CButton *>(GetDlgItem(IDOK));
    	CEdit *txtPing = reinterpret_cast<CEdit *>(GetDlgItem(IDC_Ping2));
    	CString strPing;
    
    	btnOk->EnableWindow(FALSE);
    
    	BOOL PipeResult( const CString& CommandLine, CString& strOutFile );
    	STARTUPINFO si;
    	memset( &si, 0, sizeof( si ) );
    	si.cb = sizeof( si );
    	si.dwFlags = STARTF_USESHOWWINDOW;
    	si.wShowWindow = SW_HIDE;
    	SECURITY_ATTRIBUTES saAttr;
    	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    	saAttr.bInheritHandle = TRUE;
    	saAttr.lpSecurityDescriptor = NULL;
    	HANDLE hReadPipe, hWritePipe;
    	BOOL cp = CreatePipe( &hReadPipe, &hWritePipe, &saAttr, 0 );
    	if ( !cp)
    	{
    		AfxMessageBox(_T(GetLastError()));
    	}
    	else
    	{
    		si.dwFlags |= STARTF_USESTDHANDLES;
    		si.hStdOutput = hWritePipe;
    		PROCESS_INFORMATION pi;
    		cp = CreateProcess( NULL, (LPTSTR)(LPCTSTR)"ping google.com", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi );
    		if (!cp)
    		{
    			AfxMessageBox(_T(GetLastError()));
    		}
    		else
    		{
    			// Close write pipe
    			CloseHandle( hWritePipe );
    			DWORD NumberOfBytesRead;
    			TCHAR Buffer[ 500 ];
    			while(::ReadFile( hReadPipe, Buffer, sizeof(Buffer)/sizeof(Buffer[ 0 ])-1,&NumberOfBytesRead, NULL ) )
    				{
    					if (NumberOfBytesRead)
    					{
    						Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
    						strPing += Buffer;
    					}
    					else
    					{
    						break;
    					};
    
    					CloseHandle( hReadPipe );
    					WaitForSingleObject( pi.hProcess, INFINITE );
    					cp = TRUE;
    			}
    		}
    	}
    
    	btnOk->EnableWindow(TRUE);
    	txtPing->SetWindowTextA(strPing);
    Last edited by bderagon; February 15th, 2008 at 05:02 PM.

  2. #2
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    Re: Desperately Stuck...CreateProcess & Pipes

    Updated code with slight changes and still doesn't work as expected.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Desperately Stuck...CreateProcess & Pipes

    Well, one thig I noticed, you only set the 'hStdOutput' handle in the startup info structure. The MSDN says
    If this flag (STARTF_USESTDHANDLES) is specified when calling the GetStartupInfo function, these members are either the handle value specified during process creation or INVALID_HANDLE_VALUE.
    Viggy

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Desperately Stuck...CreateProcess & Pipes

    A quick search of the MSDN brings up this example:

    http://msdn2.microsoft.com/en-us/library/ms682499.aspx

    Viggy

  5. #5
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    Re: Desperately Stuck...CreateProcess & Pipes

    Thanks for the reply, but I actually do get a redirected output, just updated my code again, and got the text to display in my edit text box control.

    It still won't display in a static text control, unsure as to why, but it doesn't matter, it displays fine in the edit control which will suit my purposes.

    But, it's only the first line, there should be like 5 lines, and it's only showing the first, any idea on how to get the rest to show?
    Last edited by bderagon; February 15th, 2008 at 05:04 PM.

  6. #6
    Join Date
    Oct 2007
    Location
    Fredericksburg, VA
    Posts
    41

    Re: Desperately Stuck...CreateProcess & Pipes

    Fixed it, stupid mistake.

  7. #7
    Join Date
    May 2007
    Posts
    1

    Re: Desperately Stuck...CreateProcess & Pipes

    Quote Originally Posted by bderagon View Post
    Fixed it, stupid mistake.
    Hi, I'm currently stuck with the same problem, and you seem to have solved it. Would you mind sharing the code?

    Cheers,
    Shup

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured