为WIN32窗口应用程序附加控制台窗口,方便输入输出调试信息
来源:互联网 发布:遗传算法 图形分割 编辑:程序博客网 时间:2024/06/06 02:16
转自:http://www.codeguru.com/Cpp/W-D/console/redirection/article.php/c3961/
// ConsoleWinApp.cpp : implementation file//#include "stdafx.h"#ifdef _CONSOLEWIN//Set subsystem to console#pragma comment ( linker, "/subsystem:console" )BOOL WINAPI ConsoleWinHandlerRoutine( DWORD dwCtrlType ){ // Signal type switch( dwCtrlType ) { case CTRL_C_EVENT: case CTRL_BREAK_EVENT: case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: case CTRL_SHUTDOWN_EVENT: // You can stop here gracefully: // // AfxGetMainWnd()->SendMessage( WM_CLOSE, 0, 0 ); // WaitForSingleObject( AfxGetThread(), INFINITE ); // ExitProcess(0); break; } return TRUE;}// Console main functionint _tmain( DWORD, TCHAR**, TCHAR** ){#define SPACECHAR _T(' ')#define DQUOTECHAR _T('/"') // Set the new handler SetConsoleCtrlHandler( ConsoleWinHandlerRoutine, TRUE ); // Get command linLPTSTR lpszCommandLine = ::GetCommandLine();if(lpszCommandLine == NULL)return -1;// Skip past program name (first token in command line).// Check for and handle quoted program name.if(*lpszCommandLine == DQUOTECHAR){// Scan, and skip over, subsequent characters until// another double-quote or a null is encountered.do{lpszCommandLine = ::CharNext(lpszCommandLine);}while((*lpszCommandLine != DQUOTECHAR) && (*lpszCommandLine != _T('/0')));// If we stopped on a double-quote (usual case), skip over it.if(*lpszCommandLine == DQUOTECHAR)lpszCommandLine = ::CharNext(lpszCommandLine);}else{while(*lpszCommandLine > SPACECHAR)lpszCommandLine = ::CharNext(lpszCommandLine);}// Skip past any white space preceeding the second token.while(*lpszCommandLine && (*lpszCommandLine <= SPACECHAR))lpszCommandLine = ::CharNext(lpszCommandLine);STARTUPINFO StartupInfo;StartupInfo.dwFlags = 0;::GetStartupInfo(&StartupInfo);return _tWinMain(::GetModuleHandle(NULL), NULL, lpszCommandLine,(StartupInfo.dwFlags & STARTF_USESHOWWINDOW) ?StartupInfo.wShowWindow : SW_SHOWDEFAULT);}#endif // _CONSOLEWIN
Usage
Crete your MFC windows project and add the ConsoleWindowsApp.cpp or this source code to your project, and
- Do Define _CONSOLEWIN in your project settings, if you want your project to work like a console and windows application. The standard output, and input will be redirected to the parent console. You can use cout, cerr, cin, printf, etc.
- Do not define _CONSOLEWIN in your project settings, and you will get a windows application. (Default)
- 为WIN32窗口应用程序附加控制台窗口,方便输入输出调试信息
- MFC,Win32程序向控制台(Console)窗口输出调试信息
- Win32应用程序使用Console窗口输出调试信息
- 在Win32应用程序中添加控制台窗口
- 如何隐藏win32控制台应用程序的窗口
- Win32应用程序中创建控制台窗口
- 在Win32应用程序中添加控制台窗口
- Win32应用程序中创建控制台窗口
- win32控制台应用程序不显示窗口启动
- Window应用程序使用控制台窗口调试
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 在MFC,Win32程序中向控制台(Console)窗口输出调试信息
- 打开控制台窗口以显示调试信息
- 实验: 用控制台窗口查看调试信息
- Mysql 游标应用
- Coby推出了全新收音机
- Websphere6报“不推荐使用资源 jdbc/xxx 的直接 JNDI 查找”解决办法
- Mysql 示图应用
- 在websphere上一个J2EE应用中引用多个数据源JNDI的方法
- 为WIN32窗口应用程序附加控制台窗口,方便输入输出调试信息
- WEBSPHERE连接数据库测试错误Need to specify class name in environment or system property, or as an applet parameter, or in an application
- js幻灯片播放器
- Java和Eclipse中加载本地库研究
- TL-WN321G (rt73)USB无线网卡驱动在armlinux平台上的移植
- 24个常用方法有效优化ASP.NET的性能(三)
- Cognos8.3 安装及配置
- javascript技巧参考
- JSP字符中文乱码问题解决方法汇总