|
-
February 15th, 2008, 04:04 PM
#1
[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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|