How to call GetLastError in the Debugger (Visual Studio)

Every time I debug with someone else at their machine, they are surprised by this little trick.  So I will post it here.

In Windows programming, you often have Win32 calls which return an error, and you then need to call GetLastError() in order to find the real error number.  If you are in the debugger, you don’t need to modify your code to call GetLastError at that point. 

Instead, just add “@err” or “@err,hr” to your debug watch window.  Now it’s more like looking at errno.

2 thoughts on “How to call GetLastError in the Debugger (Visual Studio)

  • March 28, 2011 at 8:55 am

    Was easier to google this answer here than in the official docs, hehe.

    I would caution against the ,hr though, since GetLastError does not return an HRESULT. You want to copy the number and put it in the Tools->Error Lookup to see what it means.


Leave a Reply

Your email address will not be published. Required fields are marked *