[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);
Re: Desperately Stuck...CreateProcess & Pipes
Updated code with slight changes and still doesn't work as expected.
Re: Desperately Stuck...CreateProcess & Pipes
Well, one thig I noticed, you only set the 'hStdOutput' handle in the startup info structure. The MSDN says
Quote:
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
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
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?
Re: Desperately Stuck...CreateProcess & Pipes
Fixed it, stupid mistake.
Re: Desperately Stuck...CreateProcess & Pipes
Quote:
Originally Posted by
bderagon
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