3

我前段时间在 SO 上找到了有关此功能的信息,但该主题与 Visual Studio 的隐藏功能(2005-2008)重复?我再也找不到了。

我想使用这样的东西:

#ifdef DEBUG
#define break_here(condition) if (condition) ... // don't remember, what must be here
#else
#define break_here(condition) if (condition) return H_FAIL;
#endif
//...
hresult = do_something(...);
break_here(hresult != H_OK);
//...
var = do_other_thing(...);
break_here(var > MAX_VAR);

它必须表现得像错误断点。这有点像断言,但没有对话,而且更轻量级。

我不能在这里使用正常的断点,因为我的模块是几个项目的一部分,可以在几个 VS 解决方案中进行编辑。当在其他解决方案中编辑代码时,这会导致在一个解决方案中设置的断点在源代码中的某个位置移动。

4

2 回答 2

9

看看DebugBreak

导致当前进程发生断点异常。这允许调用线程向调试器发出信号以处理异常。

例子:

 var = do_other_thing(...);
 if (var > MAX_VAR)
      DebugBreak();
于 2009-09-15T17:22:53.503 回答
2

我忘记了,我也需要 ARM 版本,其中一个不是在 MS Visual Studio 中编译的 :)

此外,我最好不要在我的模块的库版本中链接其他代码。需要为 DebugBreak() 包含“winbase.h”是其中的一件“坏事”,最好有一些内在的东西。但这不是什么“坏事”,因为最终版本中不会有断点 :)

在crashmstr的回答的帮助下,我找到了DebugBreak()的替代品。现在我正在使用以下结构:

#ifdef _DEBUG

    #ifdef _MSC_VER
      #ifdef _X86_
        #define myDebugBreak { __asm { int 3 } }
      #else
        #define myDebugBreak  { __debugbreak(); } // need <intrin.h>
      #endif
    #else
      #define myDebugBreak { asm { trap } } // GCC/XCode ARM11 variant
    #endif

#else

      #define myDebugBreak

#endif

#define break_here(condition) if (condition) { myDebugBreak; return H_FAIL; }
于 2009-09-15T17:57:26.337 回答