https://www.codePRoject.com/kb/cpp/editbin.aspx
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.)
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.
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:WINDOWSThe 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.exeSide 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)
新闻热点
疑难解答