user1741137
user1741137

Reputation: 5058

When running an MFC application is there a way to output to a "console"?

I'm used to developing with C++ in Qt Creator, much more so than Visual Studio. In the former, you can do something like this

qDebug()<< my_trace_string; 

to output my_trace_string to a "console" which can be handy so that you can see what is going on. I wondered if there is a Visual Studio window to which an application can easily write, independently of the dialogs and controls. This is complementary to the use of the debugger. It leaves a visual trace for example.

Upvotes: 0

Views: 1649

Answers (3)

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4017

  1. First of all you could try to call the AllocConsole function to create a Console for your process.
  2. Send your output to the console via _cprintf()And don't forget to #include wherever you use _cprintf()
  3. If you need to close the console output, call FreeConsole();

For more details you could refer to this link

Upvotes: 1

YePhIcK
YePhIcK

Reputation: 5856

OutputDebugString() from debuapi.h (just #include <windows.h>) is your friend. I usually wrap it up into a function like this (use A suffix for ASCII or W suffix for for UNICODE):

void dbgMsg(PCWSTR _format, ...)
{
    va_list args;
    va_start(args, _format);

    WCHAR msg[MAX_PATH];

    if(SUCCEEDED(StringCbVPrintfW(msg, sizeof(msg), _format, args))){
        OutputDebugStringW(msg);
    }
}

That will output into Output window in IDE (next to Call stack, Error List and others) enter image description here

Upvotes: 3

SoronelHaetir
SoronelHaetir

Reputation: 15164

In Visual Studio when working on GUI programs the usual method for such is to use the _RPTX (where X is the number of varadic parameters) macros which when report type is _CRT_WARN print to the Output window within VS itself rather than use an external console window.

For example:

_RPT1(_CRT_WARN, __FUNCTION__ ": hWnd: %#x\r\n", m_hWnd);

Personally I prefer using the following (custom) macros which do not take the report type (I simple use _CRT_WARN) and also do away with the need to know the number of vararg parameters and also the ability to take more than 4 vararg parameters (the limit with the default macros). The _RPT macros have simply never been updated to take advantage of the variadic macro capable preprocessor:

#if !defined(_RPTW)
#if defined(_DEBUG)
#define _RPTW(pszFmt, ...) _CrtDbgReportW(_CRT_WARN, NULL, __LINE__, NULL, (pszFmt), __VA_ARGS__)
#define _RPTWF(dest, fmt, ...) _CrtDbgReportW((dest), _T(__FILE__), __LINE__, NULL, (pszFmt), __VA_ARGS__)
#else
#define _RPTW(pszFmt, ...)
#define _RPTWF(dest, pszFmt)
#endif
#endif // #if !defined(_RPTW)

#if !defined(_RPTA)
#if defined(_DEBUG)
#define _RPTA(pszFmt, ...) _CrtDbgReport(_CRT_WARN, NULL, __LINE__, NULL, (pszFmt), __VA_ARGS__)
#define _RPTAF(dest, fmt, ...) _CrtDbgReport((dest), __FILE__, __LINE__, NULL, (pszFmt), __VA_ARGS__)
#else
#define _RPTA(pszFmt, ...)
#define _RPTAF(dest, pszFmt)
#endif
#endif // #if !defined(_RPTA)

#if !defined(_RPTT)
#if defined(_UNICODE)
#define _RPTT _RPTW
#define _RPTTF _RPTWF
#else
#define _RPTT _RPTA
#define _RPTTF _RPTAF
#endif
#endif // #if !defined(_RPTT)

Using the custom macros the original example becomes:

_RPTA(__FUNCTION__ ": hWnd: %#x\r\n", m_hWnd);

The original way does have the benefit of taking the report type as a parameter, using that output can be redirected to a window (as _ASSERT does), the output window (the default with _CRT_WARN) or to a trace file (requires calling _CrtSetReportMode and _CrtSetReportFile).

Upvotes: 2

Related Questions