summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins/src/registry/registry.cpp
diff options
context:
space:
mode:
authorOdd Stranne <odd@mullvad.net>2018-11-02 09:25:16 +0100
committerOdd Stranne <odd@mullvad.net>2018-11-02 09:27:36 +0100
commit3cd771f55f8630a4ff93669955c28b0757e9a254 (patch)
treeea0639cef93649a3b8603a6b0cddab1c0c7c4b47 /windows/nsis-plugins/src/registry/registry.cpp
parent9ba298fe1a67a0935d0e77ff4efed1e95371125b (diff)
downloadmullvadvpn-3cd771f55f8630a4ff93669955c28b0757e9a254.tar.xz
mullvadvpn-3cd771f55f8630a4ff93669955c28b0757e9a254.zip
Add NSIS plugin for working with the registry
Diffstat (limited to 'windows/nsis-plugins/src/registry/registry.cpp')
-rw-r--r--windows/nsis-plugins/src/registry/registry.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/windows/nsis-plugins/src/registry/registry.cpp b/windows/nsis-plugins/src/registry/registry.cpp
new file mode 100644
index 0000000000..e94c0540ee
--- /dev/null
+++ b/windows/nsis-plugins/src/registry/registry.cpp
@@ -0,0 +1,88 @@
+#include "stdafx.h"
+#include <windows.h>
+#include <libcommon/string.h>
+#include <libcommon/registry/registry.h>
+#include <libcommon/registry/registrypath.h>
+#include <nsis/pluginapi.h>
+#include <string>
+#include <stdexcept>
+
+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 std::runtime_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"
+//
+enum class MoveKeyStatus
+{
+ GENERAL_ERROR = 0,
+ SUCCESS
+};
+
+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(MoveKeyStatus::SUCCESS);
+ }
+ catch (std::exception &err)
+ {
+ pushstring(common::string::ToWide(err.what()).c_str());
+ pushint(MoveKeyStatus::GENERAL_ERROR);
+ }
+ catch (...)
+ {
+ pushstring(L"Unspecified error");
+ pushint(MoveKeyStatus::GENERAL_ERROR);
+ }
+}