我是靠谱客的博主 可爱眼睛,最近开发中收集的这篇文章主要介绍怎样捕捉控制台程序的输出?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

最后

以上就是可爱眼睛为你收集整理的怎样捕捉控制台程序的输出?的全部内容,希望文章能够帮你解决怎样捕捉控制台程序的输出?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部