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

VS中为非控制台程序提供控制台输出窗口的更多相关文章

  1. Net Core 控制台程序使用Nlog 输出到log文件

    using CoreImportDataApp.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions ...

  2. c# 控制台程序 隐藏控制台窗口

    在某些项目中,需要采用控制台程序,但是又不需要通过dos窗口进行交互,同时打算隐藏掉难看的控制台窗口.实现的方法很多,有的是修改链接命令.我采用的方法略有些麻烦,首先是给窗口命名,之后找到该窗口指针, ...

  3. Delphi中如何控制其他程序窗体上的窗口控件

    回调函数一般是按照调用者的要求定义好参数和返回值的类型,你向调用者提供你的回调函数的入口地址,然后调用者有什么事件发生的时候就可以随时按照你提供的地址调用这个函数通知你,并按照预先规定好的形式传递参数 ...

  4. 控制台程序的中文输出乱码问题(export LC_CTYPE=zh_CN.GBK,或者修改/etc/sysconfig/i18n为zh_CN.GBK。使用setlocale(LC_CTYPE, "");会使用默认办法。编译器会将源码做转换成Unicode格式,或者指定gcc的输入文件的编码参数-finput-charset=GBK。Linux下应该用wprintf(L"%ls/n",wstr))

    今天发现用securecrt登陆时,gcc编译出错时会出现乱码,但直接在主机的窗口界面下用Shell编译却没有乱码.查看了一下当时的错误描述,发现它的引号是中文引号,导致在SecureCRT中显示出错 ...

  5. C#控制台程序,运行完窗口不退出的方法

    .... static void Main(string[] args){ Console.WriteLine("运行完后不退出窗口"); Console.ReadKey();// ...

  6. PyCharm中运行同一个python程序时选择平行窗口运行

    问题描述 当我们进行Socket编程时,客户端可能有多个,原则上如果有n个客户端,那么我们就要编辑n客户端的代码.然而其实我们每个客户端的代码都是相同,如果编辑n遍,将会相当的浪费空间. 解决办法 学 ...

  7. C++ gui程序附加dos输出窗口

    C++ gui程序附加console qtcreator 1:在.pro文件中加入一句: CONFIG+= console 2:在运行设置里勾选在终端运行的选项 vs 1.新建gui项目 2.连接器( ...

  8. C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...

  9. VC++中的C运行时库浅析(控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    1.概论 运行时库是程序在运行时所需要的库文件,通常运行时库是以LIB或DLL形式提供的.C运行时库诞生于20世纪70年代,当时的程序世界还很单纯,应用程序都是单线程的,多任务或多线程机制在此时还属于 ...

随机推荐

  1. go语言练习:通道、协程

    1.通道的简单例子: package main import "fmt" func main() { channel:=make(chan string) //创建了一个通道对象 ...

  2. LeetCode题解之Rotate String

    1.题目描述 2.问题分析 直接旋转字符串A,然后做比较即可. 3.代码 bool rotateString(string A, string B) { if( A.size() != B.size( ...

  3. webstorm使用过程中的一些问题与技巧

    这一篇会随着使用逐渐更新: 1. 问题:string templates are not supported by current javascript version 解决 : setting &g ...

  4. innodb compressed 表碰到的问题,BUG么?

    innodb compressed 表碰到的问题 (摘自 http://papaisadba.puyu.me/?p=239) compressed innodb 表是MySQL 5.5开始提供的功能, ...

  5. SQL 中常用存储过程xp_cmdshell运行cmd命令 (转载)

    目的:使用SQL语句,在D盘创建一个文件夹myfile 首先查询系统配置 SELECT * FROM sys.configurations WHERE name='xp_cmdshell' OR na ...

  6. LINUX使用extundelete进行数据恢复

    从删库到跑路不是一句笑话,不小心误删除操作很容易造成整个公司业务崩溃.所以真的误删除了,第一时间就是赶紧停机,不要再进行任何写入操作,把这块磁盘挂载到其他机器进行数据恢复 这里我们介绍一下使用 LIN ...

  7. pycharm的放大和缩小字体的显示 和ubunt的截圖工具使用 ubuntu上安装qq微信等工具

    https://www.cnblogs.com/sui776265233/p/9322074.html#_label0 ubuntu: 截圖工具的使用 在ubuntu 10.04 的时候,还可以很方便 ...

  8. [Spark Core] Spark 使用第三方 Jar 包的方式

    0. 说明 Spark 下运行job,使用第三方 Jar 包的 3 种方式. 1. 方式一 将第三方 Jar 包分发到所有的 spark/jars 目录下 2. 方式二 将第三方 Jar 打散,和我们 ...

  9. [Spark Core] Spark 实现气温统计

    0. 说明 聚合气温数据,聚合出 MAX . MIN . AVG 1. Spark Shell 实现 1.1 MAX 分步实现 # 加载文档 val rdd1 = sc.textFile(" ...

  10. glViewport()函数和glOrtho()函数的理解(转)

    http://www.cnblogs.com/yxnchinahlj/archive/2010/10/30/1865298.html 在OpenGL中有两个比较重要的投影变换函数,glViewport ...