lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


thanks Asko Kauppi and same to others.
 
I have tried Ask Kauppi's suggestion, not resolve the problem, and I had tried others[ as follow code]. here are the source code in my application:
/*
*   读取控制台输出管道内容线程
*/
unsigned long __stdcall ConsoleStdoutPipeReadThread(void * lpParameter)
{
    DWORD dwRead;
    char szPipeData[PIPE_READ_THREAD_BUFFER_SIZE];
    HANDLE stdoutPipeReadHandle = (HANDLE)lpParameter;
    for( ;; )
    {
        memset(szPipeData, '\0', sizeof(szPipeData));
        if(!ReadFile(stdoutPipeReadHandle, szPipeData, PIPE_READ_THREAD_BUFFER_SIZE - 1,  &dwRead, NULL) || dwRead == 0)
        {
            break;
        }
        szPipeData[dwRead / sizeof(char)] = '\0';
        // 写入控制台日志到输出窗口
        theCockroachApp.GetWorkbench().GetOutputPane()->Write(szPipeData);
    }
    // 关闭控制台输出管道读句柄
    CloseHandle(stdoutPipeReadHandle);
    return 0;
}

BOOL CApplication::InitializeScriptHost()
{
    try
    {
        m_ScriptHost.Initialize();
    }
    catch(CAppException *pEx)
    {
        pEx->ReportError();
        pEx->Delete();
        return FALSE;
    }
    DWORD dwReadThreadID = 0;
    m_ConsoleStdoutPipeReadThread = ::CreateThread(NULL, 0, ConsoleStdoutPipeReadThread, (LPVOID)m_ScriptHost.GetConsoleStdoutPipe(), CREATE_SUSPENDED, &dwReadThreadID);
    ::SetThreadPriority(m_ConsoleStdoutPipeReadThread, THREAD_PRIORITY_BELOW_NORMAL);
    ::ResumeThread(m_ConsoleStdoutPipeReadThread);
    return TRUE;
}
 
void CScriptHost::Initialize()
{
    // 保存系统标准输出
    m_StdoutOldFileDescriptor = _dup(/*_fileno(stdout)*/ 1);
    // 创建控制台管道
    if (!CreatePipe(&m_ConsolePipeStdoutRead, &m_ConsolePipeStdoutWrite, NULL, 0))
    {
        UserThrowException("Console stdout pipe creation failed!");
    }
    // 把控制台输出转移到管道上
    m_PipeWriteFileDescriptor = _open_osfhandle((long)m_ConsolePipeStdoutWrite, _O_TEXT | _O_APPEND);
    if(-1 == _dup2(m_PipeWriteFileDescriptor, /*_fileno(stdout)*/ 1))
    {
        // 关闭管道读写句柄
        ClosePipeHandles();
        UserThrowException("Redirect console stdout to pipe failed!");
    }
    ////iPipeWriteHandle = _open_osfhandle((long)m_ConsolePipeStdoutWrite, _O_TEXT | _O_APPEND);
    //if(-1 == _dup2(m_PipeWriteFileDescriptor, /*_fileno(stderr)*/ 2))
    //{   
    //    // 关闭管道读写句柄
    //    ClosePipeHandles();
    //    UserThrowException("Redirect console stderr to pipe failed!");
    //}
    //=============================================================================================
    // 重新打开管道文件,设置其缓冲区为零【缓冲区非零时无法正常输出】
    //m_PipeWriteFileHandler = _fdopen(m_PipeWriteFileDescriptor, "w");
    //if(m_PipeWriteFileHandler ==NULL)
    //{
    //    // 关闭管道读写句柄
    //    ClosePipeHandles();
    //    UserThrowException("Reopen console stdout pipe writer file failed!");
    //}
    //fflush(stdout);
    //setvbuf(stdout, NULL, _IONBF, 0);
    //_commit(1 /* stdout */);

    //// 设置管道文件缓冲区【不缓冲输出】
    //if(setvbuf(m_PipeWriteFileHandler, NULL, _IONBF, 0) != 0)
    //{
    //    // 关闭管道读写句柄
    //    ClosePipeHandles();
    //    UserThrowException("Set console stdout pipe writer file buffer size failed!");
    //}
 
    // 设置初始化标记
    m_Initialized = TRUE;
}
 
void CScriptHost::ClosePipeHandles()
{
    // 关闭打开的文件描述器对象
    if(m_PipeWriteFileDescriptor)
    {
        _close(m_PipeWriteFileDescriptor);
        m_PipeWriteFileDescriptor = 0;
    }
    // 关闭控制台输出管道写句柄
    if(m_ConsolePipeStdoutWrite)
    {
        CloseHandle(m_ConsolePipeStdoutWrite);
        m_ConsolePipeStdoutWrite = 0;
    }
    // 关闭控制台输出管道读句柄
    if(m_ConsolePipeStdoutRead)
    {
        CloseHandle(m_ConsolePipeStdoutRead);
        m_ConsolePipeStdoutRead = 0;
    }
    // 恢复系统标准输出
    _dup2(m_StdoutOldFileDescriptor, /*_fileno(stdout)*/ 1);
}
 
 
------------------
Andy Tao[陶祖洪]
祖洪测试自动化 http://www.zuhong.cn
天是圆的,地是方的,凡事都要有个规矩!
 
 
------------------ Original ------------------
Date:  Sun, Oct 26, 2008 04:13 PM
To:  "Lua list"<lua@bazar2.conectiva.com.br>;
Subject:  Re: Why cann't capture the stdout
 

If the Lua process does not run to its end, this can also be caused by 
buffering. stderr is flushed at each output whereas stdout is normally 
buffered.

You can change Lua stdout buffering mode by

    io.stdout:setvbuf "no"

http://www.lua.org/manual/5.1/manual.html#pdf-file:setvbuf

-asko


V S P kirjoitti 26.10.2008 kello 9:55:

> I am using PHP's proc_open
>
> and can receive with no problem output
> of Lua's print commands
>
>
> (using lua 5.1.4 on Windows for now)
>
> So I am thinking you are doing something wrong
> on the MFC side
>
>
>
> On Sun, 26 Oct 2008 09:34:54 +0200, "Dennis Povshedny" <dp@xeepe.com>
> said:
>> Hello Andy.Tao,
>>
>> Hi Andy!
>>
>> Not answering your question, but just a hint: probably you can
>> consider usage of io.write() instead of print?
>>
>> Regards, Dennis
>>
>> Hi gus,
>>
>> I have meet a trouble, does anyone could help me?
>>
>> I embeded lua in my MFC program, and create pipe to receive lua
>> stdout text, it can receive error message but print, why? code
>> context is:
>>
>> 1. Create Pipe: CreatePipe(&PipeRead, &PipeWrite, NULL, 0)
>>
>> 2. Assign pipe write handle to stdout and stderr:
>>
>> _dup2(_open_osfhandle(PipeRead, _O_TEXT), 1)
>>
>> _dup2(_open_osfhandle(PipeRead, _O_TEXT), 2)
>>
>> 3. Create thread to read pipe read handle
>>
>> 4. Execute lua code which contain statement: print and error
>>
>> 5. It can receive statement(error) message but print
>>
>> thanks in advanced!
>>
>> ------------------
>>
>> Andy Tao[&#38518;&#31062;&#27946;]
>>
>> &#31062;&#27946;&#27979;&#35797;&#33258;&#21160;&#21270; [1]http://www.zuhong.cn
>>
>> &#22825
>> ;&#26159
>> ;&#22278
>> ;&#30340
>> ;&#65292
>> ;&#22320
>> ;&#26159
>> ;&#26041
>> ;&#30340
>> ;&#65292
>> ;&#20961
>> ;&#20107;&#37117;&#35201;&#26377;&#20010;&#35268;&#30697;&#65281;
>>
>> --
>>
>> Best regards,
>>
>> Dennis                            [2]mailto:dp@xeepe.com
>>
>> References
>>
>> 1. http://www.zuhong.cn/
>> 2. mailto:dp@xeepe.com
> --
>  V S P
>  toreason@fastmail.fm
>
> --
> http://www.fastmail.fm - Same, same, but different...
>