|
-
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.
-
February 15th, 2008, 04:20 PM
#2
Re: Desperately Stuck...CreateProcess & Pipes
Updated code with slight changes and still doesn't work as expected.
-
February 15th, 2008, 04:53 PM
#3
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
-
February 15th, 2008, 04:55 PM
#4
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
-
February 15th, 2008, 05:02 PM
#5
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.
-
February 15th, 2008, 05:32 PM
#6
Re: Desperately Stuck...CreateProcess & Pipes
Fixed it, stupid mistake.
-
August 18th, 2012, 03:37 AM
#7
Re: Desperately Stuck...CreateProcess & Pipes
 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
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
|