[windows]_[Intermediate]_[Interface Program open console output-forward output to console]

Source: Internet
Author: User

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.