How to execute GetLastError () when debugging in Visual Studio

You go through the C / C ++ code and just called the Win32 API, which failed (usually returning some useless common error code, like 0). Your code does not create a subsequent call to GetLastError (), the return value of which you could check for more information about the error.

How can you get the error value without recompiling and reproducing the failure? Entering "GetLastError ()" in the Clock window does not work ("syntax error").

+45
debugging visual-studio winapi
01 Oct '08 at 21:42
source share
3 answers

As mentioned several times, the @err pseudo-register will show the last error value, and @err,hr will show the error as a string (if possible).

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

You can also use the $ err pseudo-register at a conditional breakpoint; therefore, you can break a line of code only if the last error is nonzero. This can be a very convenient trick.

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

  • $tib - shows the flow information block
  • $clk - shows the number of hours (useful for synchronization functions). To make it easier to use, put the clock $clk and then the extra clock $clk=0 . The second second will clear the pseudo register after displaying the current value, so the next step or step above you will give you time only for this action. Note that this is an approximate time, which includes a fair bit of debugger overhead, but it can still be very useful.
+54
01 Oct '08 at 22:19
source share

ERR,hr in the viewport the trick is usually performed

+6
Oct 01 '08 at 21:46
source share

"edit and continue" add code so you can see the error (just do not create a new global variable to save it). It works very well if you can quickly send a call to a pre-existing function that executes such error handling code.

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

If you cannot do this, QBziZ is right "ERR, hr" does it.

0
01 Oct '08 at 21:59
source share



All Articles