I get an exception from .net process that is using interop call to win32 api function. I have a debugger attached and I want to see the value of LastError. Is it possible to see the value of LastError from Visual Studio debugger?
Two notes: The process that throws the exception is not mine and I can't modify its source code to get the value of LastError.
I'm using the beta version of Visual Studio 2010 I assumes that a solution that will work for VS 2008 will work with 2010.
Thanks.
You can call Marshal.GetLastWin32Error() to get the last Windows API error message. This is the suggested method (as opposed to using P/Invoke).
From the docs: "GetLastWin32Error exposes the Win32 GetLastError API method from Kernel32.DLL. This method exists because it is not safe to make a direct platform invoke call to GetLastError to obtain this information. If you want to access this error code, you must call GetLastWin32Error rather than writing your own platform invoke definition for GetLastError and calling it. The common language runtime can make internal calls to APIs that overwrite the operating system maintained GetLastError."
Also, in terms of not modifying source:
This will work in the Visual Studio immediate window, provided the P/Invoke call was decorated with DllImportAttribute.SetLastError. If the P/Invoke call to the windows API was missing that attribute, though, you'll be somewhat out of luck, since it's very possible that the marshalling api will overwrite the value in GetLastError before you could see it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With