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

时间:2022-03-01 02:56:21

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").

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

3 个解决方案

#1


57  

As mentioned a couple times, the @err pseudo-register will show the last error value, and @err,hr will 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,小时)。暂时支持$和@。

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 very handy 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的优秀书籍“Microsoft .NET和Microsoft Windows调试应用程序”):

  • $tib - shows the thread information block
  • $ tib - 显示线程信息块

  • $clk - shows a clock count (useful for timing functions). To more easily use this, place a $clk watch then an additional $clk=0 watch. 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.
  • $ clk - 显示时钟计数(对计时功能有用)。为了更方便地使用它,请放置$ clk手表,然后再添加$ clk = 0手表。第二个监视器将在显示当前值后清除伪寄存器,因此下一步或步骤将为您提供仅执行该操作的时间。请注意,这是一个粗略的时序,包括相当多的调试器开销,但它仍然非常有用。

#2


6  

ERR,hr in a watch window usually does the trick

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

#3


0  

"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”就可以了。

#1


57  

As mentioned a couple times, the @err pseudo-register will show the last error value, and @err,hr will 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,小时)。暂时支持$和@。

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 very handy 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的优秀书籍“Microsoft .NET和Microsoft Windows调试应用程序”):

  • $tib - shows the thread information block
  • $ tib - 显示线程信息块

  • $clk - shows a clock count (useful for timing functions). To more easily use this, place a $clk watch then an additional $clk=0 watch. 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.
  • $ clk - 显示时钟计数(对计时功能有用)。为了更方便地使用它,请放置$ clk手表,然后再添加$ clk = 0手表。第二个监视器将在显示当前值后清除伪寄存器,因此下一步或步骤将为您提供仅执行该操作的时间。请注意,这是一个粗略的时序,包括相当多的调试器开销,但它仍然非常有用。

#2


6  

ERR,hr in a watch window usually does the trick

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

#3


0  

"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”就可以了。