-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRegHive.h
More file actions
61 lines (53 loc) · 1.53 KB
/
Copy pathRegHive.h
File metadata and controls
61 lines (53 loc) · 1.53 KB
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
#pragma once
#include <system_error>
#include <utility>
#include <Windows.h>
class RegHive {
public:
RegHive(HKEY hkey, LPCWSTR subkey, LPCWSTR file) : _hkey(hkey), _subkey(subkey) {
auto result = RegLoadKeyW(hkey, subkey, file);
if (result != ERROR_SUCCESS)
throw std::system_error(result, std::system_category(), "RegLoadKeyW");
}
RegHive(const RegHive &) = delete;
RegHive &operator=(const RegHive &) = delete;
RegHive(RegHive &&other) noexcept {
swap(*this, other);
}
RegHive &operator=(RegHive &&other) noexcept {
if (this != &other) {
dispose();
swap(*this, other);
}
return *this;
};
~RegHive() {
dispose();
}
constexpr HKEY HKey() const noexcept {
return _hkey;
}
constexpr LPCWSTR SubKey() const noexcept {
return _subkey;
}
int release() noexcept {
dispose();
}
friend void swap(RegHive &self, RegHive &other) noexcept {
using std::swap;
swap(self._hkey, other._hkey);
swap(self._subkey, other._subkey);
}
private:
HKEY _hkey = (HKEY)INVALID_HANDLE_VALUE;
LPCWSTR _subkey = nullptr;
void dispose() noexcept {
if (_hkey != (HKEY)INVALID_HANDLE_VALUE) {
auto result = RegUnLoadKeyW(_hkey, _subkey);
if (result != ERROR_SUCCESS)
wprintf(L"Cannot unload key: 0x%lx\n", result);
_hkey = (HKEY)INVALID_HANDLE_VALUE;
_subkey = nullptr;
}
}
};