Skip to content

Commit c7188d1

Browse files
committed
[win] Add hkey::delete_value() function
1 parent 1d4bd64 commit c7188d1

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

base/win/registry.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Base Library
2-
// Copyright (c) 2023 Igara Studio S.A.
2+
// Copyright (c) 2023-2025 Igara Studio S.A.
33
// Copyright (c) 2017 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -215,6 +215,16 @@ void hkey::dword(const std::string& name, const DWORD value)
215215
throw Win32Exception("Error setting registry value");
216216
}
217217

218+
void hkey::delete_value(const std::string& name)
219+
{
220+
if (!m_hkey)
221+
return;
222+
223+
LONG result = RegDeleteValueW(m_hkey, from_utf8(name).c_str());
224+
if (result != ERROR_SUCCESS)
225+
throw Win32Exception("Error deleting registry value");
226+
}
227+
218228
void hkey::delete_tree(const std::string& subkey)
219229
{
220230
if (!m_hkey)

base/win/registry.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Base Library
2-
// Copyright (c) 2021-2023 Igara Studio S.A.
2+
// Copyright (c) 2021-2025 Igara Studio S.A.
33
// Copyright (c) 2017 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -37,6 +37,7 @@ class hkey {
3737
~hkey();
3838

3939
hkey& operator=(hkey&& key);
40+
operator bool() const { return (m_hkey != nullptr); }
4041

4142
HKEY handle() const { return m_hkey; }
4243

@@ -52,6 +53,8 @@ class hkey {
5253
DWORD dword(const std::string& name);
5354
void dword(const std::string& name, const DWORD value);
5455

56+
void delete_value(const std::string& name);
57+
5558
// Deletes keys in the given subkey, if subkey == "", all children
5659
// keys are deleted but this hkey isn't (the root is kept).
5760
void delete_tree(const std::string& subkey);

base/win/registry_tests.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Base Library
2-
// Copyright (c) 2024 Igara Studio S.A.
2+
// Copyright (c) 2024-2025 Igara Studio S.A.
33
// Copyright (c) 2017 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -33,8 +33,8 @@ TEST(Registry, OpenKey)
3333
TEST(Registry, CreateKey)
3434
{
3535
try {
36-
hkey k = hkey::current_user();
37-
k = k.create("Software\\Classes\\.laf-base-test-extension");
36+
hkey hkcu = hkey::current_user();
37+
hkey k = hkcu.create("Software\\Classes\\.laf-base-test-extension");
3838
k.string("", "testing");
3939
k.string("A", "value A");
4040
k.string("B", "value B");
@@ -49,7 +49,46 @@ TEST(Registry, CreateKey)
4949
// We cannot use k.delete_tree("") because it does delete the
5050
// whole tree, but leaves the root key untouched.
5151

52-
hkey::current_user().delete_tree("Software\\Classes\\.laf-base-test-extension");
52+
hkcu.delete_tree("Software\\Classes\\.laf-base-test-extension");
53+
}
54+
catch (Win32Exception& ex) {
55+
printf("Win32Exception: %s\nError Code: %d\n", ex.what(), ex.errorCode());
56+
throw;
57+
}
58+
catch (const std::exception& ex) {
59+
printf("std::exception: %s\n", ex.what());
60+
throw;
61+
}
62+
}
63+
64+
TEST(Registry, DeleteValue)
65+
{
66+
try {
67+
hkey hkcu = hkey::current_user();
68+
hkey k = hkcu.create("Software\\Classes\\.laf-base-test-delete-value");
69+
k.string("A", "value A");
70+
k.string("B", "");
71+
k.dword("C", 2);
72+
73+
EXPECT_TRUE(k.exists("A"));
74+
EXPECT_TRUE(k.exists("B"));
75+
EXPECT_TRUE(k.exists("C"));
76+
EXPECT_EQ("value A", k.string("A"));
77+
EXPECT_EQ("", k.string("B"));
78+
EXPECT_EQ(2, k.dword("C"));
79+
80+
k.delete_value("A");
81+
k.delete_value("B");
82+
k.delete_value("C");
83+
84+
EXPECT_FALSE(k.exists("A"));
85+
EXPECT_FALSE(k.exists("B"));
86+
EXPECT_FALSE(k.exists("C"));
87+
EXPECT_EQ("", k.string("A"));
88+
EXPECT_EQ("", k.string("B"));
89+
EXPECT_EQ(0, k.dword("C"));
90+
91+
hkcu.delete_tree("Software\\Classes\\.laf-base-test-delete-value");
5392
}
5493
catch (Win32Exception& ex) {
5594
printf("Win32Exception: %s\nError Code: %d\n", ex.what(), ex.errorCode());

0 commit comments

Comments
 (0)