概述
HANDLE ReadPipe;
HANDLE WritePipe;
SECURITY_ATTRIBUTES PipeAttributes;
const DWORD nSize = 4096;
TCHAR buffer [ nSize ];
::ZeroMemory ( buffer , nSize );
PipeAttributes.nLength = sizeof ( PSECURITY_ATTRIBUTES );
PipeAttributes.bInheritHandle = TRUE;
PipeAttributes.lpSecurityDescriptor = NULL;
::CreatePipe ( & ReadPipe , & WritePipe , & PipeAttributes , 0 );
STARTUPINFO start;
::ZeroMemory ( & start , sizeof ( STARTUPINFO ) );
start.cb = sizeof ( start );
start.hStdOutput = WritePipe;
start.hStdInput = ReadPipe;
start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
start.wShowWindow = SW_HIDE;
PROCESS_INFORMATION ProcessInfo;
BOOL ret = CreateProcess ( NULL ,
"cmd.exe /c dir c://" ,
& PipeAttributes ,
& PipeAttributes ,
TRUE ,
NORMAL_PRIORITY_CLASS ,
NULL ,
NULL ,
& start ,
& ProcessInfo );
WaitForSingleObject ( ProcessInfo.hProcess , 1000 );
CString out;
DWORD i;
ReadFile ( ReadPipe , buffer , nSize - 1 , & i , NULL );
buffer [ nSize - 1 ] = 0;
OemToAnsi ( buffer , buffer );
out = buffer;
AfxMessageBox ( out );
CloseHandle ( ProcessInfo.hProcess );
CloseHandle ( ProcessInfo.hThread );
CloseHandle ( ReadPipe );
CloseHandle ( WritePipe );
最后
以上就是可爱眼睛为你收集整理的怎样捕捉控制台程序的输出?的全部内容,希望文章能够帮你解决怎样捕捉控制台程序的输出?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复