Skip to content

Instantly share code, notes, and snippets.

@ian-abbott
Last active December 19, 2022 13:16
Show Gist options
  • Save ian-abbott/732c5b88182a1941a603 to your computer and use it in GitHub Desktop.
Save ian-abbott/732c5b88182a1941a603 to your computer and use it in GitHub Desktop.
C function to convert an NTSTATUS code into a Win32 error code - alternative to RtlNtStatusToDosError()
#include <windows.h>
/*
* This is an alternative to the RtlNtStatusToDosError()
* function in ntdll.dll. It uses the GetOverlappedResult()
* function in kernel32.dll to do the conversion.
*/
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;
}
@ian-abbott
Copy link
Author

@sergio-nsk GetOverlappedResult is expected to fail. ConvertNtStatusToWin32Error(0xC0000022L) should return 5, mapping STATUS_ACCESS_DENIED to ERROR_ACCESS_DENIED.

@sergio-nsk
Copy link

sergio-nsk commented Mar 5, 2021

You are right, it is working well now. Don't know what it was recently, always returned 87.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment