-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinError.cpp
73 lines (69 loc) · 1.71 KB
/
WinError.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
#include "framework.h"
#include "WinError.h"
#include <iostream>
// This is an almost exact copy-paste of the WinError.cpp file from ggxrd_hitbox_injector project
WinError::WinError() {
code = GetLastError();
}
void WinError::moveFrom(WinError& src) noexcept {
message = src.message;
code = src.code;
src.message = NULL;
src.code = 0;
}
void WinError::copyFrom(const WinError& src) {
code = src.code;
if (src.message) {
size_t len = wcslen(src.message);
message = (LPWSTR)LocalAlloc(0, (len + 1) * sizeof(wchar_t));
if (message) {
memcpy(message, src.message, (len + 1) * sizeof(wchar_t));
}
else {
WinError winErr;
OutputDebugStringW(L"Error in LocalAlloc: ");
OutputDebugStringW(winErr.getMessage());
OutputDebugStringW(L"\n");
return;
}
}
}
WinError::WinError(const WinError& src) {
copyFrom(src);
}
WinError::WinError(WinError&& src) noexcept {
moveFrom(src);
}
LPCWSTR WinError::getMessage() {
if (!message) {
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)(&message),
0, NULL);
}
return message;
}
void WinError::clear() {
if (message) {
LocalFree(message);
message = NULL;
}
}
WinError::~WinError() {
clear();
}
WinError& WinError::operator=(const WinError& src) {
clear();
copyFrom(src);
return *this;
}
WinError& WinError::operator=(WinError&& src) noexcept {
clear();
moveFrom(src);
return *this;
}