visual-studio 在 Visual Studio 中调试时如何执行 GetLastError()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/159888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:41:33  来源:igfitidea点击:

How to execute GetLastError() while debugging in Visual Studio

visual-studiodebuggingwinapi

提问by Peter Baer

You're stepping through C/C++ code and have just called a Win32 API that has failed (typically by returning some unhelpful generic error code, like 0). Your code doesn't make a subsequent GetLastError() call whose return value you could inspect for further error information.

您正在逐步执行 C/C++ 代码,并且刚刚调用了一个失败的 Win32 API(通常是通过返回一些无用的通用错误代码,例如 0)。您的代码不会进行后续的 GetLastError() 调用,您可以检查其返回值以获取更多错误信息。

How can you get the error value without recompiling and reproducing the failure? Entering "GetLastError()" in the Watch window doesn't work ("syntax error").

如何在不重新编译和重现失败的情况下获得错误值?在监视窗口中输入“GetLastError()”不起作用(“语法错误”)。

回答by Michael Burr

As mentioned a couple times, the @errpseudo-register will show the last error value, and @err,hrwill show the error as a string (if it can).

如前所述,@err伪寄存器将显示最后一个错误值,并将@err,hr错误显示为字符串(如果可以的话)。

According to Andy Pennell, a member of the Visual Studio team, starting with VS 7 (Visual Studio .NET 2002), using the '@' character to indicate pseudo-registers is deprecated - they prefer to use '$' (as in $err,hr). Both $ and @ are supported for the time being.

根据 Visual Studio 团队成员 Andy Pennell 的说法,从 VS 7 (Visual Studio .NET 2002) 开始,不推荐使用“@”字符来指示伪寄存器 - 他们更喜欢使用“$”(如$err,hr) . $和@暂时都支持。

You can also use the $err pseudo-register in a conditional breakpoint; so you can break on a line of code only if the last error is non-zero. This can be a veryhandy trick.

您还可以在条件断点中使用 $err 伪寄存器;所以只有当最后一个错误不为零时,您才能中断一行代码。这可能是一个非常方便的技巧。

Some other pseudo registers that you may find handy (from John Robbins' outstanding book, "Debugging Applications for Microsoft .NET and Microsoft Windows"):

您可能会发现一些其他的伪寄存器(来自 John Robbins 的杰出著作“Debugging Applications for Microsoft .NET and Microsoft Windows”):

  • $tib- shows the thread information block
  • $clk- shows a clock count (useful for timing functions). To more easily use this, place a $clkwatch then an additional $clk=0watch. The second watch will clear the pseudo register after the display of the current value, so the next step or step over you do gives you the time for that action only. Note that this is a rough timing that includes a fair bit of debugger overhead, but it can still be very useful.
  • $tib- 显示线程信息块
  • $clk- 显示时钟计数(对计时功能有用)。为了更轻松地使用它,先放置一块$clk手表,然后再放置一块$clk=0手表。第二个手表将在显示当前值后清除伪寄存器,因此您的下一步或跨步只会为您提供该操作的时间。请注意,这是一个粗略的时间安排,包括相当多的调试器开销,但它仍然非常有用。

回答by QBziZ

ERR,hrin a watch window usually does the trick

ERR,hr在监视窗口中通常可以解决问题

回答by gbjbaanb

"edit and continue" add the code so you can see the error (just don't create a new global variable to store it). It works really well if you can quickly put a call to a pre-existing function that executes this kind of error handling code.

“编辑并继续”添加代码,以便您可以看到错误(只是不要创建新的全局变量来存储它)。如果您可以快速调用执行此类错误处理代码的预先存在的函数,它会非常有效。

As a bonus, you can leave the new code there for the future too.

作为奖励,您也可以将新代码留在那里以备将来使用。

If you can't do this, then QBziZ is right "ERR,hr" does it.

如果您不能这样做,那么 QBziZ 是正确的“ERR,hr”。