-
Notifications
You must be signed in to change notification settings - Fork 0
/
hresult.cpp
73 lines (60 loc) · 1.82 KB
/
hresult.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <winternl.h>
#include <iostream>
#include <sstream>
#define REQUIRE_ARGS(_n) if (argc != (_n) + 1) { \
std::cout << "Expected " << (_n) << "arguments.\n"; return 1; \
} else { \
argv[1] = argv[_n]; \
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: hresult [-w] <error-code>.\n";
std::cerr << "By default interprets error-code as NTSTATUS.\n";
std::cerr << "Pass -w to interpret error-code as HRESULT (DOS error).\n";
std::cerr << "Numbers can be in decimal or hex (0x...) format.\n";
return 1;
}
constexpr int cvt_none = 0;
constexpr int cvt_nt = 1;
int convert = cvt_nt;
if (!_strcmpi(argv[1], "-w")) {
REQUIRE_ARGS(2)
} else {
convert = cvt_none;
REQUIRE_ARGS(1)
}
std::stringstream ss;
DWORD messageId;
if (argv[1][0] == '0' && (argv[1][1] == 'x' || argv[1][1] == 'X')) {
ss << std::hex << &argv[1][2];
} else {
ss << argv[1];
}
ss >> messageId;
std::cout << "Code 0x" << std::hex << std::uppercase << messageId
<< std::dec << std::nouppercase << " (" << messageId << ")\n";
switch (convert) {
case cvt_nt:
messageId = reinterpret_cast<ULONG (*)(NTSTATUS)>(
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlNtStatusToDosError")
)(messageId);
std::cout << "NTSTATUS -> HRESULT = 0x" << std::hex << std::uppercase
<< messageId << std::dec << std::nouppercase << " (" << messageId << ")\n";
break;
default:
break;
}
char buffer[1024];
auto len = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, messageId, 0, buffer, 1024, nullptr
);
if (!len) {
std::cout << "Couldn't decode result code.\n";
return GetLastError();
}
std::cout << buffer;
return 0;
}