在Win32应用程序中添加控制台窗口

 

1.  背景知识介绍

1.Win32 Handles和RTL Handles
标准C定义的文件操作类型为FILE*
Win32 API定义文件操作的类型为 HANDLE,  在Windows平台上, 标准C的输入输出函数是HANDLE实现, 标准C的输入输出FILE* 类型会和相应的HANDLE关联。
标准C函数预定义了三个FILE* 变量:  stdin, stdout和stderr
Win32 也分别定义了这三个HANDLE, 可以通过HANDLE GetStdHandle() 函数取得

GetStdHandle 参数:
STD_INPUT_HANDLE
STD_OUTPUT_HANDLE
STD_ERROR_HANDLE
这些HANDLE都可以重定向到文件或者控制台窗口,Win32 GUI程序没有控制台窗口,所以Win32的这三个File HANDLE也没有指向Standard input, output, error,  但是我们可以将这三个 FILE HANDLE重定向到控制台窗口,然后将标准C FILE* 关联到这些HANDLE。然后我们就可以正常使用C标准输入输出函数了。

其他用到的函数
_open_osfhandle() 函数, 返回一个RTL handle, 这个RTL handle与Win32  FILE HANDLE相关联, 通过这个RTL handle重定向I/O

FILE *_fdopen(int fd, const char *mode ); 基于文件描述符打开一个C输入输出流 


代码

  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. #include <io.h>  
  4. #include <iostream>  
  5. #include <fstream>  
  6. #include <iostream>  
  7. #ifndef _USE_OLD_IOSTREAMS  
  8. using namespace std;  
  9. #endif  
  10. // maximum mumber of lines the output console should have  
  11. static const WORD MAX_CONSOLE_LINES = 500;  
  12. void RedirectIOToConsole()  
  13. {  
  14. int hConHandle;  
  15. long lStdHandle;  
  16. CONSOLE_SCREEN_BUFFER_INFO coninfo;  
  17. FILE *fp;  
  18. // allocate a console for this app  
  19. AllocConsole();  
  20. // set the screen buffer to be big enough to let us scroll text  
  21. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);  
  22. coninfo.dwSize.Y = MAX_CONSOLE_LINES;  
  23. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),  
  24. coninfo.dwSize);  
  25. // redirect unbuffered STDOUT to the console  
  26. lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);  
  27. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);  
  28. fp = _fdopen( hConHandle, "w" );  
  29. *stdout = *fp;  
  30. setvbuf( stdout, NULL, _IONBF, 0 );  
  31. // redirect unbuffered STDIN to the console  
  32. lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);  
  33. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);  
  34. fp = _fdopen( hConHandle, "r" );  
  35. *stdin = *fp;  
  36. setvbuf( stdin, NULL, _IONBF, 0 );  
  37. // redirect unbuffered STDERR to the console  
  38. lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);  
  39. hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);  
  40. fp = _fdopen( hConHandle, "w" );  
  41. *stderr = *fp;  
  42. setvbuf( stderr, NULL, _IONBF, 0 );  
  43. // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog  
  44. // point to console as well  
  45. ios::sync_with_stdio();  
  46. }  

 

测试

  1. void test()  
  2. {  
  3. int iVar;  
  4. RedirectIOToConsole();  
  5. // test stdio  
  6. fprintf(stdout, "Test output to stdout/n");  
  7. fprintf(stderr, "Test output to stderr/n");  
  8. fprintf(stdout, "Enter an integer to test stdin: ");  
  9. scanf("%d", &iVar);  
  10. printf("You entered %d/n", iVar);  
  11. //test iostreams  
  12. cout << "Test output to cout" << endl;  
  13. cerr << "Test output to cerr" << endl;  
  14. clog << "Test output to clog" << endl;  
  15. cout << "Enter an integer to test cin: ";  
  16. cin >> iVar;  
  17. cout << "You entered " << iVar << endl;  
  18. }  

 

参考

http://www.halcyon.com/~ast/dload/guicon.htm

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值