summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins/src/registry/registry.cpp
blob: 760fadf1465d1f4704699f888d5c14cf6597b9b3 (plain)
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
#include "stdafx.h"
#include "../error.h"
#include <windows.h>
#include <libcommon/string.h>
#include <libcommon/error.h>
#include <libcommon/registry/registry.h>
#include <libcommon/registry/registrypath.h>
#include <nsis/pluginapi.h>
#include <string>

namespace
{

std::wstring PopString()
{
	//
	// NSIS functions popstring() and popstringn() require that you definitely size the buffer
	// before popping the string. Let's do it ourselves instead.
	//

	if (!g_stacktop || !*g_stacktop)
	{
		THROW_ERROR("NSIS variable stack is corrupted");
	}

	stack_t *th = *g_stacktop;

	std::wstring copy(th->text);

	*g_stacktop = th->next;
	GlobalFree((HGLOBAL)th);

	return copy;
}

} // anonymous namespace

//
// MoveKey "source" "destination"
//
// Moves a registry key.
//
// Example usage:
//
// MoveKey "HKLM\Software\A" "HKLM\Software\B"
//

void __declspec(dllexport) NSISCALL MoveKey
(
	HWND hwndParent,
	int string_size,
	LPTSTR variables,
	stack_t **stacktop,
	extra_parameters *extra,
	...
)
{
	EXDLL_INIT();

	try
	{
		const auto source = PopString();
		const auto destination = PopString();

		auto typedSource = common::registry::RegistryPath(source);
		auto typedDestination = common::registry::RegistryPath(destination);

		common::registry::Registry::MoveKey(typedSource.key(), typedSource.subkey(), typedDestination.key(),
			typedDestination.subkey(), common::registry::RegistryView::Force64);

		pushstring(L"");
		pushint(NsisStatus::SUCCESS);
	}
	catch (std::exception &err)
	{
		pushstring(common::string::ToWide(err.what()).c_str());
		pushint(NsisStatus::GENERAL_ERROR);
	}
	catch (...)
	{
		pushstring(L"Unspecified error");
		pushint(NsisStatus::GENERAL_ERROR);
	}
}