This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.h
108 lines (90 loc) · 2.28 KB
/
helpers.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#ifndef id_26FCBE24_1243_4829_89F4_B266B158236E
#define id_26FCBE24_1243_4829_89F4_B266B158236E
#include <windows.h>
#include <exception>
#include <string>
#if defined(_MSC_VER) && _MSC_VER < 1600
const class nullptr_t {
public:
template<class T>
inline operator T*() const // convertible to any type of null non-member pointer...
{ return 0; }
template<class C, class T>
inline operator T C::*() const // or any type of null member pointer...
{ return 0; }
private:
void operator&() const; // Can't take address of nullptr
} nullptr = {};
#endif
class AutoHandle {
HANDLE handle;
public:
AutoHandle() : handle(nullptr) {}
explicit AutoHandle(HANDLE h) : handle(h) {}
~AutoHandle() { CloseHandle(handle); }
operator HANDLE() { return handle; }
HANDLE get() { return handle; }
void set(HANDLE h) { *this = h; }
AutoHandle &operator=(HANDLE h) {
if (handle)
CloseHandle(handle);
handle = h;
return *this;
}
HANDLE detach() {
HANDLE h = handle;
handle = nullptr;
return h;
}
void close() {
CloseHandle(handle);
handle = nullptr;
}
};
class SeDebugPrivilege {
HANDLE hToken;
TOKEN_PRIVILEGES tp;
public:
SeDebugPrivilege();
~SeDebugPrivilege();
};
std::string FormatWindowsError(DWORD error);
class WindowsException : public std::exception {
DWORD error;
mutable char message[1024];
const char *location;
public:
WindowsException(const char* location);
WindowsException(const char* location, DWORD error);
const char* what() const override;
DWORD code() { return error; }
};
class HRException : public std::exception {
HRESULT error;
mutable char message[1024];
const char *location;
public:
HRException(const char* location, HRESULT error);
const char* what() const override;
HRESULT code() { return error; }
};
inline void writeQword(BYTE *buf, DWORD64 qword)
{
buf[0] = qword & 0xFF;
buf[1] = (qword >> 8) & 0xFF;
buf[2] = (qword >> 16) & 0xFF;
buf[3] = (qword >> 24) & 0xFF;
buf[4] = (qword >> 32) & 0xFF;
buf[5] = (qword >> 40) & 0xFF;
buf[6] = (qword >> 48) & 0xFF;
buf[7] = (qword >> 56) & 0xFF;
}
inline void writeDword(BYTE *buf, DWORD dword)
{
buf[0] = dword & 0xFF;
buf[1] = (dword >> 8) & 0xFF;
buf[2] = (dword >> 16) & 0xFF;
buf[3] = (dword >> 24) & 0xFF;
}
#endif