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.cpp
61 lines (49 loc) · 1.75 KB
/
helpers.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
#include "helpers.h"
#include <sstream>
#include <strsafe.h>
std::string FormatWindowsError(DWORD error) {
LPSTR message = nullptr;
if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error, 0, (LPSTR) &message, 1033, nullptr)) {
std::string str(message);
LocalFree(message);
return str;
}
std::stringstream ss;
ss << error;
return ss.str();
}
WindowsException::WindowsException(const char* location) :
error(GetLastError()), location(location) {
message[0] = '\0';
}
WindowsException::WindowsException(const char* location, DWORD error) :
error(error), location(location) {
message[0] = '\0';
}
const char* WindowsException::what() const {
if (!message[0])
StringCchPrintfA(message, ARRAYSIZE(message), "%s: %d", location, error);
return message;
}
SeDebugPrivilege::SeDebugPrivilege() {
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
throw WindowsException("OpenProcessToken");
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!LookupPrivilegeValue(nullptr, SE_DEBUG_NAME, &tp.Privileges[0].Luid))
throw WindowsException("LookupPrivilegeValue SE_DEBUG_NAME");
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, nullptr, nullptr))
throw WindowsException("AdjustTokenPrivileges");
}
SeDebugPrivilege::~SeDebugPrivilege() {
tp.Privileges[0].Attributes = 0;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, 0, nullptr, nullptr))
throw WindowsException("AdjustTokenPrivileges");
}
HRException::HRException(const char* location, HRESULT error):
error(error), location(location) {}
const char* HRException::what() const {
if (!message[0])
StringCchPrintfA(message, ARRAYSIZE(message), "%s: 0x%08X", location, error);
return message;
}