How can I use the TRACE macro in non-MFC projects?

You can try the DebugOutputString function. TRACE is only enabled in debug builds.


Build your own.

trace.cpp:

#ifdef _DEBUG
bool _trace(TCHAR *format, ...)
{
   TCHAR buffer[1000];

   va_list argptr;
   va_start(argptr, format);
   wvsprintf(buffer, format, argptr);
   va_end(argptr);

   OutputDebugString(buffer);

   return true;
}
#endif

trace.h:

#include <windows.h>
#ifdef _DEBUG
bool _trace(TCHAR *format, ...);
#define TRACE _trace
#else
#define TRACE false && _trace
#endif

then just #include "trace.h" and you're all set.

Disclaimer: I just copy/pasted this code from a personal project and took out some project specific stuff, but there's no reason it shouldn't work. ;-)


If you use ATL you can try ATLTRACE.

TRACE is defined in afx.h as (at least in vs 2008):

// extern ATL::CTrace TRACE;
#define TRACE ATLTRACE

And ATLTRACE can be found in atltrace.h