麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

MFC|Console Output from a GUI program(MFC調用cout輸出)

2019-11-06 08:04:10
字體:
來源:轉載
供稿:網友

https://www.codePRoject.com/kb/cpp/editbin.aspx

Sample Image - EditBin.gif

Introduction

If you create a Windows program using the MFC application Wizard, the program will not show text output which has been sent to the Standard Output or Standard Error handles.

Visibility of the output may be desirable at some point in time for diagnostic or functional purposes. Or you may wish to add command line Operation of the program. Then to be consistent with command line operation, text should automatically be output to the command/dos window.

This article documents the use of EditBin utility to activate console mode operation of a Windows (or GUI ) program. (And for those who want some source code, the program contains an EditBin program relating to redirection of thecout stream object.)

Alternatives for obtaining text output

Run the Windows program from a command prompt and redirect the output using the redirection feature of the command window: ">out.txt" and "2>errors.txt"

Reassign the standard output and error output handles to be piped or sent to another file using::SetStdHandle(). This process seems to be tricky to achieve.

You may write a console program that remaps the stdout, stdin, andstderr provided to a spawned program. The spawn program would be the one which has the output that you want to collect.

The Microsoft EditBin Utility

EditBin.exe is found in the VC98/bin subpath of Visual Studio 6.

This obscure tool offers the key for enabling text console output from Windows mode programs written in Visual C++ 6 (or Visual Basic 6). You can toggle the program executable between Windows mode and console mode.

EditBin MyProgram.exe /SUBSYSTEM:CONSOLE EditBin MyProgram.exe /SUBSYSTEM:WINDOWS

Including the Editbin utility in the build process

The article image shows Visual Studio 6 Project Settings to show in situ how the Microsoft EditBin utility appears when added to become part of the build process.

The "Post-Build Step" tab of the Projects Settings dialog allows you to enter in a command on your final executable. The picture shows the example:

editbin /SUBSYTEM:CONSOLE $(OUTDIR)/iTHXTool.exe

Side Effect for a Console version of the program

The console window will be appear along with the application window. Such may be the desired option if you want to view diagnostic information. If you start the program from a command prompt, the command window will show the Standard Output and Standard Error text.

You may have to create two copies of your application: a strictly GUI program, and a command line version. The programs will involve the same compiled code, but one will be modified using the EditBin tool.

Also a Microsoft help entry mentioned that the file handles 0,1 and 2 (that should be equivalent somehow tostdout andstderr) do not get reassigned since these values are assigned in the CRT startup of the Windows program. (Yet you may be able to modify the CRT using the files from thevc98/CRT/source directory.)

One last observation on the VS6 project settings...

You may notice "/subsystem:windows" as appears in an MFC Wizard generated application under the Project Settings Link tab. Yet console output can't be obtained by simply changing this entry to/subsystem:console.

Some code for redirection of cout

The source code program here, provides an example for redirecting the cout stream object. For some reason(s), the source code doesn't properly redirect thestdout file handle, though various websites describe that thestdout should be redirectable.

The application consists of a text editor window that is automatically filled with the time of day at ten seconds intervals. The program relies on piped output which is being read by aReadFile call operating within a thread.

The heart of the redirection occurs within the initialization of the main application object. For all related code, search for the "April 2005" string which will lead you to all code modifications to the standard wizard generated code.

Initialization Steps

rval = CreatePipe(&hRead,&hWrite,0,0);rval = SetStdHandle(STD_OUTPUT_HANDLE,hWrite);//Now assign hWrite to stdout, coutFILE *fp;int hConHandle;long lStdHandle;lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);fp = _fdopen( hConHandle, "w" );// The stdout reassignment didn't seem to work// *stdout = *fp;  // redefine the stdout to point to the pipesetvbuf( fp, NULL, _IONBF, 0 ); // Filebuf object attached to "test.dat"filebuf *fb = new filebuf(_fileno(fp  ) );cout = fb;

This program shows a little bit of how redirection can be done. Note that the buffering of text (for text being inserted into the RTF document) is not designed ruggedly. The code bypasses the need to use the MicrosoftEditBin.exe tool in some but not all cases.

It would have been desirable to write the data to a memory file instead of sending data through a pipe. Then there would not have been a need to create a thread to watch the output of the pipe. Anyhow, the_open_osfhandle failed to convert a memory map handle to a typical file handle (inhConHandle).

Conclusion

The Windows GUI development doesn't conveniently support the access to cout,cerr,stdout, orstderr. It may be difficult to obtain these under direct control of the window program. At least there exist some tricks to obtain the information if really needed.

License

This article, along with any associated source code and files, is licensed underThe Code Project Open License (CPOL)


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩视频一区 | 亚洲第一页综合 | 涩涩伊人 | 亚洲成人午夜精品 | 老女人碰碰在线碰碰视频 | av免费在线观看av | 免费啪视频在线观看 | 日本黄色大片免费 | 高清做爰免费无遮网站挡 | 国产精品区在线12p 午夜视频色 | 亚洲国产精品一区二区三区 | 一级毛片在线观看免费 | 久久久久免费精品国产小说色大师 | 超碰人人做人人爱 | 污版视频在线观看 | 一日本道久久久精品国产 | av免费在线免费观看 | av色先锋| 91精品国产91热久久久做人人 | 久久久久久91 | 欧美日韩国产一区二区三区在线观看 | 黄色毛片免费视频 | 蜜桃麻豆视频 | 日本高清黄色片 | 91精品国产99久久久久久红楼 | 久久艹国产精品 | 日本在线播放一区二区 | 国产一级淫片在线观看 | 成人爽a毛片免费啪啪红桃视频 | 精品中文字幕在线播放 | 国产精品av久久久久久久久久 | 成人在线免费小视频 | 久久久久久久久久91 | 国产合集91合集久久日 | 久久久久久久91 | 99精品视频网站 | 久久久精品福利 | 国产一区在线观看视频 | 日韩精品a在线观看 | 中文字幕一二三区芒果 | 国产精品三级a三级三级午夜 |