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); // 启动外部程序
  • 相关阅读:
    Prime Ring Problem 素数环
    下沙的沙子有几粒?
    小兔的棋盘
    超级楼梯
    一只小蜜蜂...
    变形课
    Buy the Ticket
    How Many Trees?
    通过拦截器来统计每个action的执行时间
    apache+tomcat+session(JK实现方式)
  • 原文地址:https://www.cnblogs.com/aozima/p/2199826.html
Copyright © 2011-2022 走看看