How do you get a text description of the last Win32 error?

  • Thread starter Thread starter Will Pittenger
  • Start date Start date
W

Will Pittenger

I see System.Runtime.InteropServices.Marshal.GetLastWind32Error, but I see
no method to get a string based on that error. Do I need to use the Win32
function FormatMessage? If so, that defeats the purpose of C# having
GetLastWin32Error. Since FormatMessage does so many things besides look up
error messages, it tends to be a bit of a burden even in C++. I hate to
think of what calling it would be like under C#.
 
Will,
I see System.Runtime.InteropServices.Marshal.GetLastWind32Error, but I see
no method to get a string based on that error. Do I need to use the Win32
function FormatMessage?

No, you can do

Win32Exception ex = new Win32Exception();
string errMsg = ex.Message;

The Win32Exception class internally checks GetLastWin32Error and
retrieves the corresponding error message.



Mattias
 
Back
Top