Win32和MFC项目如何输出调试信息到VS的调试窗口

直接举例说明:

Win32项目:

#include <Windows.h>

OutputDebugString(TEXT("调试信息:MyCircleImpl::~MyCircleImpl GETS CALLED!"));

MFC项目:

#include  <afxwin.h>

TRACE(_T("调试信息:MyCircleImpl::~MyCircleImpl GETS CALLED!"));

 

用法上,这两个的用法跟printf是一样的。

区别在于,TRACE是对OutputDebugString的封装,只有在DEBUG配置下才会起作用。(定义了_DEBUG宏),否则TRACE什么都不做(去afx.h查看TRACE的定义)。

OutputDebugString不受_DEBUG宏的控制,但是在没有debugger的情况下,OutputDebugString进入其内部后什么也不做直接return

 

所以如果是Win32项目,最好的做法如下:

Trace.h

#pragma once

#include <Windows.h>

#ifdef _DEBUG
#define TRACE OutputDebugString
#else
#define TRACE __noop // afx.h中如果没定义_DEBUG,TRACE就定义为__noop从而在Release配置下不会产生影响
#endif

client code:

#include "Trace.h"

TRACE(TEXT("调试信息:MyCircleImpl::~MyCircleImpl GETS CALLED!\n"));

 

 

参考资料:

http://stackoverflow.com/questions/494653/how-can-i-use-the-trace-macro-in-non-mfc-projects

https://msdn.microsoft.com/en-us/library/s6btaxcs.aspx

http://bbs.csdn.net/topics/390482393

https://msdn.microsoft.com/en-us/library/windows/apps/aa363362(v=vs.85).aspx

 

posted @ 2015-10-01 13:06  rldts  阅读(1323)  评论(0编辑  收藏  举报