Win32 Errors: How to Format GetLastError() Output into Readable Strings

I’ve been doing a moderate amount of native Win32 C++ programming over the past few weeks, and occasionally I’ve needed to set up some debug points to print errors that occur during file and memory I/O.

When something goes wrong inside the Win32 API, some methods will return a system error code directly (such as all of the registry methods) and others will simple return a NULL pointer or a 0-length DWORD and require you to poll the GetLastError method yourself.

Unfortunately, these error codes are just long integers (DWORDs) and don’t contain any of that human-friendly information that I’m used to for .NET exceptions.

I created a Gist on Github that shows how we do it and have also included the code below:

Discussion, links, and tweets

I'm the CTO and founder of Petabridge, where I'm making distributed programming for .NET developers easy by working on Akka.NET, Phobos, and more..