zoukankan      html  css  js  c++  java
  • wxWidgets中如何使用输入输出重定向调用外部程序

    网上先是搜索到此篇文章: http://hi.baidu.com/xxai/blog/item/035f2bad22331d0b4a36d6ee.html

    程序是被成功调用了.但没有获取到输出.搜索wxWidgets的exec例程.
    发现需要自己实现OnTerminate函数来捕获输出. OnTerminate会在应用程序执行完毕后被调用.

    #include <wx/process.h>
    class MyProcess : public wxProcess
    {
    public:
    MyProcess(): wxProcess()
    {
    Redirect(); //在构架函数里面直接设置输出重定向.
    }

    // instead of overriding this virtual function we might as well process the
    // event from it in the frame class - this might be more convenient in some
    // cases
    virtual void OnTerminate(int pid, int status);
    };

    void MyProcess::OnTerminate(int pid, int status)
    {
    #define READ_LEN 256

    if ( IsInputAvailable() )
    {
    wxInputStream * ResultStream = GetInputStream();

    if( ResultStream != NULL )
    {
    wxString result = _("");
    char read_buffer[READ_LEN];

    while( 1 )
    {
    ResultStream->Read(read_buffer,READ_LEN-1); /**< 最后至少要留空一个字节用于放置 \0 */
    size_t byte_read = ResultStream->LastRead();
    if( byte_read == 0 )
    {
    break;
    }
    read_buffer[byte_read] = '\0'; /**< 在最后插入一个字符结束符 */
    result += wxString(read_buffer, wxCSConv(wxT("gb2312"))); /**< 编码转换 */
    }
    /**< 向文本框输出字符 */
    }
    }

    if ( IsErrorAvailable() )
    {
    terminal->AppendText(_("---- error ----\r\n"));
    wxInputStream * ResultStream = GetErrorStream();

    if( ResultStream != NULL )
    {
    wxString result = _("");
    char read_buffer[READ_LEN];

    while( 1 )
    {
    ResultStream->Read(read_buffer,READ_LEN-1); /**< 最后至少要留空一个字节用于放置 \0 */
    size_t byte_read = ResultStream->LastRead();
    if( byte_read == 0 )
    {
    break;
    }
    read_buffer[byte_read] = '\0'; /**< 在最后插入一个字符结束符 */
    result += wxString(read_buffer, wxCSConv(wxT("gb2312"))); /**< 编码转换 */
    }
    /**< 向文本框输出字符 */
    }
    }
    }

    MyProcess * pMyProcess = new MyProcess();
    wxString cmd = _("cmd /c ver ");
    wxExecute(cmd,wxEXEC_ASYNC,pMyProcess); // 启动外部程序
  • 相关阅读:
    洛谷P3620 [APIO/CTSC 2007] 数据备份
    洛谷P2744 量取牛奶
    洛谷P1560 蜗牛的旅行
    luogu P1776 宝物筛选_NOI导刊2010提高(02)
    luogu P1020 导弹拦截
    luogu P2015 二叉苹果树
    luogu P1137 旅行计划
    树形dp瞎讲+树形dp基础题题解
    luogu P1252 马拉松接力赛 P1803 凌乱的yyy / 线段覆盖
    luogu P1196 [NOI2002]银河英雄传说
  • 原文地址:https://www.cnblogs.com/aozima/p/2199826.html
Copyright © 2011-2022 走看看