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); // 启动外部程序
  • 相关阅读:
    错误C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
    为什么DW的可视化下看到的效果与浏览器的效果有所区别?
    font-family:黑体;导致css定义全部不起作用
    web标准中定义id与class有什么区别吗
    网页尺寸规范
    SEO为什么要求网页设计师用DIV+CSS布局网页?
    去掉CSS赘余代码,CSS可以更简洁
    解决IE6、IE7、Firefox兼容最简单的CSS Hack
    实战中总结出来的CSS常见问题及解决办法
    高效整洁CSS代码原则 (下)
  • 原文地址:https://www.cnblogs.com/aozima/p/2199826.html
Copyright © 2011-2022 走看看