/************************************************************************/
/*
模块名: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 = ; 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( - == 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( - == 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

最新文章

  1. Can&#39;t find any matching row in the user table
  2. 【转】selenium学习路线
  3. Android PopupWindow使用之地区、学校选择二级联动
  4. OC中NSDictionary和NSSet简单操作
  5. 读jQuery官方文档:数据方法与辅助方法
  6. ASP怎样获得代码中第一张图片地址
  7. Android内的生命周期整理
  8. 单片机IO处理 电容触摸按键
  9. linux之模拟简单登录的脚本
  10. 自学Xpath的几个例子
  11. [Splay模版1]
  12. eclipse构建maven+scala+spark工程
  13. IE的变态
  14. JournalNode failed to restart
  15. 【安富莱】【RL-TCPnet网络教程】第7章 RL-TCPnet网络协议栈移植(裸机)
  16. django入门与实践 - 关于升级到django 3.7,三种模板超链接配置(编辑中)
  17. QTcpSocket 相关知识总结
  18. vue-cli watch简单用法
  19. [转载]PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写
  20. pyculiarity 时间序列(异常流量)异常检测初探——感觉还可以,和Facebook的fbprophet本质上一样

热门文章

  1. Spring Boot (#1 quick start)
  2. 13 款惊艳的 Node.js 框架——第1部分
  3. Python 面向对象补充
  4. 3hibernate核心对象关系映射 xxx.hbm.xml
  5. 关于SQLSERVER走起公众帐号推送视频的通知
  6. pyhon类继承
  7. Ubuntu安装ffmpeg
  8. c#WebApi使用form表单提交excel,实现批量写入数据库
  9. 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
  10. Jar版本:java.lang.UnsupportedClassVersionError: ******