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); // 启动外部程序
  • 相关阅读:
    基于C#.NET的Windows进程管理工具
    .NET 中的MD5及hash加密以及密文输出(C#)
    你不一定会的宝宝级数学题
    用C#实现图片文件与base64string编码解码
    怵目惊心的图片
    古鸽和犤毒鸟的故事(扫盲一日)
    office安装问题
    JS 设置与获取Cookie,函数setCookie(name,value,time)getCookie(name)
    JavaScript解码url中的中文字符的方法
    js追加事件FF IE兼容
  • 原文地址:https://www.cnblogs.com/aozima/p/2199826.html
Copyright © 2011-2022 走看看