forked from zodiacon/TotalRegistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeValueCommand.cpp
43 lines (37 loc) · 1.07 KB
/
ChangeValueCommand.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
#include "pch.h"
#include "ChangeValueCommand.h"
#include "Registry.h"
ChangeValueCommand::ChangeValueCommand(PCWSTR path, PCWSTR name, DWORD type, const PVOID data, LONG size, AppCommandCallback<ChangeValueCommand> cb)
: RegAppCommandBase(L"Change Value " + CString(name), path, name, cb), _type(type), _size(size) {
_data = std::make_unique<BYTE[]>(size);
memcpy(_data.get(), data, size);
}
bool ChangeValueCommand::Execute() {
auto key = Registry::OpenKey(GetPath(), KEY_SET_VALUE | KEY_QUERY_VALUE);
if (!key)
return false;
//
// read old value
//
ULONG size = 0;
DWORD type;
key.QueryValue(GetName(), &type, nullptr, &size);
auto buffer = std::make_unique<BYTE[]>(size);
auto error = key.QueryValue(GetName(), &type, buffer.get(), &size);
if (ERROR_SUCCESS != error) {
::SetLastError(error);
return false;
}
//
// make the change
//
error = key.SetValue(GetName(), _type, _data.get(), _size);
if (ERROR_SUCCESS != error) {
::SetLastError(error);
return false;
}
_size = size;
_type = type;
_data = std::move(buffer);
return InvokeCallback(false);
}