Convert NTSTATUS code to Win32 error source code

This snippet submitted by Ian Abbott on 2012-02-24. It has been viewed 17859 times.
Rating of 5.4 with 147 votes

/*
 * This is an alternative to the RtlNtStatusToDosError()
 * function in ntdll.dll.  It uses the GetOverlappedResult()
 * function in kernel32.dll to do the conversion.
 */
#include <windows.h>

DWORD
ConvertNtStatusToWin32Error(LONG ntstatus)
{
        DWORD oldError;
        DWORD result;
        DWORD br;
        OVERLAPPED o;

        o.Internal = ntstatus;
        o.InternalHigh = 0;
        o.Offset = 0;
        o.OffsetHigh = 0;
        o.hEvent = 0;
        oldError = GetLastError();
        GetOverlappedResult(NULL, &o, &br, FALSE);
        result = GetLastError();
        SetLastError(oldError);
        return result;
}





More C and C++ source code snippets