Skip to content

Commit e5afc31

Browse files
committed
Add RegistryApi and centralize registry access
1 parent 9ba91f1 commit e5afc31

4 files changed

Lines changed: 209 additions & 272 deletions

File tree

Lines changed: 11 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using SecondaryClick.WinApi;
2-
using System.Text;
32

43
namespace SecondaryClick;
54

@@ -63,7 +62,7 @@ public T Get()
6362
return (T)(object)stringValue;
6463
}
6564

66-
if (valueType == Advapi32.REG_DWORD && data.Length >= sizeof(int))
65+
if (RegistryApi.IsDwordType(valueType) && data.Length >= sizeof(int))
6766
{
6867
int intValue = BitConverter.ToInt32(data, 0);
6968
return (T)Convert.ChangeType(intValue, typeof(T));
@@ -80,101 +79,25 @@ public T Get()
8079

8180
public void Set(T value)
8281
{
83-
int createResult = Advapi32.RegCreateKeyEx(
84-
Advapi32.HKEY_CURRENT_USER,
85-
RegistryPath,
86-
0,
87-
null,
88-
0,
89-
Advapi32.KEY_SET_VALUE,
90-
IntPtr.Zero,
91-
out IntPtr keyHandle,
92-
out _);
93-
94-
if (createResult != Win32Error.ERROR_SUCCESS)
95-
return;
96-
97-
try
98-
{
99-
if (typeof(T) == typeof(bool))
100-
{
101-
bool boolValue = (bool)(object)value!;
102-
byte[] data = BitConverter.GetBytes(boolValue ? 1 : 0);
103-
_ = Advapi32.RegSetValueEx(
104-
keyHandle,
105-
_name,
106-
0,
107-
Advapi32.REG_DWORD,
108-
data,
109-
data.Length);
110-
return;
111-
}
112-
113-
string stringValue = value?.ToString() ?? string.Empty;
114-
int dataSize = (stringValue.Length + 1) * sizeof(char);
115-
_ = Advapi32.RegSetValueEx(
116-
keyHandle,
117-
_name,
118-
0,
119-
Advapi32.REG_SZ,
120-
stringValue,
121-
dataSize);
122-
}
123-
finally
82+
if (typeof(T) == typeof(bool))
12483
{
125-
_ = Advapi32.RegCloseKey(keyHandle);
84+
bool boolValue = (bool)(object)value!;
85+
_ = RegistryApi.SetDwordCurrentUser(RegistryPath, _name, boolValue ? 1 : 0);
86+
return;
12687
}
88+
89+
string stringValue = value?.ToString() ?? string.Empty;
90+
_ = RegistryApi.SetStringCurrentUser(RegistryPath, _name, stringValue);
12791
}
12892

12993
private bool TryReadRegistryValue(out uint valueType, out byte[] data)
13094
{
131-
valueType = 0;
132-
data = [];
133-
134-
int openResult = Advapi32.RegOpenKeyEx(
135-
Advapi32.HKEY_CURRENT_USER,
136-
RegistryPath,
137-
0,
138-
Advapi32.KEY_QUERY_VALUE,
139-
out IntPtr keyHandle);
140-
141-
if (openResult != Win32Error.ERROR_SUCCESS)
142-
return false;
143-
144-
try
145-
{
146-
uint dataSize = 0;
147-
int queryResult = Advapi32.RegQueryValueEx(
148-
keyHandle,
149-
_name,
150-
0,
151-
out valueType,
152-
null,
153-
ref dataSize);
154-
155-
if (queryResult != Win32Error.ERROR_SUCCESS || dataSize == 0)
156-
return false;
157-
158-
data = new byte[dataSize];
159-
queryResult = Advapi32.RegQueryValueEx(
160-
keyHandle,
161-
_name,
162-
0,
163-
out valueType,
164-
data,
165-
ref dataSize);
166-
167-
return queryResult == Win32Error.ERROR_SUCCESS;
168-
}
169-
finally
170-
{
171-
_ = Advapi32.RegCloseKey(keyHandle);
172-
}
95+
return RegistryApi.TryReadRawCurrentUser(RegistryPath, _name, out valueType, out data);
17396
}
17497

17598
private static bool ConvertToBool(uint valueType, byte[] data)
17699
{
177-
if (valueType == Advapi32.REG_DWORD && data.Length >= sizeof(int))
100+
if (RegistryApi.IsDwordType(valueType) && data.Length >= sizeof(int))
178101
{
179102
return BitConverter.ToInt32(data, 0) != 0;
180103
}
@@ -191,6 +114,6 @@ private static bool ConvertToBool(uint valueType, byte[] data)
191114

192115
private static string DecodeString(byte[] data)
193116
{
194-
return Encoding.Unicode.GetString(data).TrimEnd('\0');
117+
return RegistryApi.DecodeUnicodeString(data);
195118
}
196119
}

src/SecondaryClick/Gestures/Touchpads/PrecisionTouchpadRegistrySettings.cs

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -66,54 +66,10 @@ public static bool SetRightClickZoneEnabled(bool enabled)
6666
/// <returns>A nullable boolean, or null if the value is not found or not accessible.</returns>
6767
private static bool? ReadBoolValue(string valueName)
6868
{
69-
int openResult = Advapi32.RegOpenKeyEx(
70-
Advapi32.HKEY_CURRENT_USER,
71-
KeyPath,
72-
0,
73-
Advapi32.KEY_QUERY_VALUE,
74-
out IntPtr keyHandle);
75-
76-
if (openResult != Win32Error.ERROR_SUCCESS)
69+
if (!RegistryApi.TryReadDwordCurrentUser(KeyPath, valueName, out int intValue))
7770
return null;
7871

79-
try
80-
{
81-
uint valueType;
82-
uint dataSize = 0;
83-
84-
int queryResult = Advapi32.RegQueryValueEx(
85-
keyHandle,
86-
valueName,
87-
0,
88-
out valueType,
89-
null,
90-
ref dataSize);
91-
92-
if (queryResult != Win32Error.ERROR_SUCCESS)
93-
return null;
94-
95-
if (valueType != Advapi32.REG_DWORD || dataSize < sizeof(int))
96-
return null;
97-
98-
byte[] data = new byte[dataSize];
99-
queryResult = Advapi32.RegQueryValueEx(
100-
keyHandle,
101-
valueName,
102-
0,
103-
out valueType,
104-
data,
105-
ref dataSize);
106-
107-
if (queryResult != Win32Error.ERROR_SUCCESS)
108-
return null;
109-
110-
int intValue = BitConverter.ToInt32(data, 0);
111-
return unchecked((uint)intValue) == 0xFFFFFFFF;
112-
}
113-
finally
114-
{
115-
_ = Advapi32.RegCloseKey(keyHandle);
116-
}
72+
return unchecked((uint)intValue) == 0xFFFFFFFF;
11773
}
11874

11975
/// <summary>
@@ -124,37 +80,6 @@ public static bool SetRightClickZoneEnabled(bool enabled)
12480
/// <returns>True if the operation succeeded, false otherwise.</returns>
12581
private static bool WriteBoolValue(string valueName, bool enabled)
12682
{
127-
int createResult = Advapi32.RegCreateKeyEx(
128-
Advapi32.HKEY_CURRENT_USER,
129-
KeyPath,
130-
0,
131-
null,
132-
0,
133-
Advapi32.KEY_SET_VALUE,
134-
IntPtr.Zero,
135-
out IntPtr keyHandle,
136-
out _);
137-
138-
if (createResult != Win32Error.ERROR_SUCCESS)
139-
return false;
140-
141-
try
142-
{
143-
byte[] data = BitConverter.GetBytes(enabled ? unchecked((int)0xFFFFFFFF) : 0);
144-
145-
int setResult = Advapi32.RegSetValueEx(
146-
keyHandle,
147-
valueName,
148-
0,
149-
Advapi32.REG_DWORD,
150-
data,
151-
data.Length);
152-
153-
return setResult == Win32Error.ERROR_SUCCESS;
154-
}
155-
finally
156-
{
157-
_ = Advapi32.RegCloseKey(keyHandle);
158-
}
83+
return RegistryApi.SetDwordCurrentUser(KeyPath, valueName, enabled ? unchecked((int)0xFFFFFFFF) : 0);
15984
}
16085
}
Lines changed: 5 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using SecondaryClick.WinApi;
22
using System.Diagnostics;
3-
using System.Text;
43

54
namespace SecondaryClick;
65

@@ -32,120 +31,20 @@ private static bool TryGetStartupCommand(out string? startupCommand)
3231
{
3332
startupCommand = null;
3433

35-
int openResult = Advapi32.RegOpenKeyEx(
36-
Advapi32.HKEY_CURRENT_USER,
37-
RunKeyPath,
38-
0,
39-
Advapi32.KEY_QUERY_VALUE,
40-
out IntPtr keyHandle);
41-
42-
if (openResult != Win32Error.ERROR_SUCCESS)
34+
if (!RegistryApi.TryReadStringCurrentUser(RunKeyPath, StartupValueName, out string command))
4335
return false;
4436

45-
try
46-
{
47-
uint type;
48-
uint dataSize = 0;
49-
50-
int queryResult = Advapi32.RegQueryValueEx(
51-
keyHandle,
52-
StartupValueName,
53-
0,
54-
out type,
55-
null,
56-
ref dataSize);
57-
58-
if (queryResult != Win32Error.ERROR_SUCCESS)
59-
return false;
60-
61-
if (type != Advapi32.REG_SZ && type != Advapi32.REG_EXPAND_SZ)
62-
return false;
63-
64-
if (dataSize == 0)
65-
return false;
66-
67-
byte[] data = new byte[dataSize];
68-
69-
queryResult = Advapi32.RegQueryValueEx(
70-
keyHandle,
71-
StartupValueName,
72-
0,
73-
out type,
74-
data,
75-
ref dataSize);
76-
77-
if (queryResult != Win32Error.ERROR_SUCCESS)
78-
return false;
79-
80-
startupCommand = Encoding.Unicode.GetString(data).TrimEnd('\0');
81-
return !string.IsNullOrWhiteSpace(startupCommand);
82-
}
83-
finally
84-
{
85-
_ = Advapi32.RegCloseKey(keyHandle);
86-
}
37+
startupCommand = command;
38+
return !string.IsNullOrWhiteSpace(startupCommand);
8739
}
8840

8941
private static bool SetStartupCommand(string startupCommand)
9042
{
91-
int createResult = Advapi32.RegCreateKeyEx(
92-
Advapi32.HKEY_CURRENT_USER,
93-
RunKeyPath,
94-
0,
95-
null,
96-
0,
97-
Advapi32.KEY_SET_VALUE,
98-
IntPtr.Zero,
99-
out IntPtr keyHandle,
100-
out _);
101-
102-
if (createResult != Win32Error.ERROR_SUCCESS)
103-
return false;
104-
105-
try
106-
{
107-
int dataSize = (startupCommand.Length + 1) * sizeof(char);
108-
109-
int setResult = Advapi32.RegSetValueEx(
110-
keyHandle,
111-
StartupValueName,
112-
0,
113-
Advapi32.REG_SZ,
114-
startupCommand,
115-
dataSize);
116-
117-
return setResult == Win32Error.ERROR_SUCCESS;
118-
}
119-
finally
120-
{
121-
_ = Advapi32.RegCloseKey(keyHandle);
122-
}
43+
return RegistryApi.SetStringCurrentUser(RunKeyPath, StartupValueName, startupCommand);
12344
}
12445

12546
private static bool DeleteStartupCommand()
12647
{
127-
int openResult = Advapi32.RegOpenKeyEx(
128-
Advapi32.HKEY_CURRENT_USER,
129-
RunKeyPath,
130-
0,
131-
Advapi32.KEY_SET_VALUE,
132-
out IntPtr keyHandle);
133-
134-
if (openResult == Win32Error.ERROR_FILE_NOT_FOUND)
135-
return true;
136-
137-
if (openResult != Win32Error.ERROR_SUCCESS)
138-
return false;
139-
140-
try
141-
{
142-
int deleteResult = Advapi32.RegDeleteValue(keyHandle, StartupValueName);
143-
return deleteResult == Win32Error.ERROR_SUCCESS
144-
|| deleteResult == Win32Error.ERROR_FILE_NOT_FOUND;
145-
}
146-
finally
147-
{
148-
_ = Advapi32.RegCloseKey(keyHandle);
149-
}
48+
return RegistryApi.DeleteValueCurrentUser(RunKeyPath, StartupValueName);
15049
}
15150
}

0 commit comments

Comments
 (0)