Scene:
1. Develop Windows interface program, need to print output, breakpoints in debugging multithreaded programs have limitations, is to interfere with the priority order of threads, see the correct results, often is the breakpoint is OK,
No breakpoint program will not work properly.
2. When using the dynamic library (static library), when the problem occurs in the DLL write std::cout output information is an essential debugging method, especially for multi-threaded programs, who first who after the pause after the results will be different.
Sometimes there is no code for a dynamic library, and there is no way to break a point on a dynamic library.
3. When you open the console window, you can only see the output of the Windows program and you cannot see the DLL output?
4. Directly change the configuration Properties-"linker-" Subsystem for the console (/subsystem:console) is not realistic, because we are developing interface programs.
Method 1, view the print log in the Output window, using the function:
void WINAPI OutputDebugString ( __in_opt lpctstr lpoutputstring);
Method 2, open the console window like the console program, do not need outputdebugstring, in order to view std::cout, printf output, add code:
static void Openconsole () {allocconsole (); HANDLE HANDLE = getstdhandle (std_output_handle); int hcrt = _open_osfhandle ((long) handle,_o_text); FILE * HF = _fdopen ( hcrt, "W" ); *stdout = *hf; }
At the beginning of the _tWinMain call
#ifdef _debugopenconsole (); #endif
Called at the end
#ifdef _debugfreeconsole (); #endif
here's the problem.
The std::cout output we wrote in our own DLL library was not printed out?
The reason is that DLLs and Windows are not using the same run-time library, which causes openconsole () to invalidate the dynamic library.
The solution is to have 2 steps to confirm that both the interface and the dynamic library require the same configuration :
1. Configuration Properties-"General-" MFC usage: Using MFC in Shared DLLs
2. Configuration Properties-"C + +-" code Generation-"Multithreaded Debug DLL (/MDD)
Now you can see the output of the DLL in the console, no thanks. ^-^
Reference:
Redirecting console output of C + + DLL used in MFC GUI application
[windows]_[Intermediate]_[Interface Program open console output-forward output to console]