VS中为非控制台程序提供控制台输出窗口

/************************************************************************/
/* 
模块名:ConsoleAdapter
文件名:ConsoleAdapter.h  
功  能:为非控制台程序提供控制台输出窗口
参  考:
1. http://www.codeproject.com/Articles/15836/Writing-to-and-read-from-the-console-From-a-GUI-ap
-------------------------------------------------------------------------



#ifndef __CONSOLE_ADAPTER_H__
#define __CONSOLE_ADAPTER_H__

#include <iostream>
#include <io.h>
#include <fcntl.h>

using namespace std;

typedef enum
{
    INPUT_CONS,
    OUTPUT_CONS,
    BOTH
}CONSOLETYPE_e;

class CConsoleAdapter
{
public:
    CConsoleAdapter()
    {
        m_bHasCreate = FALSE;
    }
    ~CConsoleAdapter()
    {
        if (TRUE == m_bHasCreate)
        {
            DestroyConsole();
        }
    }

public:
    DWORD CreateConsole()
    {
        DWORD dwErrorCode = 0;

        do 
        {
            if (m_bHasCreate == TRUE)
            {
                break;
            }

            m_bHasCreate = AllocConsole();
            if (FALSE == m_bHasCreate)
            {
                dwErrorCode = GetLastError();
                break;
            }
            else
            {
                m_eConsoleType = OUTPUT_CONS;
                ReplaceHandles();
            }
        } while (FALSE);

        return dwErrorCode;
    }

    void DestroyConsole()
    {
        try
        {
            if (m_bHasCreate != TRUE)
            {
                return;
            }
            m_bHasCreate = FALSE;

            FreeConsole(); 
            if( ( INPUT_CONS == m_eConsoleType ) || ( BOTH == m_eConsoleType ) )
            {
//                 if( 0 != m_fpCRTIn )
//                 {
//                     fclose( m_fpCRTIn );
//                 }
//                 if (-1 != m_nCRTIn)
//                 {
//                     _close( m_nCRTIn );
//                     m_nCRTIn = -1;
//                 }
                
                *stdin = m_fOldStdIn;
            }

            if( ( OUTPUT_CONS == m_eConsoleType ) || ( BOTH == m_eConsoleType ) )
            {
//                 if( 0 != m_fpCRTOut )
//                 {
//                     fclose( m_fpCRTOut );
//                 }
//                 if (-1 != m_nCRTOut)
//                 {
//                     _close( m_nCRTOut );
//                     m_nCRTOut = -1;
//                 }
                
                *stdout = m_fOldStdOut;
            }
            
        }
        catch ( ... )
        {

        }

    }

private:
    BOOL ReplaceHandles()
    {
        try
        {
            if( ( INPUT_CONS == m_eConsoleType ) || ( BOTH == m_eConsoleType ) )
            {
                m_nCRTIn= _open_osfhandle(
                    (long) GetStdHandle(STD_INPUT_HANDLE),
                    _O_TEXT );
                if( -1 == m_nCRTIn )
                {
                    return FALSE;
                }
                m_fpCRTIn = _fdopen( m_nCRTIn, "r" );
                if( !m_fpCRTIn )
                {
                    return FALSE;
                }
                m_fOldStdIn = *stdin;
                *stdin = *m_fpCRTIn;
                // if clear is not done, any cout statement before AllocConsole
                // will cause, the cin after AllocConsole to fail, so very important
                std::cin.clear();
            }
            if( ( OUTPUT_CONS == m_eConsoleType ) || ( BOTH == m_eConsoleType ) )
            {
                m_nCRTOut= _open_osfhandle(
                    (long) GetStdHandle(STD_OUTPUT_HANDLE),
                    _O_TEXT );
                if( -1 == m_nCRTOut )
                {
                    return FALSE;
                }

                m_fpCRTOut = _fdopen( m_nCRTOut, "w" );
                if( !m_fpCRTOut )
                {
                    return FALSE;
                }
                m_fOldStdOut = *stdout;
                *stdout = *m_fpCRTOut;
                // if clear is not done, any cout statement before AllocConsole
                // will cause, the cout after AllocConsole to fail, so very important
                std::cout.clear();
            }
        }
        catch ( ... )
        {
            return FALSE;
        }        
        return TRUE;
    }

private:
    BOOL m_bHasCreate;
    CONSOLETYPE_e m_eConsoleType;

    FILE m_fOldStdIn;
    FILE* m_fpCRTIn;
    int m_nCRTIn;

    FILE m_fOldStdOut;
    FILE* m_fpCRTOut;
    int m_nCRTOut;
};

#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值