我是靠谱客的博主 可爱眼睛,这篇文章主要介绍怎样捕捉控制台程序的输出?,现在分享给大家,希望可以做个参考。

    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 );

最后

以上就是可爱眼睛最近收集整理的关于怎样捕捉控制台程序的输出?的全部内容,更多相关怎样捕捉控制台程序内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部